以窗口形式增加CheckedListBox中显示的每个项目的高度 [英] increase the height of each item displayed in CheckedListBox in window form
本文介绍了以窗口形式增加CheckedListBox中显示的每个项目的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i有windows格式的checkedlistbox从数据库中获取数据
i want如果每个checkedlistbox项都增加行高。
任何想法。
解决方案
这个有效:
int height = 0 ,index = 0 ;
private void button1_Click( object sender,EventArgs e)
{
checkedListBox1.Items.Add( new跨度>);
height + = checkedListBox1.GetItemHeight(index); // 添加添加到checklistbox的项目高度
if (checkedListBox1.Height< height)
{
checkedListBox1.Size = new 大小(checkedListBox1.Width,checkedListBox1.Height + checkedListBox1.GetItemHeight( 0 ));
}
index ++;
}
这适用于VS2013网络FrameWork4.5
认证声明和不断上课
用法将其余代码放在Form_Load中,如示例代码所示。
< pre lang =vb> 私有 声明 功能 SendMessageByNum Lib user32 < span class =code-keyword>别名 SendMessageA _
( ByVal hwnd 作为 IntPtr , ByVal wMsg 作为 UInt32 , ByVal wParam 作为IntPtr , ByVal lParam As IntPtr )作为 IntPtr = 我 .Font.Height + 4
私有 Const lB_SETITEMHEIGHT As 整数 =& H1A0
私有 Sub Form_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles 我 .Load
Dim ItemHeight 作为 整数
SendMessageByNum(CheckedListBoxControl.Handle,lB_SETITEMHEIGHT, CType ( 0 , IntPtr ),< span class =code-keyword> CType (ItemHeight, IntPtr ))
结束 Sub
设置 DrawMode 到 OwnerDrawVariable 并为 DrawItem 和 OnMeasureItem 添加处理程序事件
ListBox1.DrawMode = DrawMode.OwnerDrawVariable;
ListBox1.DrawItem + = new DrawItemEventHandler(ListBox1_DrawItem);
受保护 覆盖 void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = xxx;
此 .DropDownHeight = xxx * 5 ; // 如果您使用边框
}
private void ListBox1_DrawItem( object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
// 设置项目的大小并绘制它
//
e.Graphics.DrawString(ListBox1.Items [e.Index]。 ToString(),
e.Font,myBrush,e.Bounds,StringFormat.GenericDefault);
}
您会找到很多样品。
hi all,
i have a checkedlistbox in windows form which gets data from database
i want to increse the row height if each item of checkedlistbox.
any idea.
解决方案
This one works:
int height =0, index =0; private void button1_Click(object sender, EventArgs e) { checkedListBox1.Items.Add("new"); height += checkedListBox1.GetItemHeight(index); //Add height of item added to checklistbox if(checkedListBox1.Height< height) { checkedListBox1.Size = new Size(checkedListBox1.Width, checkedListBox1.Height + checkedListBox1.GetItemHeight(0)); } index++; }
This works in VS2013 net FrameWork4.5
Put declare and constant at top of class
Usage put rest of code in Form_Load as in example code.
Private Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As IntPtr, ByVal wMsg As UInt32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr Private Const lB_SETITEMHEIGHT As Integer = &H1A0 Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ItemHeight As Integer = Me.Font.Height + 4 SendMessageByNum(CheckedListBoxControl.Handle, lB_SETITEMHEIGHT, CType(0, IntPtr), CType(ItemHeight, IntPtr)) End Sub
Set the DrawMode to OwnerDrawVariable and add a handler event for DrawItem and OnMeasureItem
ListBox1.DrawMode = DrawMode.OwnerDrawVariable; ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
protected override void OnMeasureItem(MeasureItemEventArgs e) { e.ItemHeight = xxx; this.DropDownHeight = xxx * 5; // if you use borders } private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); // set the size of the item and draw it // e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); }
You'll find a lot of samples.
这篇关于以窗口形式增加CheckedListBox中显示的每个项目的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文