Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / August 2006

Tip: Looking for answers? Try searching our database.

ListBox disabled items ??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bragadiru - 22 Aug 2006 14:29 GMT
Hi all,

I want to disable one or more items from a ListBox/ComboBox list. How can I
?
In FoxPro, just adding "/" in front of Item.Text is enough for disabling
that item.

Thanks for any advice
Linda Liu [MSFT] - 23 Aug 2006 04:12 GMT
Hi Bragadiru,

You could draw the items in the ListBox/ComboBox by yourself to mimic the
disabled items. You could handle the DrawItem event of ListBox/ComboBox or
derive a new class from ListBox/ComboBox and override the OnDrawItem
method.

I recommend you to adopt the second option, so that you could reuse the new
class wherever you want and needn't handle the DrawItem event for each
ListBox/ComboBox control instance.

Below is the samples of deriving two new classes from ListBox and ComboBox.
They all have a list to contain the indexes of the disabled items. We draw
those disabled items in gray.

using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

class ListBoxEx:ListBox
   {
       public List<int> DisabledItems = new List<int>();

       public ListBoxEx()
       {
          // Only if the DrawMode property is set to OwnerDrawFixed or
OwnerDrawVariable, we can draw items by ourselves
           this.DrawMode = DrawMode.OwnerDrawFixed;
       }

       protected override void OnDrawItem(DrawItemEventArgs e)
       {
           // use this statement to avoid the design-time error when the
instance of this class is dragged&dropped onto a form
           if (!this.DesignMode)
           {
               e.DrawBackground();

               Brush myBrush = Brushes.Black;                
               if (this.DisabledItems.Contains(e.Index))
               {
                   myBrush = Brushes.Gray;
               }                

               // Draw the current item text based on the current Font and
the custom brush settings.
               e.Graphics.DrawString(this.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
               // If the ListBox has focus, draw a focus rectangle around
the selected item.
               e.DrawFocusRectangle();
               base.OnDrawItem(e);
           }      

       }
     
   }

using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

class ComboBoxEx:ComboBox
   {
       public ComboBoxEx()
       {
           this.DrawMode = DrawMode.OwnerDrawFixed;
       }

       public List<int> DisabledItems = new List<int>();

       protected override void OnDrawItem(DrawItemEventArgs e)
       {
           e.DrawBackground();
           Brush mybrush = Brushes.Black;
           if (this.DisabledItems.Contains(e.Index))
           {
               mybrush = Brushes.Gray;
           }
           e.Graphics.DrawString(this.Items[e.Index].ToString(),e.Font,
mybrush, e.Bounds);
           e.DrawFocusRectangle();

           base.OnDrawItem(e);
       }
   }

Set up a WinForms application and add two classes files into it. Copy the
above code into the two classes files and build the project. You will see
ListBoxEx and ComboBoxEx appear in the Toolbox. Drag&drop them onto the
form, add several items into them and in the form's Load event handler, add
the following code:

this.listBoxEx1.DisabledItems.Add(0);
this.listBoxEx1.DisabledItems.Add(2);

this.comboBoxEx1.DisabledItems.Add(1);
this.comboBoxEx1.DisabledItems.Add(3);

Run the program and you will see the first and third items in the
ListBoxEx1 are in gray and the second and fourth items in the comboBoxEx1
are in gray.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
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.
Linda Liu [MSFT] - 28 Aug 2006 12:41 GMT
Hi Bragadiru,

How about the problem now?

If you have anything unclear, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
Sharpe - 31 Aug 2006 11:40 GMT
Hello,
I don't know if Bragadiru has any problem, but i would, since i have the same issue, as you can still select the grayed item. Anything about that ?

Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.