gpt4 book ai didi

c# - 在 C# 中调整 ComboBox DropDown 宽度

转载 作者:行者123 更新时间:2023-11-30 15:10:58 24 4
gpt4 key购买 nike

我有这段代码可以调整组合框下拉列表的宽度:

  private void comboBox_DropDown(object sender, EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;

foreach (string s in ((ComboBox)sender).Items)
{
newWidth = (int)g.MeasureString(s, font).Width
+ vertScrollBarWidth;

if (width < newWidth)
{
width = newWidth;
}
}

senderComboBox.DropDownWidth = width;
}

效果很好,只是它向右扩展了下拉菜单的宽度,而我更希望它向左扩展,因为组合框位于我表单的右侧。如果您有任何想法或建议,我们将不胜感激。谢谢。

最佳答案

好的,所以 .Anchor 没有像我预期的那样工作,所以这是一个全新的答案,它确实有效,但我觉得有点 hack,(但也许这是一种非常合理的管理方式):

int x = 10;           
comboBox1.Location = new Point(comboBox1.Location.X - x, comboBox1.Location.Y);
comboBox1.Width += x;

此代码将其沿 x 轴向后拉 10 个像素,然后将 ComboBox1 扩展 10 个像素。

这对我来说非常顺利。这对你有用吗?

关于c# - 在 C# 中调整 ComboBox DropDown 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2877702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com