[c#] How can I make a ComboBox non-editable in .NET?

I want to have a "select-only" ComboBox that provides a list of items for the user to select from. Typing should be disabled in the text portion of the ComboBox control.

My initial googling of this turned up an overly complex, misguided suggestion to capture the KeyPress event.

This question is related to c# .net winforms combobox

The answer is


To make the text portion of a ComboBox non-editable, set the DropDownStyle property to "DropDownList". The ComboBox is now essentially select-only for the user. You can do this in the Visual Studio designer, or in C# like this:

stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

Link to the documentation for the ComboBox DropDownStyle property on MSDN.


To add a Visual Studio GUI reference, you can find the DropDownStyle options under the Properties of the selected ComboBox:

enter image description here

Which will automatically add the line mentioned in the first answer to the Form.Designer.cs InitializeComponent(), like so:

this.comboBoxBatch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

To add a Visual Studio GUI reference, you can find the DropDownStyle options under the Properties of the selected ComboBox:

enter image description here

Which will automatically add the line mentioned in the first answer to the Form.Designer.cs InitializeComponent(), like so:

this.comboBoxBatch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

Stay on your ComboBox and search the DropDropStyle property from the properties window and then choose DropDownList.


Stay on your ComboBox and search the DropDropStyle property from the properties window and then choose DropDownList.


COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;

COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;

To continue displaying data in the input after selecting, do so:

VB.NET
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    e.Handled = True
End Sub



C#
Private void ComboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}

To continue displaying data in the input after selecting, do so:

VB.NET
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    e.Handled = True
End Sub



C#
Private void ComboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}

for winforms .NET change DropDownStyle to DropDownList from Combobox property


for winforms .NET change DropDownStyle to DropDownList from Combobox property


Questions with c# tag:

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array How can I add raw data body to an axios request? Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file Convert string to boolean in C# Entity Framework Core: A second operation started on this context before a previous operation completed ASP.NET Core - Swashbuckle not creating swagger.json file Is ConfigurationManager.AppSettings available in .NET Core 2.0? No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization Getting value from appsettings.json in .net core .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Automatically set appsettings.json for dev and release environments in asp.net core? How to use log4net in Asp.net core 2.0 Get ConnectionString from appsettings.json instead of being hardcoded in .NET Core 2.0 App Unable to create migrations after upgrading to ASP.NET Core 2.0 Update .NET web service to use TLS 1.2 Using app.config in .Net Core How to send json data in POST request using C# ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response How to enable CORS in ASP.net Core WebAPI VS 2017 Metadata file '.dll could not be found How to set combobox default value? How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac ALTER TABLE DROP COLUMN failed because one or more objects access this column Error: the entity type requires a primary key How to POST using HTTPclient content type = application/x-www-form-urlencoded CORS: credentials mode is 'include' Visual Studio 2017: Display method references Where is NuGet.Config file located in Visual Studio project? Unity Scripts edited in Visual studio don't provide autocomplete How to create roles in ASP.NET Core and assign them to users? Return file in ASP.Net Core Web API ASP.NET Core return JSON with status code auto create database in Entity Framework Core Class Diagrams in VS 2017 How to read/write files in .Net Core? How to read values from the querystring with ASP.NET Core? how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application? ASP.NET Core Get Json Array using IConfiguration Entity Framework Core add unique constraint code-first No templates in Visual Studio 2017 ps1 cannot be loaded because running scripts is disabled on this system

Questions with .net tag:

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface How to decode JWT Token? What's the difference between .NET Core, .NET Framework, and Xamarin? .NET Core vs Mono How to specify the port an ASP.NET Core application is hosted on? WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery Why is the Visual Studio 2015/2017/2019 Test Runner not discovering my xUnit v2 tests Build error, This project references NuGet IIS Config Error - This configuration section cannot be used at this path There is no argument given that corresponds to the required formal parameter - .NET Error Could not find a part of the path ... bin\roslyn\csc.exe Can you install and run apps built on the .NET framework on a Mac? What is the purpose of nameof? Microsoft.ReportViewer.Common Version=12.0.0.0 What is the default value for Guid? Authentication failed because remote party has closed the transport stream What is the difference between a Shared Project and a Class Library in Visual Studio 2015? How to break out of the IF statement No connection could be made because the target machine actively refused it 127.0.0.1 Default SecurityProtocol in .NET 4.5 How to properly make a http web GET request What is ".NET Core"? IOException: The process cannot access the file 'file path' because it is being used by another process System.web.mvc missing Disable SSL fallback and use only TLS for outbound connections in .NET? (Poodle mitigation) SQL Connection Error: System.Data.SqlClient.SqlException (0x80131904) Write to Windows Application Event Log Operator overloading ==, !=, Equals System.Net.WebException: The remote name could not be resolved: Running multiple async tasks and waiting for them all to complete What is an "index out of range" exception, and how do I fix it? Found conflicts between different versions of the same dependent assembly that could not be resolved Getting the first and last day of a month, using a given DateTime object Error 1053 the service did not respond to the start or control request in a timely fashion BadImageFormatException. This will occur when running in 64 bit mode with the 32 bit Oracle client components installed Download files from SFTP with SSH.NET library An object reference is required to access a non-static member AppendChild() is not a function javascript Send JSON via POST in C# and Receive the JSON returned? Why is HttpClient BaseAddress not working? KERNELBASE.dll Exception 0xe0434352 offset 0x000000000000a49d

