Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Sun, 29 Jun 2008 16:06:42 -0600,    group: microsoft.public.dotnet.framework.windowsforms.controls        back       


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

david@at-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
date: Sun, 29 Jun 2008 16:06:42 -0600   author:   David Thielen am

RE: Need a ListBox of controls   
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@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.
date: Mon, 30 Jun 2008 10:54:33 GMT   author:   (Linda Liu[MSFT])

Re: Need a ListBox of controls   
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-lliu@online.microsoft.com (Linda
Liu[MSFT]) wrote:

>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@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.


david@at-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
date: Mon, 30 Jun 2008 10:21:07 -0600   author:   David Thielen am

Re: Need a ListBox of controls   
Hi David,

Thank you for your prompt response!

> Is there an example of how to do the in WPF?

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

> And will WPF work in an Office AddIn?

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.

> Also, is WPF supported on Win95/98/ME/XP?

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: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Tue, 01 Jul 2008 10:51:25 GMT   author:   (Linda Liu[MSFT])

Re: Need a ListBox of controls   
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-lliu@online.microsoft.com (Linda
Liu[MSFT]) wrote:

>Hi David,
>
>Thank you for your prompt response!
>
>> Is there an example of how to do the in WPF?
>
>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
>
>> And will WPF work in an Office AddIn?
>
>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.
>
>> Also, is WPF supported on Win95/98/ME/XP?
>
>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: 
>msdnmg@microsoft.com.
>
>This posting is provided "AS IS" with no warranties, and confers no rights.


david@at-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
date: Tue, 01 Jul 2008 16:29:05 -0600   author:   David Thielen am

Re: Need a ListBox of controls   
Hi David,

Thank you for your reply!

> Thank you for the links but this looks like major over-kill. 

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.

> I think what I'll do is just create a scrollable panel and place my 
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: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Fri, 04 Jul 2008 02:16:01 GMT   author:   (Linda Liu[MSFT])

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us