在 C# 中调整组合框下拉宽度
我有这段代码可以调整组合框下拉列表的宽度:
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;
}
它工作得很好,除了它将下拉列表的宽度向右扩展,而我更喜欢它向左扩展,因为组合框位于我表格的右侧。如果您有任何想法或建议,我们将不胜感激。谢谢。
I have this code that adjusts the width of a comboBox drop-down:
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;
}
It works great, except it expands the width of the drop-down to the right, whereas I would prefer it to expand to the left because the comboBox is located on the right side of my form. Any thoughts or suggestions you may have would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,所以 .Anchor 没有像我预期的那样工作,所以这里有一个全新的答案,它确实有效,但我觉得这是一种黑客,(但也许这是一种完全合理的管理方式):
此代码拉它沿 x 轴向后移动 10 个像素,然后将 ComboBox1 扩展 10 个像素。
这对我来说非常顺利。这对你有用吗?
Ok, so .Anchor didn't work like I expected it to, so here's a completely new answer which does work, but I feel is kind of a hack, (but maybe it's a perfectly reasonable way to manage it):
This code pulls it back along the x-axis by 10 pixels, and then expands ComboBox1 by 10 pixels.
This works very smoothly for me. Does this work for you?
我在 CodeProject 上写了一篇关于如何破解组合框以使其具有滚动条以水平滚动的文章。请参阅此处的文章。
I wrote up an article on CodeProject on how to hack the combo-box to give it a scroll bar to scroll horizontally. See here for the article.
您可能想考虑将控件放置在容器中。例如,创建一个 FlowLayoutPanel,其 FlowDirection 属性为 RightToLeft。将组合框放置在新面板中。此方法的好处之一是您可以通过任何方式更改尺寸,并且控件/容器将按预期运行。
You might want to look at placing the control within a container. For instance, create a FlowLayoutPanel with its FlowDirection property to RightToLeft. Place the ComboBox within the new panel. One benefit of this method is you may change the dimensions by any means and the control/container will behave as expected.
经过大量搜索,这似乎是微软尚未解决的长期问题(大惊喜)。我决定重新安排我的布局,以便在有时间时更好地适应这个缺乏的功能,但现在,我只能忍受它。谢谢大家的意见。
After much searching, it appears that this is actually along-standing problem that Microsoft has yet to address (big surprise). I've decided to re-arrange my layout to better-accomodate this feature-lack when I get the time, but for now, I'm just going to live with it. Thanks, everyone, for your input.