i want to design a Listbox that contains TextBox as Item
when we call listbox1.Item.Add(new TextBox());
how can i do this?
v-lli wrote:
Hi David,I suggest that you draw the ListBox by yourself to get what you want.
30-Jun-08
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 : ListBo
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
els
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:
***@microsoft.com.
==================================================
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.
Previous Posts In This Thread:
On Sunday, June 29, 2008 6:06 PM
David Thielen wrote:
Need a ListBox of controls
Hi;
I need to create a ListBox where each line in the list box has:
1) optionally a check box - and I need an event when it is (un)checked
2) an optional spacing - the width of the bitmap listed in (3)
3) a bitmap - and I need an event when it is clicked (to pop up a
menu)
4) a LinkLabel
How best can I do this?
thanks - dave
***@at-at-***@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com
Cubicle Wars - http://www.windwardreports.com/film.htm
On Monday, June 30, 2008 6:54 AM
v-lli wrote:
Hi David,I suggest that you draw the ListBox by yourself to get what you want.
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
els
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)
els
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 Li
Microsoft Online Community Suppor
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:
***@microsoft.com
=================================================
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.
On Monday, June 30, 2008 12:21 PM
David Thielen wrote:
Hi;The owner draw approach is a pain because we then have to write thecode to
Hi;
The owner draw approach is a pain because we then have to write the
code to emulate all the functionality in the LinkLabel control as well
as determining which part a click is on.
Is there an example of how to do the in WPF? And will WPF work in an
Office AddIn? Also, is WPF supported on Win95/98/ME/XP?
thanks - dave
On Mon, 30 Jun 2008 10:54:33 GMT, v-***@online.microsoft.com (Linda
Liu[MSFT]) wrote:
***@at-at-***@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com
Cubicle Wars - http://www.windwardreports.com/film.htm
On Tuesday, July 01, 2008 6:51 AM
v-lli wrote:
Hi David,Thank you for your prompt response!
Hi David,
Thank you for your prompt response!
No, there isn't an existing WPF example that meets your requirements.
As I have mentioned in my first reply, the WPF workaround is to create a
custom control/user control that contains a ListBox and define a
DataTemplate to add any element you want to each item of the ListBox.
Handle the events of the CheckBox and LinkLabel and fire events from within
the custom control/user control. Then use the ElementHost control to host
the WPF custom control/user control in a WinForm. Subscribe and handle the
events of the custom control/user control in the WinForm.
The following documents may help you to understand the conception mentioned
in the above workaround:
'Data Templating Overview'
http://msdn.microsoft.com/en-us/library/ms742521.aspx
'How to: Find DataTemplate-Generated Elements'
http://msdn.microsoft.com/en-us/library/bb613579.aspx
'Creating Custom WPF Controls'
http://www.codeguru.com/cpp/data/data-misc/xml/article.php/c12521__1/
'Integrate WPF UserControls in WinForms'
http://www.codeproject.com/KB/WPF/WPF_UserControls.aspx
Only if the WPF control can be used on a WinForm and the WinForm work in an
Office addin, the WPF control will work in the Office addin.
WPF is supported from .NET Framework 3.0. The supported Operation Systems
for .NET Framework 3.0 is Longhorn (Windows Code Name) ; Windows Server
2003 Service Pack 1; Windows Vista; Windows XP Service Pack 2.
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:
***@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.
On Tuesday, July 01, 2008 6:29 PM
David Thielen wrote:
Hi;Thank you for the links but this looks like major over-kill.
Hi;
Thank you for the links but this looks like major over-kill. I think
what I'll do is just create a scrollable panel and place my controls
on that. Plus there is no data associated with the control so I'd have
to fake that part too.
thanks - dave
On Tue, 01 Jul 2008 10:51:25 GMT, v-***@online.microsoft.com (Linda
Liu[MSFT]) wrote:
***@at-at-***@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com
Cubicle Wars - http://www.windwardreports.com/film.htm
On Thursday, July 03, 2008 10:16 PM
v-lli wrote:
Hi David,Thank you for your reply!
Hi David,
Thank you for your reply!
One advantage of WPF is that we can put any .NET object as the content of a
content control. However, the disadvantage is that it'll take a while to
learn WPF. Anyway, I hope the WPF solution is another direction for this
issue.
controls on that.
Yes, it's a convenient workaround.
If you have any other question in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!
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:
***@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.
Submitted via EggHeadCafe - Software Developer Portal of Choice
Join Lists with LINQ - SharePoint 2010
http://www.eggheadcafe.com/tutorials/aspnet/b0c1cd0d-fe82-444e-a16e-7d3fb7d38eca/join-lists-with-linq--sh.aspx