Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
In property of comboBox, I could not find readOnly.

What I have tried:

C#
comboBox1.ReadOnly=false;//Error
Posted
Updated 13-Sep-23 4:12am
v2

Change the DropDownStyle property to DropDownList: ComboBox.DropDownStyle Property (System.Windows.Forms) | Microsoft Learn[^] - this gives you a collection of items the user can select but not change.

If you want the user to be unable to change the selection, you would need to set the Enabled property to false.
 
Share this answer
 
To add on to what OriginalGriff pointed you to in the link, you can make your combobox control behave like a read-only control by handling user input and preventing changes -
C#
using System;
using System.Windows.Forms;

namespace ReadOnlyComboBoxExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            //Populate your ComboBox with some items...
            comboBox1.Items.Add("Item 1");
            comboBox1.Items.Add("Item 2");
            comboBox1.Items.Add("Item 3");
            
            //Set the initial selection...
            comboBox1.SelectedIndex = 0;
            
            //Disable user input to make it read-only as suggested by OriginalGriff...
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        //Handle the SelectedIndexChanged event to prevent changes...
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //This event will be triggered if the user tries to change the selection.
            //You can keep it empty to prevent any action when the selection changes.
            //OR you can show a message or perform any custom logic if needed in this block...
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900