[英]C#, tabControl, different window size of each tab
I have a tabControl with two tabs. 我有一个带有两个选项卡的tabControl。 I need to change window size when I click on tab2, by width of dataGrid which is in second tab.
当我单击tab2时,需要通过第二个选项卡中的dataGrid的宽度来更改窗口大小。 I have the following code:
我有以下代码:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"])
{
//FormValue.form2.Width = magical_function_return_dataGridwidth();
txtNumber.Select();
}
else
{
FormValue.form2.Width = 580;
}
}
Or is there some more elegant way. 还是有一些更优雅的方法。 I was playing with properties of dataGrid ... auto resize windows, but I found nothing ...
我在玩dataGrid的属性...自动调整窗口大小,但是什么也没发现...
In dataGrid I use AutoSizeColumnMode: AllCells, because I want resize cell size by text string length in it and I want to achieve the same behaviour for window. 在dataGrid中,我使用AutoSizeColumnMode:AllCells,因为我想通过其中的文本字符串长度来调整单元格大小,并且希望实现与窗口相同的行为。
Thank you. 谢谢。
I had a go at implementing your magical_function_return_dataGridwidth
function. 我可以执行您的
magical_function_return_dataGridwidth
函数。
The width you are looking for comes from the RowHeadersWidth
, the DataGridViewColumn
objects, the Scrollbar (if it's visible) and the general padding / borders etc. 您要查找的宽度来自
RowHeadersWidth
, DataGridViewColumn
对象,滚动条(如果可见)和常规的填充/边框等。
I used a nasty constant of 50 pixels to deal with all of the bits that I couldn't work out, but for me it works correctly and deals with the presence of the vertical scrollbar. 我使用了一个讨厌的常数(50像素)来处理所有我无法解决的位,但是对我而言,它可以正常工作并处理垂直滚动条的存在。
It's not an ideal solution by any means, but hopefully it will help you. 无论如何,这都不是理想的解决方案,但希望它将对您有所帮助。
private int magical_function_return_dataGridwidth(DataGridView dgv)
{
int totalWidth = dgv.RowHeadersWidth + 50;
foreach (var scroll in dgv.Controls.OfType<VScrollBar>())
{
if(!scroll.Visible)
totalWidth -= System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
}
foreach (DataGridViewColumn col in dgv.Columns)
totalWidth += col.Width;
return totalWidth;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.