2012-07-12 25 views
15

私はWinForms Appを持っており、無効なすべての値に対してSelectedIndexプロパティ-1を変更せずに、よりうまくコンボボックスアイテムを無効にする方法があるのだろうかと思いました。Combobox内の特定のアイテムを無効にする

私はグーグルであり、多くのソリューションにはASP.Net DropDownListsが含まれていますが、このLINKは有望です。私は自分自身のComboBoxコントロールを構築する必要があるかもしれないと思うが、ホイールを再発明する前に、もし可能ならここで尋ねる。私はあなたがコンボボックスを持っていると仮定し

//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は表示されません。基本的なComboBoxを拡張するのは大変難しいことではありませんが(通常、チェックボックスやアイコンなどを追加するために行われます)、標準的なサポートはないと思います。 –

+1

あなたが本当にユーザーにアイテムの使用感を与えたい場合は、あなたが言及したリンクがあります。テキストを灰色で描画したい場合や、選択範囲のバックカラーなどを表示したくない場合もあります。もちろん、ユーザーはその項目を選択できるので、もちろんselectedIndexChangedを処理し、selectedIndexを-1に設定する必要があります。しかし、視覚的には運動をすることが示唆されます。 –

答えて

23

それはあなたの目的を果たすん...これを試してみてください:ここでUPDATE

は、アリフ・Eqbalのおかげで最終的な解決策でありますComboBox1と呼ばれ、2番目のアイテム、つまりインデックス1のアイテムを無効にします。

OwnerDrawFixedにコンボボックス以下に示すようにこれら2つのイベントを処理します。

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

はここアリフEqbalさんに100%基づいて、私の答えです。 改善点は以下のとおりです。(あなたはデザイナーでそれを変更した場合は、コードを更新する必要がないように)

  • はデフォルトSystemBrushesを再利用
  • 代わりに新しいものを作成するためのコンボボックスからFontを再利用します(それはあなたのテーマに一致する必要がありますが、手動でコンボボックスで使用される色を変更すると機能しません)
  • 無効な項目については、そうでなければグレーの項目を再描画するたびに、黒に近づくと近づく
  • 作成専用IsItemDisabled m

// 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

コピー/貼り付けを回避するためにethodここでさらに変更があります。上記の解決策の問題点は、フォントフォアグラウンドと背景選択が両方とも暗いため、選択した項目が表示されないことです。したがって、フォントはe.Stateの値に応じて設定する必要があります。

関連する問題