Questions with winforms tag:

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager "Use the new keyword if hiding was intended" warning Click a button programmatically how to bind datatable to datagridview in c# Show message box in case of exception DataGridView AutoFit and Fill How do I write a backslash (\) in a string? "Input string was not in a correct format." Populate a datagridview with sql query results Close Form Button Event How to set the timeout for a TcpClient? Open File Dialog, One Filter for Multiple Excel Extensions? Fit Image into PictureBox Popup window in winform c# Setting focus to a textbox control DataGridView changing cell background color How add items(Text & Value) to ComboBox & read them in SelectedIndexChanged (SelectedValue = null) Get current folder path Reset all the items in a form Get single listView SelectedItem opening a window form from another form programmatically Playing a MP3 file in a WinForm application Using FileSystemWatcher to monitor a directory How to create many labels and textboxes dynamically depending on the value of an integer variable? Getting value of selected item in list box as string maxReceivedMessageSize and maxBufferSize in app.config Update label from another thread Invoke(Delegate) Make TextBox uneditable Close a MessageBox after several seconds How to close form Change the row color in DataGridView based on the quantity of a cell value Get the current date and time Check/Uncheck a checkbox on datagridview Adding new line of data to TextBox How to get full file path from file name? Search for value in DataGridView in a column How to properly exit a C# application? Saving image to file How to create exe of a console application How to get the selected row values of DevExpress XtraGrid?

Questions with combobox tag:

How to set combobox default value? PHP code to get selected text of a combo box How to add items to a combobox in a form in excel VBA? How add items(Text & Value) to ComboBox & read them in SelectedIndexChanged (SelectedValue = null) Get Selected value of a Combobox jQuery "on create" event for dynamically-created elements How to get the selected item of a combo box to a string variable in c# HTML combo box with option to type an entry twitter bootstrap autocomplete dropdown / combobox with Knockoutjs C# winforms combobox dynamic autocomplete ComboBox.SelectedText doesn't give me the SelectedText JavaScript code for getting the selected value from a combo box Clear ComboBox selected text How do I clear a combobox? Getting selected value of a combobox How to set selected value from Combobox? Get the value for a listbox item by index Binding Combobox Using Dictionary as the Datasource how to check if item is selected from a comboBox in C# Selecting default item from Combobox C# How to pass parameters on onChange of html select Get selected value from combo box in C# WPF QComboBox - set selected item based on the item's data ComboBox: Adding Text and Value to an Item (no Binding Source) How do I set combobox read-only or user cannot write in a combo box only can select the given items? How can I create an editable combo box in HTML/Javascript? How can I show a combobox in Android? Removing All Items From A ComboBox? ComboBox- SelectionChanged event has old value, not new value ComboBox SelectedItem vs SelectedValue Getting Serial Port Information VB.NET: how to prevent user input in a ComboBox Populating a ComboBox using C# How to get multiple selected values of select box in php? jQuery Combobox/select autocomplete? How do I style a <select> dropdown with only CSS? WPF - add static items to a combo box How to display default text "--Select Team --" in combo box on pageload in WPF? Binding an enum to a WinForms combo box, and then setting it WPF MVVM ComboBox SelectedItem or SelectedValue not working How to bind a List to a ComboBox? How to disable editing of elements in combobox for c#? Binding a WPF ComboBox to a custom list How do I set the selected item in a comboBox to match my string using C#? How can I create an editable dropdownlist in HTML? C# - Fill a combo box with a DataTable Professional jQuery based Combobox control? How can I make a ComboBox non-editable in .NET?