Connecting Tech Pros Worldwide Help | Site Map

How to make a comboBox behave like a Label at runntime

malcolm
Guest
 
Posts: n/a
#1: Nov 16 '05
I'm trying to make a combo box custom control (Windows Forms .NET 1.1
c#) that can behave like a label programatically at run time. This is
actually a strong feature request by our customers. I really don't
want to have to make a UserControl that has both a label and ComboBox
in it. I would rather have my control derive from a ComboBox for
databinding (and other) reasons.

I can get close, I've tried setting

cbo.DropDownStyle = ComboBoxStyle.Simple
cbo.BackColor = System.Drawing.SystemColors.Control (makes it grey)

but this has two problems:

1) It leaves a border (looks like a TextBox now, only with grey
background)
2) It's editable!! I need it to be readonly, like a label. Even though
it doesn't effect the datasource, it's a big usability issue...

I'm doing this for both the ListBox and ComboBox and the ListBox is
easy, because it has a BorderStyle property and I can just fudge with
the height of the listbox... but ComboBox has more properties.

The only viable path I've come across (I don't even know if this will
work) is to override the OnDrawItem method, but I have little
experience doing this. Would this be the correct path? I don't see how
this would fix #2 above... Is there an easier way?

thanks,
dave
Tom Porterfield
Guest
 
Posts: n/a
#2: Nov 16 '05

re: How to make a comboBox behave like a Label at runntime


malcolm wrote:[color=blue]
> I'm trying to make a combo box custom control (Windows Forms .NET 1.1
> c#) that can behave like a label programatically at run time. This is
> actually a strong feature request by our customers. I really don't
> want to have to make a UserControl that has both a label and ComboBox
> in it. I would rather have my control derive from a ComboBox for
> databinding (and other) reasons.
>
> I can get close, I've tried setting
>
> cbo.DropDownStyle = ComboBoxStyle.Simple
> cbo.BackColor = System.Drawing.SystemColors.Control (makes it grey)
>
> but this has two problems:
>
> 1) It leaves a border (looks like a TextBox now, only with grey
> background)
> 2) It's editable!! I need it to be readonly, like a label. Even though
> it doesn't effect the datasource, it's a big usability issue...
>
> I'm doing this for both the ListBox and ComboBox and the ListBox is
> easy, because it has a BorderStyle property and I can just fudge with
> the height of the listbox... but ComboBox has more properties.
>
> The only viable path I've come across (I don't even know if this will
> work) is to override the OnDrawItem method, but I have little
> experience doing this. Would this be the correct path? I don't see how
> this would fix #2 above... Is there an easier way?[/color]

We do it by overriding OnPaint. We add a property called ReadOnly. If
set to true then in OnPaint we create our own rectangle based on the
size of the current control and colored based on the current parent
form. We call FillRectangle on the Graphics object of the passed n
PaintEventArgs and then call DrawString to display the text of the
selected item in the combo box. In this instance we do not call the
base OnPaint as we have handled everything ourselves.

If our ReadOnly property is false then we simply delegate to base.OnPaint.
--
Tom Porterfield
malcolm
Guest
 
Posts: n/a
#3: Nov 16 '05

re: How to make a comboBox behave like a Label at runntime


ah, very clever!

So, I'm assuming by not calling the base.OnPaint, this solves the
issue of being able to see the other items. In other words, your
solution literally makes the control behave like a label when ReadOnly
is set. Brilliant!

Funny, I have something called ReadOnlyState {Never, Always,
OnlyWhenOneRow}

Thanks, I'll give it a try!

-dave

