How to make Combobox in winforms readonly

C#.NetWinforms

C# Problem Overview


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 readable. I want it to behave like a textbox with ReadOnly = true, where the text is displayed normally, but the user can't edit it.

Is there is a way of accomplishing this?

C# Solutions


Solution 1 - C#

make DropDownStyle property to DropDownList instead of DropDown then handle the TextChanged event to prevent user changing text.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

The best thing I can suggest is to replace the combo-box with a read-only textbox (or just perhaps a label) - that way the user can still select/copy the value, etc.

Of course, another cheeky tactic would be to set the DropDownStyle to DropDownList, and just remove all other options - then the user has nothing else to pick ;-p

Solution 5 - C#

enter link description here

> Just change the DropDownStyle to DropDownList. Or if you want it completely read only you can set Enabled = false, or if you don't like the look of that I sometimes have two controls, one readonly textbox and one combobox and then hide the combo and show the textbox if it should be completely readonly and vice versa.

Solution 6 - C#

I've handled it by subclassing the ComboBox to add a ReadOnly property that hides itself when set and displays a ReadOnly TextBox on top containing the same Text:

class ComboBoxReadOnly : ComboBox
{
    public ComboBoxReadOnly()
    {
        textBox = new TextBox();
        textBox.ReadOnly = true;
        textBox.Visible = false;
    }

    private TextBox textBox;

    private bool readOnly = false;

    public bool ReadOnly
    {
        get { return readOnly; }
        set
        {
            readOnly = value;

            if (readOnly)
            {
                this.Visible = false;
                textBox.Text = this.Text;
                textBox.Location = this.Location;
                textBox.Size = this.Size;
                textBox.Visible = true;

                if (textBox.Parent == null)
                    this.Parent.Controls.Add(textBox);
            }
            else
            {
                this.Visible = true;
                this.textBox.Visible = false;
            }
        }
    }
}

Solution 7 - C#

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.

Solution 8 - C#

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.

Solution 9 - C#

You can change the forecolor and backcolor to the system colors for an enabled combo box, although this may confuse the users (why have it if they can't change it), it will look better.

Solution 10 - C#

Actually, its rather simple:

Private Sub combobox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles combobox1.KeyDown
    ' the following makes this the combobox read only    
    e.SuppressKeyPress = True    
End Sub

Solution 11 - C#

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

Solution 12 - C#

Why don't you just use a text box? Text box has a "Read only" property, and since you want your combo box only to display data, I don't see why you would need a combo box.

An alternative is that you just cancel out the input for the "on value changed" event. That way you will be displaying your information no mater what user does ...

Solution 13 - C#

Set DropdownStyle Property to Simple

Add below code to to KeyPress event of ComboBox

private void comboBoxName_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
    return;
}

Add below code to to KeyDown event of ComboBox

private void comboBoxName_KeyDown(object sender, KeyEventArgs e)
{
    e.Handled = true;
    return;
}

Solution 14 - C#

If you've already populated it, and selected the appropriate item, and made it a DropDownList, then you can use an extension method like this to quickly reduce the selection list down to just the selected item:

public static void MakeReadOnly(this ComboBox pComboBox) {
   if (pComboBox.SelectedItem == null)
      return;

   pComboBox.DataSource = new List<object> {
      pComboBox.SelectedItem
   };
}

Solution 15 - C#

I know that I'm a little late to the party, but I was researching this exact question and I knew that there had to be some way to make the combobox readonly as if it were a textbox and disabled the list popping up. It's not perfect, but it is definitely better than all of the answers I've been finding all over the internet that don't work for me. After the button is pressed and the OnDropDown is called, a new thread is created that will set the DroppedDown property to false, thus creating the effect of "nothing happening." The mouse wheel is consumed and key events are consumed as well.

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

Solution 16 - C#

Simplest way in code:

instead of adding methods for KeyPress or KeyDown, add this code on 'Form1_Load' method:

> comboBox1.KeyPress += (sndr, eva) => eva.Handled = true;

or

> comboBox1.KeyDown += (sndr, eva) => eva.SuppressKeyPress = true;

(sndr, eva) is for (object sender, EventArgs e)

Solution 17 - C#

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

Solution 18 - C#

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. It doesn't have "e.KeyChar" !

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKishore AView Question on Stackoverflow
Solution 1 - C#amar vashiView Answer on Stackoverflow
Solution 2 - C#user60456View Answer on Stackoverflow
Solution 3 - C#Dan WilliamsView Answer on Stackoverflow
Solution 4 - C#Marc GravellView Answer on Stackoverflow
Solution 5 - C#VirusView Answer on Stackoverflow
Solution 6 - C#NiallView Answer on Stackoverflow
Solution 7 - C#ManishView Answer on Stackoverflow
Solution 8 - C#FabioView Answer on Stackoverflow
Solution 9 - C#Tom AndersonView Answer on Stackoverflow
Solution 10 - C#RobView Answer on Stackoverflow
Solution 11 - C#Victor ZakharovView Answer on Stackoverflow
Solution 12 - C#David BožjakView Answer on Stackoverflow
Solution 13 - C#Nalaka526View Answer on Stackoverflow
Solution 14 - C#Dave CousineauView Answer on Stackoverflow
Solution 15 - C#Michael RView Answer on Stackoverflow
Solution 16 - C#snirView Answer on Stackoverflow
Solution 17 - C#Stefan27View Answer on Stackoverflow
Solution 18 - C#BehzadView Answer on Stackoverflow