views:

989

answers:

2

I would like to extend the System.Windows.Forms.ComboBox control with a ReadOnly property, which would display the selected item’s text (similar to a label) when ReadOnly = true. (I do not like the disabled look achieved by setting Enabled=false)

How do I do this in winforms? It was really simple in ASP.NET where all I had to do was override the Render method. It doesn’t seem so straightforward with winforms however.

From what I gather I need to

a) Override the OnPaint method

b) Call this.SetStyle(ControlStyles.UserPaint, true) so that OnPaint is called.

But now it seems like I have to do ALL the painting myself. Is that true? Is it not possible to let the base ComboBox deal with painting the control when ReadOnly = false? Also, what ControlStyles should I be using?

A: 

Put a ComboBox control on a UserControl. The UserControl would pass through most of the properties and events, but in response to the .ReadOnly property, it would hide the ComboBox control and show a ReadOnly edit box instead.

Roger Lipscombe
Can I automatically pass on the properties and events of combobox via my control ?
Preets
Not easily. You'd have to delegate the interesting ones yourself. You could probably put something together using reflection to automatically build the code at compile time, though.
Roger Lipscombe
+1  A: 

Do what windows does.
Have just 1 item in the combobox and let it be selected and enabled.

shahkalpesh