RE: Need a ListBox of controls

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi David,

I suggest that you draw the ListBox by yourself to get what you want. To do
this, set the DrawMode property of the ListBox to OwnerDrawFixed or
OwnerDrawVariable and then override the OnMeasureItem and OnDrawItem and
OnMouseClick methods.

The following is a sample:

public class MyListBox : ListBox
{
List<bool> list = new List<bool>();
int itemHeight = 15;
int checkboxWidth = 20;
int imageWidth = 10;
bool initialize = true;

public delegate void ItemEventHandler(int itemindex);
public delegate void ItemCheckEventHandler(int itemindex,
CheckState state);

public event ItemCheckEventHandler ItemCheckBoxClicked ;
public event ItemEventHandler ItemImageClicked;
public event ItemEventHandler ItemTextClicked;

protected void OnItemCheckBoxClicked(int index,CheckState state)
{
if (ItemCheckBoxClicked != null)
{
ItemCheckBoxClicked(index,state);
}
}
protected void OnItemImageClicked(int index)
{
if (ItemImageClicked != null)
{
ItemImageClicked(index);
}
}
protected void OnItemTextClicked(int index)
{
if (ItemImageClicked != null)
{
ItemTextClicked(index);
}
}
public MyListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
if (DesignMode) return;
if (initialize)
{
for (int i = 0; i < this.Items.Count; i++)
{
list.Add(false);
}
initialize = false;
}

CheckBoxState state;
if (list[e.Index])
{
state = CheckBoxState.CheckedNormal;
}
else
{
state = CheckBoxState.UncheckedNormal;
}
e.DrawBackground();
CheckBoxRenderer.DrawCheckBox(e.Graphics, new
Point(e.Bounds.Left + 1, e.Bounds.Top + 1), state);
e.Graphics.DrawImage(Properties.Resources.Image1, new
Point(e.Bounds.Left + checkboxWidth, e.Bounds.Top + 1));
using (Font f = new Font(this.Font, FontStyle.Underline))
{
e.Graphics.DrawString(this.Items[e.Index].ToString(), f,
Brushes.Blue, new PointF(e.Bounds.Left + checkboxWidth + imageWidth,
e.Bounds.Top));
}
base.OnDrawItem(e);
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
if (!DesignMode)
{
e.ItemHeight = itemHeight;
Size size =
TextRenderer.MeasureText(this.Items[e.Index].ToString(), this.Font);
e.ItemWidth = size.Width;
base.OnMeasureItem(e);
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
int clickedIndex = -1;
clickedIndex = this.IndexFromPoint(e.Location);
if (clickedIndex != -1)
{
Rectangle checkboxRect = new Rectangle(0, clickedIndex *
itemHeight, checkboxWidth, itemHeight);
Rectangle imageRect = new Rectangle(checkboxWidth,
clickedIndex * itemHeight, imageWidth, itemHeight);
Size size
=TextRenderer.MeasureText(this.Items[clickedIndex].ToString(),this.Font);
Rectangle textRect = new Rectangle(checkboxWidth +
imageWidth,clickedIndex *ItemHeight,size.Width,itemHeight);
if (checkboxRect.Contains(e.Location))
{
list[clickedIndex] = !list[clickedIndex];
this.Invalidate();
if (list[clickedIndex])
{
this.OnItemCheckBoxClicked(clickedIndex,
CheckState.Checked);
}
else
{
this.OnItemCheckBoxClicked(clickedIndex,
CheckState.Unchecked);
}
}
else if (imageRect.Contains(e.Location))
{
this.OnItemImageClicked(clickedIndex);
}
else if (textRect.Contains(e.Location))
{
this.OnItemTextClicked(clickedIndex);
}
}
base.OnMouseClick(e);
}
}

Use the derived ListBox instead of the standard ListBox on your form and
subscribe the ItemCheckBoxClicked, ItemImageClicked and ItemTextClicked
events of the derived ListBox control.

As you can see, in the above solution I don't actually add a CheckBox or a
LinkLabel into each item of the ListBox, instead, I only draw them by
myself to mimc the appearance and behavior.

In fact, it's more easy to get what you want in WPF. You can add any UI
element into the items of a System.Windows.Controls.ListBox control
conveniently using a DataTemplate.

In your scenario, you can create a custom WPF control derived from the
System.Windows.Controls.ListBox class and then use the custom control on a
form in a WinForm application through the
System.Windows.Forms.Integration.ElementHost class.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

.



Relevant Pages

  • Re: Need a ListBox of controls
    ... I suggest that you draw the ListBox by yourself to get what you want. ... protected override void OnMeasureItem ... events of the derived ListBox control. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Custom ListBox
    ... Hi David,I suggest that you draw the ListBox by yourself to get what you want. ... events of the derived ListBox control. ... it's more easy to get what you want in WPF. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • RE: Bind data in a ListBox with multiple selectionmode
    ... ListBox doesn't support direct databinding of the selected values. ... ** in the ListBox 's PreRender event, you retrieve the string ... Bind data in a ListBox with multiple selectionmode ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Bind data in a ListBox with multiple selectionmode
    ... ListBox doesn't support direct databinding of the selected values. ... ** in the ListBox 's PreRender event, you retrieve the string ... Bind data in a ListBox with multiple selectionmode ...
    (microsoft.public.dotnet.framework.aspnet)
  • Custom Control Interfering with ViewState
    ... protected override void CreateChildControls() ... client then selectes an item in ListBox1 causing a post back, ... orginal items are retrieved from ViewState. ...
    (microsoft.public.dotnet.framework.aspnet)