3
votes

I'm looking to build a Windows Forms combo box that has normal coloration, and allows the dropdown list to appear, but does not allow the value to actually change. As far as I can tell, this is not a duplicate of How to make Combobox in winforms readonly since all advice there seems to be directed toward disabling the combo box's interactivity.

My rationale: I have a form where all of the controls are read-only, and due to the nature of the application I think that there would be no risk of the user getting confused when the combo box's value does not change. I would like the user to be able to see all of the possible values of the enum to which the combo box is bound.

What I have so far is a pretty bad hack:

public partial class ReadOnlyComboBox : ComboBox
{
    int prevIndex = -1;

    public ReadOnlyComboBox()
    {
        InitializeComponent();
    }

    private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (prevIndex <= 0)
            prevIndex = SelectedIndex;
        else
            SelectedIndex = prevIndex;
    }
}

In effect, this ignores spurious "0" values from the framework, and takes the first non-zero value acquired from a binding source. Immediate disadvantages are that the value may only be set once, and that the bound enum must start at 1.

Any advice on cleaning this up would be welcome. Thanks.

1
To be clear, a normal combobox with the "DropDownStyle" property set to "DropDownList" is NOT what you want, correct?Inisheer
Actually I do want DropDownList in addition to the above.Reinderien
Ok, so the DropDownList shows non-editable items. To prevent a change, how is it originally getting populated... From another process, such as updating a status / in-progress work? OR, Do you want the user to be able to edit the first time the record is being created and prevent users from changing after the initial creation value set.DRapp
The user should never be able to edit the data. The list is populated from a BindingSource.Reinderien

1 Answers

1
votes

Use DropDownClosed event

public class ReadOnlyComboBox : ComboBox
{
    bool afterDropDown ;
    int prevIndex;

    public ReadOnlyComboBox()
    {
        this.SelectedIndexChanged+=new EventHandler(ReadOnlyComboBox_SelectedIndexChanged);
        this.DropDownClosed += new EventHandler(ReadOnlyComboBox_DropDownClosed);
    }

    void ReadOnlyComboBox_DropDownClosed(object sender, EventArgs e)
    {
        afterDropDown = true;
    }

    private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (afterDropDown)
        {
            afterDropDown = false;
            SelectedIndex = prevIndex;
        }
        else
        {
            prevIndex = SelectedIndex;
        }
    }
}