How to make Combobox in winforms readonly

后端 未结 18 1940
攒了一身酷
攒了一身酷 2020-12-08 09:40

I do not want the user to be able to change the value displayed in the combobox. I have been using Enabled = false but it grays out the text, so it is not very

相关标签:
18条回答
  • 2020-12-08 09:53

    Not sure if this is what you're looking for but...

    Set the DropDownStyle = DropDownList

    Then on the SelectedIndexChanged event

    If (ComboBox1.SelectedIndex <> 0)
    {
        ComboBox1.SelectedIndex = 0
    }
    

    This ugly part is that they will "feel" like they can change it. They might think this is an error unless you give them an alert telling them why they can't change the value.

    0 讨论(0)
  • 2020-12-08 09:53

    Here is the Best solution for the ReadOnly Combo.

    private void combo1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.KeyChar = (char)Keys.None;
    }
    

    It will discard the keypress for the Combo.

    0 讨论(0)
  • 2020-12-08 09:55

    Michael R's code works, but...
    The DropDownHeight = 1; must be back to the default value when ReadOnly property is set to false. So, insert before base.OnDropDown(e): DropDownHeight = 106;

    using System;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace Test_Application
    {
        class ReadOnlyComboBox : ComboBox
        {
            private bool _readOnly;
            private bool isLoading;
            private bool indexChangedFlag;
            private int lastIndex = -1;
            private string lastText = "";
    
            public ReadOnlyComboBox()
            {
            }
    
            public bool ReadOnly
            {
                get { return _readOnly; }
                set { _readOnly = value; }
            }
    
            protected override void OnDropDown (EventArgs e)
            {
                if (_readOnly)
                {
                    DropDownHeight = 1;
                    var t = new Thread(CloseDropDown);
                    t.Start();
                    return;
                }
                DropDownHeight = 106; //Insert this line.
                base.OnDropDown(e);
            }
    
            private delegate void CloseDropDownDelegate();
            private void WaitForDropDown()
            {
                if (InvokeRequired)
                {
                    var d = new CloseDropDownDelegate (WaitForDropDown);
                    Invoke(d);
                }
                else
                {
                    DroppedDown = false;
                }
            }
            private void CloseDropDown()
            {
                WaitForDropDown();
            }
    
            protected override void OnMouseWheel (MouseEventArgs e)
            {
                if (!_readOnly) 
                    base.OnMouseWheel(e);
            }
    
            protected override void OnKeyDown (KeyEventArgs e)
            {
                if (_readOnly)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Back:
                        case Keys.Delete:
                        case Keys.Up:
                        case Keys.Down:
                            e.SuppressKeyPress = true;
                            return;
                    }
                }
                base.OnKeyDown(e);
            }
    
            protected override void OnKeyPress (KeyPressEventArgs e)
            {
                if (_readOnly)
                {
                    e.Handled = true;
                    return;
                }
                base.OnKeyPress(e);
            }
        }
    }
    

    To complete this answer:

    File -> New -> Project... Visual C# -> Windows -> Classic Desktop -> Windows Forms Control Library

    type the Name of your control - OK and paste this code.

    You can choose the name of your dll file:
    Project - yourproject Properties...

    • Assembly name: type the name. Just build the solution and you have your dll file. So, open the project where you want to use your Read Only combo, right click on References
    • Add Reference... and browse your dll file. To Insert your custom component into Toolbox, open your Toolbox, right click on General tab -> Choose Items...
    • Browse your dll file - Open. Now you can use your ReadOnlyComboBox in your projects. PS: I'm using VS2015.
    0 讨论(0)
  • 2020-12-08 09:56

    The article ComboBox-with-read-only-behavior suggests an interesting solution:

    Create both a readonly textbox and a combobox in the same place. When you want readonly mode, display the textbox, when you want it to be editable, display the combobox.

    0 讨论(0)
  • 2020-12-08 09:58

    I dont know if that is what you are looking but this prevents the user from chosing any item from the drop down and still be able to type text in the combobox. If you dont want the user to type text in the combobox you can make it Dropdown list from the properties menu.

    So you get Read Only combobox.

    1. On Selected Index Changed
    2. Make the selected Index -1 "comboBox.SelectedIndex = -1";

          private void MyComboBox_comboBox_SelectedIndexChanged(object sender, EventArgs e)
          {
              MyComboBox_comboBox.SelectedIndex = -1; 
      
          }
      
    0 讨论(0)
  • 2020-12-08 10:00

    This is how you would address the fact that a ComboBox with Enabled = False is hard to read:

    A combobox that looks decent when it is disabled

    0 讨论(0)
提交回复
热议问题