- Where Developers Learn, Share, & Build Careers
I have a WinForms application and I was wondering if there was a more elegant way to disable Combobox items without SelectedIndex property return It was - 1 for all the disabled values. I have been googling and many solutions include ASP.NET dropdown list but it looks promising. I think I might have to make my combo box control but before I invent the wheel again, I think that if it is possible I would ask here. Here is the ultimate solution, thanks for Arif Iqbal: Try it ... it serves your purpose: I think That you have a combobox named Combobox 1 and you want to disable another item with index 1 item Set the set of HonDrifix from the combo box, then show these two events below: UPDATE
// A Cambodia is a form And add it to the system named combobox 1; Using System.Drawing; Using System.Windows.Forms; Namespace WindowsFormsApplication6 {Public Partial Category Form 1: Form {Public Form 1 ()} {InitializeComponent (); This.comboBox1.DrawMode = DrawMode.OwnerDrawFixed; This.comboBox1.DrawItem + new system.window.form. DietemeventHandler (this.comboBox1_DrawItem); This.combobox1.SelectedIndexChanged + = New System. The eventhandler (this.comboBox1SelectedIndexChanged); } Private Zero form1_load (object sender, eventAgds E) {this.comboBox1.Items.Add ("Test1"); This.comboBox1.Items.Add ("Test2"); This.comboBox1.Items.Add ("test3"); This.comboBox1.Items.Add ("test4"); This.comboBox1.Items.Add ("Test5"); This.comboBox1.Items.Add ("Test6"); This.comboBox1.Items.Add ("Test7"); } Font myFont = new font ("Arial", 10, font style. Simple line | font style; regular); Font myFont2 = new font ("Aerial", 10, FontStyle. Italic | Font Style; Strikeout); Private void comboBox1_DrawItem (object sender, DrawItemEventArgs e) {if (e.Index == 1 || e.Index == 4 || e.Index == 5) // we are unable items based on the index, then You can do your logic here {e.Graphics.DrawString (comboBox1.Items [e.Index] .toString (), myFont2, Brushes.LightSlateGray, e.Bounds); } And (e. Draw the background (); E.Graphics.DrawString (comboBox1.Items [e.Index]. Tostring (), MyFont, brush. Black, e. Bounce); e.DrawFocusRectangle ();}} void comboBox1_SelectedIndexChanged (object sender, EventArgs e) {if (comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 4 || comboBox1.SelectedIndex == 5) comboBox1.SelectedIndex = -1;}}}}
font myFont = new font ("Aerial", 10, font style. regular); private void comboBox1_DrawItem (object sender, DrawItemEventArgs e) {if (e.Index == 1) // we are unable items based on the index, you {e.Graphics.DrawString (your logic here ComboBox1.Items may [e.index] .ostring (), myfonts, brush Lightgrey, E. Bounce); } And (e. Draw the background (); E.Graphics.DrawString (comboBox1.Items [e.Index]. Tostring (), MyFont, brush. Black, e. Bounce); e.DrawFocusRectangle ();}} void comboBox1_SelectedIndexChanged (Object Sender, EventArgs e) {if (comboBox1.SelectedIndex == 1) comboBox1.SelectedIndex = -1;}
Comments
Post a Comment