2012-07-12 20 views
15

में विशेष आइटम को अक्षम करना मेरे पास एक WinForms ऐप है और मैं सोच रहा था कि सभी अक्षम मूल्यों के लिए चयनित इंडेक्स प्रॉपर्टी -1 को बदले बिना कम्बोबॉक्स आइटम को अक्षम करने का एक और शानदार तरीका था।एक कॉम्बोबॉक्स

मैं googling किया गया है और समाधान का एक बहुत ASP.Net DropDownLists शामिल लेकिन इस LINK होनहार लग रहा है। मुझे लगता है कि मुझे अपना कॉम्बोबॉक्स नियंत्रण बनाना पड़ सकता है लेकिन इससे पहले कि मैं पहिया का पुन: आविष्कार करता हूं, मुझे लगता है कि अगर मैं संभव था तो मैं यहां पूछूंगा।

//Add a Combobox to a form and name it comboBox1 
// 
    using System; 
    using System.Drawing; 
    using System.Windows.Forms; 

    namespace WindowsFormsApplication6 
    { 
     public partial class Form1 : Form 
     { 
      public Form1() 
      { 
       InitializeComponent(); 
       this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed; 
       this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem); 
       this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 
      } 

      private void Form1_Load(object sender, EventArgs 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("Aerial", 10, FontStyle.Underline|FontStyle.Regular); 
      Font myFont2 = new Font("Aerial", 10, FontStyle.Italic|FontStyle.Strikeout); 

      private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
      { 
       if (e.Index == 1 || e.Index == 4 || e.Index == 5)//We are disabling item based on Index, you can have your logic here 
       { 
        e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), myFont2, Brushes.LightSlateGray, e.Bounds); 
       } 
       else 
       { 
        e.DrawBackground(); 
        e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), myFont, Brushes.Black, e.Bounds); 
        e.DrawFocusRectangle(); 
       } 
      } 

      void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
      { 
       if (comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 4 || comboBox1.SelectedIndex == 5) 
        comboBox1.SelectedIndex = -1; 
      } 
     } 
    } 
+3

ASP.NET = WinForms, वहाँ मत देखो। मूल कॉम्बोबॉक्स का विस्तार करना बहुत मुश्किल नहीं है (आमतौर पर चेकबॉक्स या आइकन या व्हाट्नॉट जोड़ने के लिए किया जाता है), लेकिन मुझे नहीं लगता कि ऐसा कोई मानक समर्थन है। –

+1

आपके द्वारा उल्लिखित लिंक यह तरीका है कि आप वास्तव में उपयोगकर्ताओं को अक्षम होने वाले आइटम का अनुभव देना चाहते हैं। आप पाठ आकर्षित करने के लिए रूप में धूसर हो, आप एक चयन backcolor और इतने पर और उपयोगकर्ता निश्चित रूप से नहीं दिखाने के लिए अभी भी है कि आइटम का चयन कर सकते हैं ताकि आप निश्चित रूप से SelectedIndexChanged संभालने के लिए और -1 SelectedIndex सेट करना होगा चाहते हो सकता है चाहते हो सकता है। लेकिन यह अभ्यास करने के लिए दृष्टि से अधिक सूचक होगा। –

उत्तर

23

इस प्रयास करें ... यह अपने उद्देश्य की पूर्ति करता है:

अद्यतन

यहाँ अंतिम समाधान, आरिफ इकबाल करने के लिए धन्यवाद है

मुझे लगता है कि आप एक बता गया है कॉम्बोबॉक्स 1 कहा जाता है और आप दूसरी वस्तु यानी इंडेक्स के साथ एक आइटम को अक्षम करना चाहते हैं।

DrawMode की संपत्ति सेट करें जैसा कि नीचे दिखाया OwnerDrawFixed को बता गया तो इन दो घटनाओं को संभाल:

Font myFont = new Font("Aerial", 10, FontStyle.Regular); 

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
{   
    if (e.Index == 1)//We are disabling item based on Index, you can have your logic here 
    { 
     e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), myFont, Brushes.LightGray, e.Bounds); 
    } 
    else 
    { 
     e.DrawBackground(); 
     e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), myFont, Brushes.Black, e.Bounds); 
     e.DrawFocusRectangle(); 
    } 
} 

void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedIndex == 1) 
      comboBox1.SelectedIndex = -1; 
    } 
+2

आप मेरे दोस्त एक प्रतिभाशाली हैं, धन्यवाद! –

11

यहाँ मेरा उत्तर आरिफ इकबाल के पर 100% आधारित है। सुधार कर रहे हैं:

  • बजाय नए लोगों को बनाने
  • डिफ़ॉल्ट का पुन: उपयोग (ताकि यदि आप इसे डिजाइनर में बदलते हैं, तो कोड को अद्यतन करने के लिए नहीं होगा) के ComboBox से Font पुन: उपयोग SystemBrushes हर बार धूसर हो जाता आइटम दोबारा बनाई हैं, उनके रंग
  • विकलांग मदों के लिए
  • मैं पृष्ठभूमि पुनः बनाने का था, और, (यदि आप मैन्युअल ComboBox हालांकि में प्रयोग किया जाता रंग बदल यह काम नहीं करेगा तो यह अपने विषय से मेल खाना चाहिए) काले
  • के करीब और करीब पहुंचें एक समर्पित IsItemDisabled मीटर बनाएं ethod कॉपी/पेस्ट

// Don't forget to change DrawMode, else the DrawItem event won't be called. 
// this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; 

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    ComboBox comboBox = (ComboBox)sender; 

    if (IsItemDisabled(e.Index)) 
    { 
     // NOTE we must draw the background or else each time we hover over the text it will be redrawn and its color will get darker and darker. 
     e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); 
     e.Graphics.DrawString(comboBox.Items[e.Index].ToString(), comboBox.Font, SystemBrushes.GrayText, e.Bounds); 
    } 
    else 
    { 
     e.DrawBackground(); 
     e.Graphics.DrawString(comboBox.Items[e.Index].ToString(), comboBox.Font, SystemBrushes.ControlText, e.Bounds); 
     e.DrawFocusRectangle(); 
    } 
} 

void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (IsItemDisabled(comboBox1.SelectedIndex)) 
     comboBox1.SelectedIndex = -1; 
} 

bool IsItemDisabled(int index) 
{ 
    // We are disabling item based on Index, you can have your logic here 
    return index % 2 == 1; 
} 
4

यहाँ एक और संशोधन है से बचने के लिए। उपर्युक्त समाधानों की समस्या यह है कि एक चयनित आइटम दिखाई नहीं दे रहा है क्योंकि फ़ॉन्ट अग्रभूमि और पृष्ठभूमि चयन दोनों अंधेरे हैं। इसलिए फ़ॉन्ट e.State के मूल्य के अनुसार सेट किया जाना चाहिए:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     ComboBox comboBox = (ComboBox)sender; 
     if (e.Index >= 0) 
     { 
      if (IsItemDisabled(e.Index)) 
      { 
       e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); 
       e.Graphics.DrawString(comboBox.Items[e.Index].ToString(), comboBox.Font, Brushes.LightSlateGray, e.Bounds); 
      } 
      else 
      { 
       e.DrawBackground(); 

       // Set the brush according to whether the item is selected or not 
       Brush br = ((e.State & DrawItemState.Selected) > 0) ? SystemBrushes.HighlightText : SystemBrushes.ControlText; 

       e.Graphics.DrawString(comboBox.Items[e.Index].ToString(), comboBox.Font, br, e.Bounds); 
       e.DrawFocusRectangle(); 
      } 
     } 
    } 
संबंधित मुद्दे