WindowsForm项目开发中,Listbox控件item数据项,只能一条数据显示在一行,有的时候内容很长,体验就非常之差。简直要歇菜了。哈哈。。。不开玩笑了。下面讲下怎么实现吧!
新建一个Winform项目,命名为ListAutoline,拖一个按钮(用来填充数据用的,触发吧),一个ListBox控件 . 如图;
弄完之后呢 将listbox属性项: 把这两个属性设置成如图所示;Enabled可设置可不设置;看个人了; DramMode主要是控制列表框绘制,系统用户绘制每项;可以理解为启用人工绘制的意思啦;
弄玩上面之后,咱们来看一下怎么绘制这个玩意吧。
先手动做几条数据吧 搞个Datatable去装数据;代码如下
Font nfont;
Color colorn;
int leftMargines;
int topMargines;
int distance;
Font tfont;
DataTable ListData;
///
/// 初始化数据
///
private void FillDataTable()
{
ListData = new DataTable("List");
//ListData.Columns.Add("Caption", System.Type.GetType("System.String"));
ListData.Columns.Add("Text", System.Type.GetType("System.String"));
ListData.Rows.Add(
new object[]
{
//"datagram ",
"工站:过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务。"
});
ListData.Rows.Add(
new object[]
{
//"digital signature ",
"过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务请检查连接P上传"
});
ListData.Rows.Add(
new object[]
{
//"Digital Signature Standard ",
"过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务 "
});
ListData.Rows.Add(
new object[]
{
// "Distinguished Encoding Rules ",
"过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务28;CellId[3]:FXP0290DGWFE90228;"
});
ListData.Rows.Add(
new object[]
{
//"electronic codebook ",
"P过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务,请检查 "
});
}
然后在Button事件下面这样写; 就是调用那个FillDataTable();再加个ID啥的;
private void button1_Click(object sender, EventArgs e)
{
this.FillDataTable();
this.listBox1.Enabled = false;
this.listBox1.Items.Clear();
for (int i = 0; i <= ListData.Rows.Count - 1; i++)
{
this.listBox1.Items.Add(i);
//this.listBox1.Items.Add(i + ListData.Rows[i]["Text"].ToString());
}
this.listBox1.Enabled = true;
}
做完以上操作之后,选中Listbox 查看事件;
新增两个事件; 还有
新建这两个事件;开始重新绘制Listbox;
代码如下:
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
Graphics gfx = e.Graphics;
int number = e.Index + 1;
//string xcaption = ListData.Rows[e.Index]["Caption"].ToString();
string dtext = ListData.Rows[e.Index]["Text"].ToString();
SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);
// SizeF f11 = gfx.MeasureString(xcaption, nfont);
Rectangle rect = new Rectangle(leftMargines + (int)f1.Width + 8, topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, e.ItemHeight);
StringFormat stf = new StringFormat();
stf.FormatFlags = StringFormatFlags.FitBlackBox;
SizeF f2 = gfx.MeasureString(dtext, tfont, rect.Width, stf);
int Temp = e.ItemWidth;
//if (f2.Width < (leftMargines + f1.Width + 8 + f11.Width))
// Temp = leftMargines + (int)f1.Width + 8 + (int)f11.Width + 4;
if (f2.Width < (leftMargines + f1.Width + 8))
Temp = leftMargines + (int)f1.Width + 8 + 4;//行间距
e.ItemHeight = topMargines + (int)f1.Height + distance + (int)f2.Height + 8 + 8;//上下高度
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
int number = e.Index + 1;
//string xcaption = ListData.Rows[e.Index]["Caption"].ToString();
string dtext = ListData.Rows[e.Index]["Text"].ToString();
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
Graphics gfx = e.Graphics;
Pen pen = new Pen(Brushes.SteelBlue, 0.4f);
gfx.DrawRectangle(pen, e.Bounds.X + 4, e.Bounds.Y + 4, e.Bounds.Width - 8, e.Bounds.Height - 8);
gfx.FillRectangle(Brushes.LightSteelBlue, e.Bounds.X + 5, e.Bounds.Y + 5, e.Bounds.Width - 9, e.Bounds.Height - 9);
gfx.DrawString(number.ToString() + ".", nfont, Brushes.Black, e.Bounds.X + leftMargines, e.Bounds.Y + topMargines);
SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);
//gfx.DrawString(xcaption, nfont, Brushes.SteelBlue, e.Bounds.X + leftMargines + f1.Width + 8, e.Bounds.Y + topMargines);
//SizeF f11 = gfx.MeasureString(xcaption, nfont);
Rectangle rect = new Rectangle(e.Bounds.X + leftMargines + (int)f1.Width + 8, e.Bounds.Y + topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, this.ClientSize.Height);
StringFormat stf = new StringFormat();
stf.FormatFlags = StringFormatFlags.FitBlackBox;
gfx.DrawString(dtext, tfont, Brushes.Black, rect, stf);
}
else
{
Graphics gfx = e.Graphics;
gfx.FillRectangle(Brushes.White, e.Bounds.X + 4, e.Bounds.Y + 4, e.Bounds.Width - 7, e.Bounds.Height - 7);
gfx.FillRectangle(Brushes.SteelBlue, e.Bounds.X + 4, e.Bounds.Y + e.Bounds.Height - 8, e.Bounds.Width - 8, 1);
Pen pen = new Pen(Brushes.SteelBlue, 0.4f);
gfx.DrawString(number.ToString() + ".", nfont, Brushes.Black, e.Bounds.X + leftMargines, e.Bounds.Y + topMargines);
SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);
//gfx.DrawString(xcaption, nfont, Brushes.SteelBlue, e.Bounds.X + leftMargines + f1.Width + 8, e.Bounds.Y + topMargines);
//SizeF f11 = gfx.MeasureString(xcaption, nfont);
Rectangle rect = new Rectangle(e.Bounds.X + leftMargines + (int)f1.Width + 8, e.Bounds.Y + topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, this.ClientSize.Height);
StringFormat stf = new StringFormat();
stf.FormatFlags = StringFormatFlags.FitBlackBox;
gfx.DrawString(dtext, tfont, Brushes.Black, rect, stf);
}
e.DrawFocusRectangle();
}
然后在窗体Load事件中写入以下代码:
private void ListAutoLine_Load(object sender, EventArgs e)
{
nfont = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
tfont = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
colorn = Color.Black;
leftMargines = 8;
topMargines = 10;
distance = 12;
}
新建窗体SizeChange事件:
private void ListAutoLine_SizeChanged(object sender, EventArgs e)
{
listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
}
好了,以上代码就实现了ListBox自动换了;写个事件测试测试;
private void listBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("当前选中-[" + listBox1.SelectedItem.ToString().Trim() + "]-项");
MessageBox.Show(getListText(listBox1.SelectedItem.ToString().Trim()));
}
基本上是没有什么问题的啦;
效果图为:
就这样子了;
以上就是本期全部内容了;如有不当之处,还请指正,感谢;欢迎留言讨论;