Windows Develop Bookmark and Share   
 index > Windows Forms Designer > how to make a combobox read only?
 

how to make a combobox read only?

hi

i want to make the combo box readonly
what should i do?

thank u
farnaz  Monday, September 26, 2005 10:40 AM
Try changing the DropDownStyle property to DropDownList. This can either be done through the designer or via code.
MarcD  Monday, September 26, 2005 12:32 PM
Try changing the DropDownStyle property to DropDownList. This can either be done through the designer or via code.
MarcD  Monday, September 26, 2005 12:32 PM
 MarcD wrote:
Try changing the DropDownStyle property to DropDownList. This can either be done through the designer or via code.

I heard this before but, IMHO setting it to dropdownlist makes it not readonly
because you you stil can chance the value, the best alternative is to chance enabled to false
Remco
RemcoJVG  Monday, September 26, 2005 4:47 PM

Exactly, the user still has a chance to change the values.

In my case, based on the user group, I would like to provide an option to change combo value. Though I set the combo to dropdownlist, I just leftout with an option to disable the control, which looks ugly in the form with no legibility to the content inside the combo.

Any other solution?

Murali Krishna K  Wednesday, November 08, 2006 7:32 AM

I solved this onceby checking if the value typed in was in the collection of added items. If it was, I accepted the value and did further processing. If it wasn`t in the collection, I discarded the value and stopped further processing. Maybe this approach could help you.

Draakje  Wednesday, November 08, 2006 12:36 PM
i want to make combobox read only and autocomplete
RanaRani  Wednesday, July 11, 2007 5:55 AM

use

ComboBox.IsEditable Property

ComboBox.IsReadOnly Property

Namespace : System.Windows.Controls

Jogesh Grover  Thursday, July 19, 2007 1:39 PM

here is one more solution i find out

ComboBox cb = new ComboBox();
Point p = new Point(100, 100);
cb.Location = p;
this.Controls.Add(cb);
cb.BringToFront();
cb.Items.Add("A");
cb.Items.Add("B");
cb.DropDownStyle = ComboBoxStyle.DropDownList;

Jogesh Grover  Thursday, July 19, 2007 2:03 PM

Hi,

Next code will make comboBox readOnly. To be more exact it will make textBox of the comboBox readOnly.

private bool m_readOnly = false;

private const int EM_SETREADONLY = 0x00CF;

internal delegate bool EnumChildWindowsCallBack( IntPtr hwnd, IntPtr lParam );

[DllImport("user32.dll", CharSet = CharSet.Auto)]

internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

[ DllImport( "user32.dll" ) ]

internal static extern int EnumChildWindows( IntPtr hWndParent, EnumChildWindowsCallBack lpEnumFunc, IntPtr lParam );

private bool EnumChildWindowsCallBackFunction(IntPtr hWnd, IntPtr lparam)

{

if( hWnd != IntPtr.Zero )

{

IntPtr readonlyValue = ( m_readOnly ) ? new IntPtr( 1 ) : IntPtr.Zero;

SendMessage( hWnd, EM_SETREADONLY, readonlyValue, IntPtr.Zero );

comboBox1.Invalidate();

return true;

}

return false;

}

private void MakeComboBoxReadOnly( bool readOnly )

{

m_readOnly = readOnly;

EnumChildWindowsCallBack callBack = new EnumChildWindowsCallBack( this.EnumChildWindowsCallBackFunction );

EnumChildWindows( comboBox1.Handle, callBack, IntPtr.Zero );

}


pavlo_ua  Friday, December 21, 2007 12:36 PM

I found the best wayis tocreate an ExtenderProvider for the combo box whereas inside you change the dropdownstyle to simple and trap all keystrokes. You'll need to rebuild the project after you add the ReadOnlyComboProvider class. Then simply drag one of these from the toolbox onto your form and set the properties via a property window or via code.

Here's the code for the extender Provider:

Option Explicit On

Option Strict On

Option Compare Text

Imports System.ComponentModel

Imports System.Windows.Forms

<Drawing.ToolboxBitmap(GetType(System.Windows.Forms.TextBox)), _

System.ComponentModel.DesignerCategoryAttribute("Code"), _

ProvideProperty("ReadOnly", GetType(Control)), _

ProvideProperty("OrigDropDownStyle", GetType(Control))> _

Public Class ReadOnlyComboProvider

Inherits System.ComponentModel.Component

Implements IExtenderProvider

'This hash table stores all the controls extended by this extender provider

Friend htProvidedProperties As New Hashtable

#Region "IExtenderProvider"

''' <summary>

''' The Property should be Extend / Not for the TextBox classes

''' </summary>

''' <param name="extendee"></param>

''' <returns></returns>

''' <remarks></remarks>

Public Function CanExtend(ByVal extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend

If TypeOf extendee Is ComboBox Then

Return True

Else

Return False

End If

End Function

#End Region

#Region "Extensible Properties"

Private Class ComboBoxProperties

Public IsReadOnly As Boolean = False

Public OrigComboBoxStyle As ComboBoxStyle = ComboBoxStyle.DropDown

End Class

<Category("Read Only Combobox Provider")> _

Sub SetReadOnly(ByVal ctrl As Control, ByVal value As Boolean)

Dim cbo As ComboBox = CType(ctrl, ComboBox)

If value = True Then

cbo.DropDownStyle = ComboBoxStyle.Simple

Else

cbo.DropDownStyle = GetControlFromHashtable(ctrl).OrigComboBoxStyle

End If

GetControlFromHashtable(ctrl).IsReadOnly = value

End Sub

<Category("Read Only Combobox Provider")> _

Function GetReadOnly(ByVal ctrl As Control) As Boolean

Return GetControlFromHashtable(ctrl).IsReadOnly

End Function

<Category("Read Only Combobox Provider")> _

Sub SetOrigDropDownStyle(ByVal ctrl As Control, ByVal value As ComboBoxStyle)

GetControlFromHashtable(ctrl).OrigComboBoxStyle = value

End Sub

<Category("Read Only Combobox Provider")> _

Function GetOrigDropDownStyle(ByVal ctrl As Control) As ComboBoxStyle

Return GetControlFromHashtable(ctrl).OrigComboBoxStyle

End Function

#End Region

#Region "Behavior"

Private Function GetControlFromHashtable(ByVal ctrl As Control) As ComboBoxProperties

If htProvidedProperties.Contains(ctrl) Then

Return DirectCast(htProvidedProperties(ctrl), ComboBoxProperties)

Else

Dim ProvidedProperties As New ComboBoxProperties

'Add A KeyPress Event Handler as the control is added to hash table

AddHandler ctrl.KeyPress, AddressOf KeyPressHandler

htProvidedProperties.Add(ctrl, ProvidedProperties)

Return ProvidedProperties

End If

End Function

Private Sub KeyPressHandler(ByVal sender As Object, ByVal e As KeyPressEventArgs)

Dim cboSender As ComboBox = CType(sender, ComboBox)

If GetControlFromHashtable(cboSender).IsReadOnly = True Then

e.Handled = True

Else

e.Handled = False

End If

End Sub

#End Region

End Class

Here's the code example to make the combobox readonly via code:

Note: This works similiarly to tooltips...

Me.ReadOnlyComboProvider1.SetReadOnly(myComboBox, False)

or

Me.ReadOnlyComboProvider1.SetReadOnly(myComboBox, True)

Rance Downer  Thursday, May 22, 2008 6:11 PM

Hi,

I am using framwork 2.0 and I could't fınd System.Windows.Controls.dll to give referances from my project.

thanks

ebrusongul  Thursday, July 10, 2008 1:30 PM

The best solution I have seen is to disable the box only for the short moment when it is entered.

Thatwill make it behaveexactly the same as if it was read only.

Example:

private void comboBox1_Enter(object sender, EventArgs e)

{

comboBox1.Enabled = false;

comboBox1.Enabled = true;

}

  • Proposed As Answer byJP14 Wednesday, February 25, 2009 3:42 PM
  •  
SharpTea  Wednesday, August 06, 2008 10:49 AM

Rance Downer

The Code you provided Seems to work great for locking a combo box.

however it doesn't seem to unlock the combobox. am I missing something?

By the way kudo's for the best solution I have seen so far to this Fiasco by microsoft in eliminating the Locked(readonly) selection from comboboxes.

Morgan

Morgan Gardiner  Friday, August 29, 2008 3:38 PM
This really *is* a fiasco! Several dozen lines of code just to sort out the simple 'combobox.readonly=true'!!!

What on earth would MS overlook this?! Surely there must be a reason?



WillC9999  Friday, February 13, 2009 10:58 AM
This was the simplest mechanism to achieve what I wanted, thanks for the tip, I used it on ToolStripComboBox
JP14  Wednesday, February 25, 2009 3:43 PM

You can use google to search for other answers

Custom Search

More Threads

• Getting 'MissingManifestResourceException' on derived control
• Referenced Project controls not in Toolbox?
• datagrid in winforms (vb.net)...
• Form Blew Up??!
• How to add a user control from the current project to the toolbox?
• Custom Component Designer - Visual Studio Design time environment
• Statustrip merge show childforms
• Dynamic Control Events
• Designer fails after adding more controls after adding tooltips
• Inheriting from a component and showing the result in the designer?