Preets
Preets

Reputation: 6952

Extend System.Windows.Forms.ComboBox

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?

Upvotes: 1

Views: 2385

Answers (2)

shahkalpesh
shahkalpesh

Reputation: 33476

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

Upvotes: 1

Roger Lipscombe
Roger Lipscombe

Reputation: 91825

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.

Upvotes: 0

Related Questions