5 c# combobox scrollbar winforms
我在 Windows 窗体应用程序中有一个组合框,显示来自 MySQL 的特定数据。我只是想知道如何将水平滚动条添加到我的组合框中,因为我的数据太长而无法显示?
如果使用 Windows Presentation Foundation (WPF):
ScrollViewer.HorizontalScrollBarVisibility 属性
获取或设置一个值,该值指示是否应显示水平 ScrollBar。
在此添加ScrollViewer.HorizontalScrollBarVisibility="Visible":
<ComboBox HorizontalAlignment="Left" Margin="60,44,0,0" VerticalAlignment="Top" Width="264" Height="72" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
Run Code Online (Sandbox Code Playgroud)
例如:
或者您可以导航到对象的属性并在此处选择:
如果使用 Windows 窗体 (WinForms):
如果下拉列表的长度是静态的,您只需将 设为DropDownWidth足够大的值即可显示列表的完整长度。
例如,不进行调整(文本被截断):
例如,经过调整(显示文字):
如果您需要动态设置宽度,请将以下代码放入DropDown事件处理程序中或将其设为私有函数/方法调用:
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;
Run Code Online (Sandbox Code Playgroud)
例如,使用动态宽度:
| 归档时间: |
|
| 查看次数: |
6513 次 |
| 最近记录: |