在组合框禁用特定项目 [英] Disabling particular Items in a Combobox

查看:111
本文介绍了在组合框禁用特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序,我想知道是否有禁用ComboBox项不改变SelectedIndex属性-1所有残障值的更优雅的方式。

我一直在使用Google和很多解决方案都涉及ASP.Net DropDownLists但这<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode%28VS.80%29.aspx\">LINK看起来很有希望。我想我可能要建立自己的ComboBox控件,但我之前重新发明我想我会问这里是否有可能车轮。

更新

下面是最终的解决方案,这要归功于阿里夫Eqbal:

  //组合框添加到窗体并将其命名为comboBox1
//
    使用系统;
    使用System.Drawing中;
    使用System.Windows.Forms的;    命名空间WindowsFormsApplication6
    {
        公共部分Form1类:表格
        {
            公共Form1中()
            {
                的InitializeComponent();
                this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
                this.comboBox1.DrawItem + =新System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
                this.comboBox1.SelectedIndexChanged + =新System.EventHandler(this.comboBox1_SelectedIndexChanged);
            }            私人无效Form1_Load的(对象发件人,EventArgs的发送)
            {
                this.comboBox1.Items.Add(测试1);
                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);
            }            字体myFont =新Font(空中,10,FontStyle.Underline | FontStyle.Regular);
            字体myFont2 =新Font(空中,10,FontStyle.Italic | FontStyle.Strikeout);            私人无效comboBox1_DrawItem(对象发件人,DrawItemEventArgs E)
            {
                如果(e.Index == 1 || e.Index == 4 || e.Index == 5)//我们禁用基于索引项,你可以有你的逻辑在这里
                {
                    e.Graphics.DrawString(comboBox1.Items [e.Index]的ToString(),myFont2,Brushes.LightSlateGray,e.Bounds);
                }
                其他
                {
                    e.DrawBackground();
                    e.Graphics.DrawString(comboBox1.Items [e.Index]的ToString(),myFont,Brushes.Black,e.Bounds);
                    e.DrawFocusRectangle();
                }
            }            无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs的发送)
            {
                如果(comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 4 || comboBox1.SelectedIndex == 5)
                    comboBox1.SelectedIndex = -1;
            }
        }
    }


解决方案

尝试......它是否满足你的目的:

我假设你有一个名为ComboBox1组合框,并要禁用的第二个项目,即与指数项目1.

,如下图所示设置组合框OwnerDrawFixed再处理这两个事件的DrawMode属性:

 字体myFont =新的字体(空中,10,FontStyle.Regular);私人无效comboBox1_DrawItem(对象发件人,DrawItemEventArgs E)
{
    如果(e.Index == 1)//我们禁用基于索引的项目,你可以在这里有你的逻辑
    {
        e.Graphics.DrawString(comboBox1.Items [e.Index]的ToString(),myFont,Brushes.LightGray,e.Bounds);
    }
    其他
    {
        e.DrawBackground();
        e.Graphics.DrawString(comboBox1.Items [e.Index]的ToString(),myFont,Brushes.Black,e.Bounds);
        e.DrawFocusRectangle();
    }
} 无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs的发送)
    {
        如果(comboBox1.SelectedIndex == 1)
            comboBox1.SelectedIndex = -1;
    }

I have a WinForms App and I was wondering if there was a more elegant way of disabling Combobox item without changing the SelectedIndex property -1 for all disabled values.

I have been googling and a lot of the solutions involve ASP.Net DropDownLists but this LINK looks promising. I think I may have to build my own ComboBox control but before I re-invent the wheel I figure I would ask here if it was possible.

UPDATE

Here is the final solution, thanks to Arif Eqbal:

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

解决方案

Try this...does it serve your purpose:

I assume you have a combobox called ComboBox1 and you want to disable the second item i.e. an item with index 1.

Set the DrawMode property of the comboBox to OwnerDrawFixed then handle these two events as shown below:

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;
    }

这篇关于在组合框禁用特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