Tom Porterfield <tpporter@mvps.org> wrote in message news:<#nUVqyYpEHA.1176@TK2MSFTNGP12.phx.gbl>...[color=blue]
> malcolm wrote:[color=green]
> > I'm trying to make a combo box custom control (Windows Forms .NET 1.1
> > c#) that can behave like a label programatically at run time. This is
> > actually a strong feature request by our customers. I really don't
> > want to have to make a UserControl that has both a label and ComboBox
> > in it. I would rather have my control derive from a ComboBox for
> > databinding (and other) reasons.
> >
> > I can get close, I've tried setting
> >
> > cbo.DropDownStyle = ComboBoxStyle.Simple
> > cbo.BackColor = System.Drawing.SystemColors.Control (makes it grey)
> >
> > but this has two problems:
> >
> > 1) It leaves a border (looks like a TextBox now, only with grey
> > background)
> > 2) It's editable!! I need it to be readonly, like a label. Even though
> > it doesn't effect the datasource, it's a big usability issue...
> >
> > I'm doing this for both the ListBox and ComboBox and the ListBox is
> > easy, because it has a BorderStyle property and I can just fudge with
> > the height of the listbox... but ComboBox has more properties.
> >
> > The only viable path I've come across (I don't even know if this will
> > work) is to override the OnDrawItem method, but I have little
> > experience doing this. Would this be the correct path? I don't see how
> > this would fix #2 above... Is there an easier way?[/color]
>
> We do it by overriding OnPaint. We add a property called ReadOnly. If
> set to true then in OnPaint we create our own rectangle based on the
> size of the current control and colored based on the current parent
> form. We call FillRectangle on the Graphics object of the passed n
> PaintEventArgs and then call DrawString to display the text of the
> selected item in the combo box. In this instance we do not call the
> base OnPaint as we have handled everything ourselves.
>
> If our ReadOnly property is false then we simply delegate to base.OnPaint.[/color]
malcolm
Guest
 
Posts: n/a
#4: Nov 16 '05

re: How to make a comboBox behave like a Label at runntime


actually, how did you get the OnPaint to even work? It doesn't fire,
and the research I've done implies you must override the DrawItem or
MeasureItem method. Naother method implies you have to override the
WndPrc method! I'm using .NET 1.1 c#. Are you using VB or something?

thanks,
dave

Tom Porterfield <tpporter@mvps.org> wrote in message news:<#nUVqyYpEHA.1176@TK2MSFTNGP12.phx.gbl>...[color=blue]
> malcolm wrote:[color=green]
> > I'm trying to make a combo box custom control (Windows Forms .NET 1.1
> > c#) that can behave like a label programatically at run time. This is
> > actually a strong feature request by our customers. I really don't
> > want to have to make a UserControl that has both a label and ComboBox
> > in it. I would rather have my control derive from a ComboBox for
> > databinding (and other) reasons.
> >
> > I can get close, I've tried setting
> >
> > cbo.DropDownStyle = ComboBoxStyle.Simple
> > cbo.BackColor = System.Drawing.SystemColors.Control (makes it grey)
> >
> > but this has two problems:
> >
> > 1) It leaves a border (looks like a TextBox now, only with grey
> > background)
> > 2) It's editable!! I need it to be readonly, like a label. Even though
> > it doesn't effect the datasource, it's a big usability issue...
> >
> > I'm doing this for both the ListBox and ComboBox and the ListBox is
> > easy, because it has a BorderStyle property and I can just fudge with
> > the height of the listbox... but ComboBox has more properties.
> >
> > The only viable path I've come across (I don't even know if this will
> > work) is to override the OnDrawItem method, but I have little
> > experience doing this. Would this be the correct path? I don't see how
> > this would fix #2 above... Is there an easier way?[/color]
>
> We do it by overriding OnPaint. We add a property called ReadOnly. If
> set to true then in OnPaint we create our own rectangle based on the
> size of the current control and colored based on the current parent
> form. We call FillRectangle on the Graphics object of the passed n
> PaintEventArgs and then call DrawString to display the text of the
> selected item in the combo box. In this instance we do not call the
> base OnPaint as we have handled everything ourselves.
>
> If our ReadOnly property is false then we simply delegate to base.OnPaint.[/color]
Tom Porterfield
Guest
 
Posts: n/a
#5: Nov 16 '05

re: How to make a comboBox behave like a Label at runntime


malcolm wrote:[color=blue]
> actually, how did you get the OnPaint to even work? It doesn't fire,
> and the research I've done implies you must override the DrawItem or
> MeasureItem method. Naother method implies you have to override the
> WndPrc method! I'm using .NET 1.1 c#. Are you using VB or something?[/color]

OnPaint fires when the control is painted. If I were using VB or
something then I wouldn't be posting in a csharp newsgroup.
--
Tom Porterfield
Etienne Boucher
Guest
 
Posts: n/a
#6: Nov 16 '05

re: How to make a comboBox behave like a Label at runntime


I never overrode the combobox myself, so I can't give you any specific help,
but the combobox is a weird control. There is a listbox part and a textbox
part. You might want to look at some articles found at codeproject.com like
this one:

http://www.codeproject.com/cs/combob...pears_flat.asp

Etienne Boucher


Closed Thread