| 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] |