c#动态设置combobox控件下拉项宽度以实现下拉项文字可以完全显示。
2009-01-07 17:15
1096 查看
c#动态设置combobox控件下拉项宽度以实现下拉项文字可以完全显示。
这个几乎是来自这里的啊: http://blog.csdn.net/ccy3253/archive/2008/01/26/2067304.aspx
0.控件类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class CtrlComboBox :ComboBox
{
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
AdjustComboBoxDropDownListWidth(); //调整comboBox的下拉列表的大小
}
private void AdjustComboBoxDropDownListWidth()
{
Graphics g = null;
Font font = null;
try
{
int width = this.Width;
g = this.CreateGraphics();
font = this.Font;
//checks if a scrollbar will be displayed.
//If yes, then get its width to adjust the size of the drop down list.
int vertScrollBarWidth =
(this.Items.Count > this.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (object s in this.Items) //Loop through list items and check size of each items.
{
if (s != null)
{
//newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
newWidth = (int)g.MeasureString(((ComboBoxData)s).StrText, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
width = newWidth; //set the width of the drop down list to the width of the largest item.
}
}
this.DropDownWidth = width;
}
catch
{ }
finally
{
if (g != null)
g.Dispose();
}
}
}
}
1.数据源类
public class ComboBoxData
{
private string _StrValue;
private string _StrText;
public ComboBoxData(string StrValue, String StrText)
{
_StrText = StrText;
_StrValue = StrValue;
}
public string StrValue
{
get
{
return _StrValue;
}
set
{
_StrValue = value;
}
}
public string StrText
{
get
{
return _StrText;
}
set
{
_StrText = value;
}
}
}
2.form 里使用它。
ArrayList al = new ArrayList();
for (int i = 0; i < 9; i++)
{
al.Add( new ComboBoxData("aaaaaaaaaaaaaaaaaaa" + i.ToString(), "aaaaaaaaaaaaaaaaaaa" + i.ToString()));
}
al.Add(new ComboBoxData("只要在我们向combox添加完所有项后,调用一下,就可以调整comboBox下拉列表的宽度了", "只要在我们向combox添
加完所有项后,调用一下,就可以调整comboBox下拉列表的宽度了"));
ctrlComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
ctrlComboBox1.DisplayMember = "StrText";
ctrlComboBox1.ValueMember = "StrValue";
ctrlComboBox1.DataSource = al;
如果想实现在选择了某项后,combobox以提示的方式来查看选择项的完全text,那要加一个toolTip
private void comboBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.toolTip1.SetToolTip(this.comboBox1, this.comboBox1.Text);
}
这个几乎是来自这里的啊: http://blog.csdn.net/ccy3253/archive/2008/01/26/2067304.aspx
0.控件类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class CtrlComboBox :ComboBox
{
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
AdjustComboBoxDropDownListWidth(); //调整comboBox的下拉列表的大小
}
private void AdjustComboBoxDropDownListWidth()
{
Graphics g = null;
Font font = null;
try
{
int width = this.Width;
g = this.CreateGraphics();
font = this.Font;
//checks if a scrollbar will be displayed.
//If yes, then get its width to adjust the size of the drop down list.
int vertScrollBarWidth =
(this.Items.Count > this.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (object s in this.Items) //Loop through list items and check size of each items.
{
if (s != null)
{
//newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
newWidth = (int)g.MeasureString(((ComboBoxData)s).StrText, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
width = newWidth; //set the width of the drop down list to the width of the largest item.
}
}
this.DropDownWidth = width;
}
catch
{ }
finally
{
if (g != null)
g.Dispose();
}
}
}
}
1.数据源类
public class ComboBoxData
{
private string _StrValue;
private string _StrText;
public ComboBoxData(string StrValue, String StrText)
{
_StrText = StrText;
_StrValue = StrValue;
}
public string StrValue
{
get
{
return _StrValue;
}
set
{
_StrValue = value;
}
}
public string StrText
{
get
{
return _StrText;
}
set
{
_StrText = value;
}
}
}
2.form 里使用它。
ArrayList al = new ArrayList();
for (int i = 0; i < 9; i++)
{
al.Add( new ComboBoxData("aaaaaaaaaaaaaaaaaaa" + i.ToString(), "aaaaaaaaaaaaaaaaaaa" + i.ToString()));
}
al.Add(new ComboBoxData("只要在我们向combox添加完所有项后,调用一下,就可以调整comboBox下拉列表的宽度了", "只要在我们向combox添
加完所有项后,调用一下,就可以调整comboBox下拉列表的宽度了"));
ctrlComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
ctrlComboBox1.DisplayMember = "StrText";
ctrlComboBox1.ValueMember = "StrValue";
ctrlComboBox1.DataSource = al;
如果想实现在选择了某项后,combobox以提示的方式来查看选择项的完全text,那要加一个toolTip
private void comboBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.toolTip1.SetToolTip(this.comboBox1, this.comboBox1.Text);
}
相关文章推荐
- winform窗体中 ComboBox 控件的DropDownWidth属性根据显示的文本设置下拉列表的显示宽度
- Android控件TextView中ellipsize属性(设置当文字长度超过textview宽度时的显示方式)
- C# 实现完整功能的截图控件(1)-实现绘图工具栏控件 控件实现了截图、绘制矩形、圆形、箭头、线条、文字,还需要可以撤销绘制步骤、保存图形等功能,基本实现了跟QQ2008截图一样的功能,还可以更
- VB.Net自己写的一个控件:ComboBox下拉列表中显示多列数据(可以绑定数据表)
- 设置ComboBox控件的下拉栏的宽度.
- 使用XTablayout实现横向滑动菜单,可以设置文字下面导航栏的宽度
- 解决WinForm中ComboBox控件的“设置"DataSourse”属性后无法修改项集合”以及两个不相关联的ComboxBox控件实现数据列表显示不可实现的问题
- Android自定义控件View(实现控件的动画效果,自定义类画简略时钟,TextView中文字逐一显示,动态设置progress的圆环/扇形ProgressBar等)
- html使用display:inline-block实现标签右对齐,值左对齐效果。和设置div宽度,并居中显示。嵌套div的里层div文字居中显示
- 如何动态设置ExtJS Form控件文字显示的颜色
- 转:控制ComboBox下拉框的下拉部分宽度,使内容能够显示完全
- C#实现ComboBox控件显示出多个数据源属性的方法
- easysize的使用方法 (实现控件大小位置随界面变化而有相应的变化,这个变化可以自己设置)
- 微信小程序 实现动态显示和隐藏某个控件
- C# 批量设置ComboBox控件的当前选中项的一种方法
- 如何实现桌面App图标可以动态显示消息数(类似手机上的QQ图标)?
- ComboBox有下拉项,可是无法下拉,而用键盘上的上下键和滚轮都可以选择该ComboBox的items.
- 点击HeadView 实现对cell动态下拉一个TableView,实现对SectionHeader悬停进行设置
- 用css实现文字超出指定宽度显示省略号(...)
- XtraGrid控件,实现在界面中动态修改列显示,列名列宽