Click here to Skip to main content
15,888,053 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted

This one works:

C#
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++;
        }
 
Share this answer
 
v2
Comments
suniltikli 1-Aug-11 14:51pm    
it will actuall increase the height of checkedlistbox control but i need to increase the height of item row of checkedlistbox
Praveen Kullu 1-Aug-11 14:58pm    
I don't get it. Please show me your code for this.
suniltikli 1-Aug-11 15:10pm    
for ex there r 3 items rows in my checkedlistbox as following : -
item row 1 = Country1
item row 2 = Country2
item row 3 = Country3
i want the first item row height should be 30, secound item row height should be 20 and 3rd item row height should be 15 in the checkedlistbox.
Praveen Kullu 1-Aug-11 15:29pm    
Sorry, can't help you. Perhaps creating a custom checkedlistbox would do it.
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.

VB
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
 
Share this answer
 
Set the DrawMode to OwnerDrawVariable and add a handler event for DrawItem and OnMeasureItem

C#
ListBox1.DrawMode = DrawMode.OwnerDrawVariable;
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);


C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900