在 C# 中调整组合框下拉宽度

发布于 2024-09-02 17:15:51 字数 852 浏览 4 评论 0原文

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

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

も星光 2024-09-09 17:15:51

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

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

此代码拉它沿 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):

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

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?

樱&纷飞 2024-09-09 17:15:51

我在 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.

摘星┃星的人 2024-09-09 17:15:51

您可能想考虑将控件放置在容器中。例如,创建一个 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.

划一舟意中人 2024-09-09 17:15:51

经过大量搜索,这似乎是微软尚未解决的长期问题(大惊喜)。我决定重新安排我的布局,以便在有时间时更好地适应这个缺乏的功能,但现在,我只能忍受它。谢谢大家的意见。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文