C# 修复 TableLayoutPanel 中每一行的行高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15004937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fix row height of every row in TableLayoutPanel
提问by iVad
I'm working on Windows c#.
我正在使用 Windows c#。
Firstly, the things those can not be change as my need are following:
首先,根据我的需要,那些无法改变的事情如下:
- The Size of
TableLayoutPanel
is fixed. - The Total # of columns are fixed.
- 的大小
TableLayoutPanel
是固定的。 - 总列数是固定的。
Now, I want to set a fix height for all rows but as increasing the rows, if I set the RowStyle
property to Percent
with 100.0F
then it works fine for 3 to 4 items, but after 4-5 items, the control on one row overwrites controls on another row.
现在,我想为所有行设置一个固定高度,但随着行的增加,如果我将RowStyle
属性设置为Percent
with100.0F
那么它可以正常工作 3 到 4 个项目,但是在 4-5 个项目之后,一行上的控件会覆盖上的控件另一行。
I have searched for this so more but i'm not able to get the proper answer. I have also tried the AutoSize
, Percent
, Absolute
properties of RowStyle
, even though it is not working.
我已经搜索了更多,但我无法得到正确的答案。我也尝试过AutoSize
, Percent
,Absolute
属性RowStyle
,即使它不起作用。
So what to do and how? How can I achieve this?
那么该怎么做以及如何做呢?我怎样才能做到这一点?
Ultimately, I want to do same like as DataGridView
of Windows C#.
最终,我想做与DataGridView
Windows C#一样的事情。
Thanks in advance....
提前致谢....
I'm working on WinForms...the sample code is here..
我正在研究 WinForms...示例代码在这里...
int cnt = tableLayout.RowCount = myDataTable.Rows.Count;
tableLayout.Size = new System.Drawing.Size(555, 200);
for (int i = 1; i <= cnt; i++)
{
Label lblSrNo = new Label();
lblSrNo.Text = i.ToString();
TextBox txt = new TextBox();
txt.Text = "";
txt.Size = new System.Drawing.Size(69, 20);
tableLayout.Controls.Add(lblSrNo, 0, i - 1);
tableLayout.Controls.Add(txt, 1, i - 1);
}
tableLayout.RowStyles.Clear();
foreach (RowStyle rs in tableLayout.RowStyles)
tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
The label and textboxes are working fine for 4-5 #of rows but whenever the #of row(in this case, variable cnt in for loop) increases, the rows are overwriting each other that is one control overwrite to another...I had drag-drop the TableLayoutPanel control and created just one row and 2 columns manually.
标签和文本框对于 4-5 #of 行工作正常,但是每当 #of 行(在这种情况下,for 循环中的变量 cnt)增加时,行会相互覆盖,即一个控件覆盖另一个控件......我拖放了 TableLayoutPanel 控件并手动创建了一行和 2 列。
So please tell me how to do it.
所以请告诉我怎么做。
回答by NL3294
I'm still new to tableLayoutPanels myself, but I noticed that at the bottom of your code, you're Clearing all the rowstyles from the collection, then you're trying to iterate through them in your foreach loop.
我自己还是 tableLayoutPanels 的新手,但我注意到在代码的底部,您正在清除集合中的所有行样式,然后您尝试在 foreach 循环中遍历它们。
You did this:
你这样做了:
tableLayout.RowStyles.Clear(); //now you have zero rowstyles
foreach (RowStyle rs in tableLayout.RowStyles) //this will never execute
tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
Try this instead.
试试这个。
TableLayoutRowStyleCollection styles =
tableLayout.RowStyles;
foreach (RowStyle style in styles){
// Set the row height to 20 pixels.
style.SizeType = SizeType.Absolute;
style.Height = 20;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Edit: I just realized that adding N rows doesn't add N rowstyles that you can iterate through. I think what's happening is that you're adding N rows, but none of them have a rowstyles.
编辑:我刚刚意识到添加 N 行不会添加您可以迭代的 N 行样式。我认为发生的事情是您添加了 N 行,但它们都没有行样式。
I suppose you can Clear() the rowstyles, then just add N rowstyles similar to how you're already doing.
我想您可以 Clear() 行样式,然后只需添加 N 个类似于您已经在做的行样式。
回答by jo_Veera
There are 2 ways to increase the row height of table layout panel.
有两种方法可以增加表格布局面板的行高。
Look into the following link : https://social.msdn.microsoft.com/Forums/windows/en-US/d80db8e1-d6cc-48b8-957f-0f73263c6d4a/how-to-change-the-row-height-of-a-tablelayoutpanel-at-runtime?forum=winforms
It specifies by setting the YourTableLayoutPanel.RowStyles[index].Height int he code behind class.
The other way is to set the row height in the designer of your UI. Through UI, go into Rows properties of the panel, select the row and set the required height using percent or absolute
查看以下链接:https: //social.msdn.microsoft.com/Forums/windows/en-US/d80db8e1-d6cc-48b8-957f-0f73263c6d4a/how-to-change-the-row-height-of- a-tablelayoutpanel-at-runtime?forum=winforms
它通过设置 YourTableLayoutPanel.RowStyles[index].Height int 类背后的代码来指定。
另一种方法是在 UI 设计器中设置行高。通过 UI,进入面板的 Rows 属性,选择行并使用百分比或绝对值设置所需的高度