Cómo puedo hacer un ComboBox no editable in.NET?


Quiero tener un "select-only" ComboBox que proporcione una lista de elementos para que el usuario seleccione. La escritura debe estar desactivada en la parte de texto del control ComboBox.

Mi búsqueda inicial en Google de esto resultó en una sugerencia demasiado compleja y equivocada para capturar el evento KeyPress.

Author: Peter Mortensen, 2008-09-17

5 answers

Para que la parte de texto de un ComboBox no sea editable, establezca la propiedad DropDownStyle en "DropDownList". El ComboBox ahora es esencialmente select-only para el usuario. Puedes hacer esto en Visual Studio designer, o en C# de esta manera:

stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

Enlace a la documentación de la propiedad ComboBox DropDownStyle en MSDN.

 343
Author: Cory Engebretson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 12:15:14

Para agregar una referencia de interfaz gráfica de Visual Studio, puede encontrar las opciones DropDownStyle en las Propiedades del cuadro combinado seleccionado:

introduzca la descripción de la imagen aquí

Que añadirá automáticamente la línea mencionada en la primera respuesta al Formulario.Diseñador.cs InitializeComponent(), así:

this.comboBoxBatch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
 56
Author: invertigo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-23 21:44:09

Permanezca en su cuadro combinado y busque la propiedad DropDropStyle desde la ventana propiedades y luego elija DropDownList.

 27
Author: LZara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-15 17:51:05

COMBOBOXID.DropDownStyle = ComboBoxStyle.En la lista desplegable;

 2
Author: Abhishek Jaiswal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-03 08:33:32

Para continuar mostrando los datos en la entrada después de seleccionar, hágalo:

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;
}
 1
Author: Diogo Rodrigues,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-16 13:58:07