TableLayoutPanel 大小调整
我可能没有使用正确的控件来实现我想要的功能。我正在用控件填充表格,并且希望每列自动调整大小以适应其中包含的控件。例如,一列文本框将比一列复选框宽。由于不同操作系统、不同 DPI、不同字体等的复杂性,我不想摆弄测量是否可以帮助它。表格可以水平扩展以适应控件,并带有滚动条。对于 TableLayoutPanel 或其他一些控件,这怎么可能?
谢谢。
编辑以添加代码:(
private void UpdateLocations()
{
tableLayoutPanel1.RowCount = CurrentSchedule.location.Length + 1;
tableLayoutPanel1.ColumnCount = 7;
int row = 1;
int timeWidth = TextRenderer.MeasureText("00:00:00x", tableLayoutPanel1.Font).Width;
Label lab = new Label();
lab.Text = "Location";
tableLayoutPanel1.Controls.Add(lab, 0, 0);
lab = new Label();
lab.Text = "Arrive";
tableLayoutPanel1.Controls.Add(lab, 1, 0);
lab = new Label();
lab.Text = "Depart";
tableLayoutPanel1.Controls.Add(lab, 2, 0);
lab = new Label();
lab.Text = "Pass?";
tableLayoutPanel1.Controls.Add(lab, 3, 0);
lab = new Label();
lab.Text = "Path";
tableLayoutPanel1.Controls.Add(lab, 4, 0);
lab = new Label();
lab.Text = "Plat";
tableLayoutPanel1.Controls.Add(lab, 5, 0);
lab = new Label();
lab.Text = "Line";
tableLayoutPanel1.Controls.Add(lab, 6, 0);
foreach (location loc in CurrentSchedule.location)
{
TextBox tb = new TextBox();
tb.Text = loc.locationID;
tableLayoutPanel1.Controls.Add(tb, 0, row);
tb = new TextBox();
tb.Text = loc.arrivalTime;
tb.Width = timeWidth;
tableLayoutPanel1.Controls.Add(tb, 1, row);
tb = new TextBox();
tb.Text = loc.departureTime;
tb.Width = timeWidth;
tableLayoutPanel1.Controls.Add(tb, 2, row);
CheckBox cb = new CheckBox();
cb.Checked = loc.passingTime;
tableLayoutPanel1.Controls.Add(cb, 3, row);
tb = new TextBox();
tb.Text = loc.pathCode;
tableLayoutPanel1.Controls.Add(tb, 4, row);
tb = new TextBox();
tb.Text = loc.platformCode;
tableLayoutPanel1.Controls.Add(tb, 5, row);
tb = new TextBox();
tb.Text = loc.lineCode;
tableLayoutPanel1.Controls.Add(tb, 6, row);
row++;
}
/*for (int idx = 0; idx < tableLayoutPanel1.RowCount; idx++)
{
tableLayoutPanel1.RowStyles[idx].SizeType = SizeType.AutoSize;
}
for (int idx = 0; idx < tableLayoutPanel1.ColumnCount; idx++)
{
tableLayoutPanel1.ColumnStyles[idx].SizeType = SizeType.AutoSize;
}*/
}
是的,它需要大量重构 - 我只是想让它先工作)
注释掉的位会导致越界异常,即使从逻辑上(对我来说)不应该。该范围似乎仅限于我在设计时设置的任何内容,而不是在运行时设置的值。
I may not be using the right control for what I want. I'm filling a table with controls and I want each column to automatically size to the controls contained within it. For example, a column of textboxes will be wider than a column of checkboxes. I don't want to fiddle with measuring if I can help it, due to the complexities of different OS, different DPI, different fonts, etc. The table can expand horizontally to fit the controls, with a scrollbar. How is this possible with a TableLayoutPanel - or some other control?
Thanks.
Edited to add code:
private void UpdateLocations()
{
tableLayoutPanel1.RowCount = CurrentSchedule.location.Length + 1;
tableLayoutPanel1.ColumnCount = 7;
int row = 1;
int timeWidth = TextRenderer.MeasureText("00:00:00x", tableLayoutPanel1.Font).Width;
Label lab = new Label();
lab.Text = "Location";
tableLayoutPanel1.Controls.Add(lab, 0, 0);
lab = new Label();
lab.Text = "Arrive";
tableLayoutPanel1.Controls.Add(lab, 1, 0);
lab = new Label();
lab.Text = "Depart";
tableLayoutPanel1.Controls.Add(lab, 2, 0);
lab = new Label();
lab.Text = "Pass?";
tableLayoutPanel1.Controls.Add(lab, 3, 0);
lab = new Label();
lab.Text = "Path";
tableLayoutPanel1.Controls.Add(lab, 4, 0);
lab = new Label();
lab.Text = "Plat";
tableLayoutPanel1.Controls.Add(lab, 5, 0);
lab = new Label();
lab.Text = "Line";
tableLayoutPanel1.Controls.Add(lab, 6, 0);
foreach (location loc in CurrentSchedule.location)
{
TextBox tb = new TextBox();
tb.Text = loc.locationID;
tableLayoutPanel1.Controls.Add(tb, 0, row);
tb = new TextBox();
tb.Text = loc.arrivalTime;
tb.Width = timeWidth;
tableLayoutPanel1.Controls.Add(tb, 1, row);
tb = new TextBox();
tb.Text = loc.departureTime;
tb.Width = timeWidth;
tableLayoutPanel1.Controls.Add(tb, 2, row);
CheckBox cb = new CheckBox();
cb.Checked = loc.passingTime;
tableLayoutPanel1.Controls.Add(cb, 3, row);
tb = new TextBox();
tb.Text = loc.pathCode;
tableLayoutPanel1.Controls.Add(tb, 4, row);
tb = new TextBox();
tb.Text = loc.platformCode;
tableLayoutPanel1.Controls.Add(tb, 5, row);
tb = new TextBox();
tb.Text = loc.lineCode;
tableLayoutPanel1.Controls.Add(tb, 6, row);
row++;
}
/*for (int idx = 0; idx < tableLayoutPanel1.RowCount; idx++)
{
tableLayoutPanel1.RowStyles[idx].SizeType = SizeType.AutoSize;
}
for (int idx = 0; idx < tableLayoutPanel1.ColumnCount; idx++)
{
tableLayoutPanel1.ColumnStyles[idx].SizeType = SizeType.AutoSize;
}*/
}
(Yes it needs heavy refactoring - I'm just trying to get it to work first)
The commented out bits cause out of bounds exceptions, even though logically (to me) it shouldn't. The range appears limited to whatever I set at design time, not at runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉,将列设置为“自动调整大小”有什么问题吗?这就是 TableLayoutPanel 的作用,调整列的大小以适合其中的控件。展开表格并使用滚动条需要将表格 Autosize 属性设置为 true,然后将 TableLayoutPanel 放置在另一个启用了滚动条的面板中。但是,除非我误解了您的要求,否则列的大小应该可以开箱即用。
只是为了确保,您要转到 columns 属性并将每列的 SizeType 设置为 AutoSize,对吧?不仅仅是表本身的 AutoSize 属性?
这是您想要的吗?
-邮政编码:
感谢您提供代码。我建议你使用设计师来做很多这样的事情。至少要设置列,将它们设置为自动调整大小,并添加标题标签。
您可能还想查看 Datagrid 控件并将其绑定到您的位置列表。
要使此方法发挥作用:
1)您的列看起来大小相同的原因是您使用的标题标签不会自动调整大小。它们都是 x 像素宽,这会拉伸列。执行此操作:
您还需要在 CheckBox 控件和添加为内容的任何其他标签上将 AutoSize 属性设置为 true。
2) 设置 RowCount 和 ColumnCount 不会影响 RowStyles 或 ColumnStyles 集合。您有 7 个列,但只有 2 个 ColumnStyle。尝试:
唯一需要注意的另一件事是某些控件在行中会错位(例如标签显得太高)。要解决此问题,请设置 Margin 属性,通常设置为 3,6,3,0,以便将它们与文本框和复选框等对齐。
Sorry, but what's wrong with having the columns set to Autosize? That's what TableLayoutPanel does, size columns to the fit the controls within it. Expanding the table and having a scrollbar would require you to set the tables Autosize property to true, then sit the TableLayoutPanel within another panel that has scrollbars enabled. But the column sizing should work out of the box if unless I'm misunderstanding your requirements.
Just to make sure, you are going to the columns property and setting each column's SizeType to AutoSize right? Not just the AutoSize property of the table itself?
is this what you want?
-Post code:
Thanks for the code. I'd suggest that you use designer to do a lot of this. At least to set up the columns, set them to autosize, and add the heading labels.
You also might want to check out the Datagrid control and bind that to your location list.
To get this method working though:
1) the reason your columns look the same size is because the heading labels you're using aren't autosizing. They're all x pixels wide and that's stretching the columns. Do this:
You'll also need to set the AutoSize property to true on the CheckBox control and any other labels you add as content.
2) Setting the RowCount and ColumnCount won't affect the RowStyles or ColumnStyles collection. You've got 7 Columns but only 2 ColumnStyles. Try:
Only other thing to look out for is that some controls will be misaligned in the rows (labels appear too high for example). To fix that set the Margin property, normally to 3,6,3,0 to align them with textboxes and checkboxes etc.
您需要处理 ControlAdded 事件,然后如果新控件的宽度大于列的宽度,则调整列的大小...
您说您不想摆弄测量,但如果您要调整大小,则有点必须。如果您小心复选框的 TextAlign 等,则不应使用字体等内容......
You'll need to handle the ControlAdded event, then resize the column if the new control's width is greater than the column's width...
You say you don't want to fiddle with measuring, but if you're resizing, you kind of have to. Fonts and such shouldn't come into it if you're careful about the TextAlign of your checkboxes etc...
您可以在另一个容器内使用多个 splitContainers控制板。但不要使用很多嵌套面板,您可能会遇到诸如此之类的调整大小/重绘问题。
如果您想使用 SplitContainer<,您可以找到很多示例/a>.
You could use multiple splitContainers one inside another's panel. But don't use many nested panels, you might get resize/redraw issues like this.
You can find many examples if you want to use SplitContainer.