如何让组合框的宽度自动适应
2010-05-19 21:42
381 查看
在Windows Forms应用程序开发过程中,我们经常会用到组合框(ComboBox),但它的那个下拉列表宽度是固定的,那么如何让它支持自动适应的宽度呢。下面是一个小例子
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Load += new EventHandler(Form1_Load); } void Form1_Load(object sender, EventArgs e) { var dir = new DirectoryInfo("e:\\temp"); var query = from f in dir.GetFiles() select new { FullName = f.FullName, Name = f.Name }; comboBox1.DataSource = query.ToArray(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "FullName"; using (Graphics g = comboBox1.CreateGraphics()) { int widestWidth = comboBox1.DropDownWidth; foreach (var item in query.Select(v=>v.Name)) { int current = (int)g.MeasureString(item, comboBox1.Font).Width; if (current > widestWidth) widestWidth = current; } comboBox1.DropDownWidth = widestWidth; } } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- 如何让组合框的宽度自动适应
- 如何让div自动适应内容宽度,并且居中
- iframe的用法,以及如何让iframe自动适应父及的宽度和高度
- Iframe自动适应宽度和高度(both IE and Firefox )
- 一个常见问题的解决——Ext grid的宽度高度如何自适应
- 在WEB页面中根据分辨率自动调整内容宽度,以适应表格宽度
- 探究客户端浏览器分辨率的自适应问题(如何使网站脚本自动适应客户端分辨率)(3)
- HTML如何让IMG自动适应DIV容器大小
- 如何设置table,使单元格内容无论多少,只显示一行,超过单元格宽度的部分自动隐藏,当鼠标放到单元格上,内容自动显示完整?
- easyui combobox 动态加载数据并回显选中 ,设置宽度和高度自动适应
- 自动适应输入内容宽度的TextBox控件
- NPOI导出Excel - 自动适应中文宽度(帮助类下载)
- C# ListView控件的间隔色和自动适应宽度
- 父框架的宽度和高度自动适应其所包含页面的宽度和高度
- iOS AutoLayout自动布局中级开发教程(4)-label文字自动适应大小,宽度(新)
- 如何使IFrame的长宽与内容自动适应大小
- 如何使IFrame的长宽与内容自动适应大小
- 如何使IFrame的长宽与内容自动适应大小
- iOS UILabel根据字符串长度自动适应宽度和高度
- 子级必须浮动,父级如何自动适应高度