C#-如何获得组合框的实际高度
我知道组合框。高度不能轻易设置。可以使用C#-如何获得组合框的实际高度,c#,winforms,combobox,size,C#,Winforms,Combobox,Size,我知道组合框。高度不能轻易设置。可以使用字体更改。但我需要知道它的最终高度。在显示窗口和控件之前,它不会更新 我怎么计算呢?当我运行此按钮时,按钮不在下面,而是在组合框后面: // my forms must be disigned by code only (no designer is used) public class Form1: Form { public Form1() { ComboBox box = new ComboBox();
字体更改。但我需要知道它的最终高度。在显示窗口和控件之前,它不会更新
我怎么计算呢?当我运行此按钮时,按钮不在下面,而是在组合框后面:
// my forms must be disigned by code only (no designer is used)
public class Form1: Form
{
public Form1()
{
ComboBox box = new ComboBox();
box.Font = new Font("Comic Sans MS", 100, FontStyle.Regular);
Controls.Add(box);
Button button = new Button();
button.Text = "hello world";
button.SetBounds(box.Left, box.Bottom, 256, 32);
button.SetBounds(box.Left, box.Height, 256, 32); // doesn't work either
Controls.Add(button);
}
}
问题是在绘制组合框之前,ComboBox.Bottom
属性不会更新以补偿字体大小
解决方案是在窗体中动态添加控件。加载事件而不是构造函数:
private void MainForm_Load(object sender, EventArgs e)
{
ComboBox box = new ComboBox();
box.Font = new Font("Comic Sans MS", 100, FontStyle.Regular);
Controls.Add(box);
Button button = new Button();
button.Text = "hello world";
button.SetBounds(box.Left, box.Bottom, 256, 32);
Controls.Add(button);
}
它继承自控件,所以属性有什么问题吗?@Sayse我猜操作的问题是组合框弹出列表覆盖了表单上的其他控件Height
将只返回TextBox部件控件高度。@说它在窗口和组合框可见之前不会更新。我非常怀疑这是真的,在初始化组件后和在OnLoad
方法中应该可以。是的,这很有帮助。谢谢另外,组合框的调整大小
事件也起作用。