Search This Blog

Sunday, June 30, 2013

disable a combobox in WinForms without graying it out

Setting these on your comobobox will do the trick you are looking for, Combo is enabled but nobody can change or type anything so Appearance = Enabled, Behaviour = Disabled :)
        comboBox1.DropDownHeight = 1;
        comboBox1.KeyDown += (s, e) => e.Handled = true;
        comboBox1.KeyPress += (s, e) => e.Handled = true;
        comboBox1.KeyUp += (s, e) => e.Handled = true;
If for some reason you cannot use lambdas then following handlers can be associated. Right Click -> Paste has to be handled additionally if you have DropDownStyle = DropDown.
    //void comboBox1_KeyUp(object sender, KeyEventArgs e)
    //{
    //    e.Handled = true;
    //}

    //void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    //{
    //    e.Handled = true;
    //}

    //void comboBox1_KeyDown(object sender, KeyEventArgs e)
    //{
    //    e.Handled = true;
    //}

Wednesday, June 26, 2013

Prevent duplicate MDI children forms

foreach (Form form in Application.OpenForms)
{
    if (form.GetType() == typeof(MyFormType))
    {
        form.Activate();
        return;
    }
}

Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();