Trying to make User Control

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'm trying to make a user control that I'll cal a DigitalDisplay. I've used GDI+ architect to design what the control looks like, but I can't figure out what I need to do to set up the properties for the control. I want the user to be able to change the color and the text. Can someone show me some code that will allow me to do this? Here is what my control looks like.'

1055764588_fa5668cf5f.jpg
 
The user control already have ForeColor and BackColor properties, you can write a property for DisplayText yourself (just write "property" and press Tab).
Control type also have a Text property that the UserControl hides, but you can overrides it if you want, then you must also set its Browsable attribute on.
 
You can for example call for Refresh/Invalidate in the property setter to immediately have the control repaint with new color. In such case you could Override the default ForeColor/BackColor or write your own properties. The easy way to override a property is to write "overrides " and select it in the list.
 
Thanks, John. One of my questions is, how do I use the text property? I'm not sure how to format that. I tried something like this:
VB.NET:
Public Property Text() As String
        Get
            Return mytext
        End Get
        Set(ByVal value As String)
            mytext = value
        End Set
    End Property

It gives me this message:
Warning 1 property 'Text' shadows an overridable method in the base class 'UserControl'. To override the base method, this method must be declared 'Overrides'. C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\DigitalDisplay\DigitalDisplay\UserControl1.vb 114 21 DigitalDisplay

I don't want to override anything, I just want to use the built in Text property.
 
For some controls won't you have to write custom paint code for when the forecolor and backcolor changes depending on which type of control you made.

Yep. I may be doing this the clumsy way, but this is what I have:

VB.NET:
Private Sub UserControl1_ForeColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ForeColorChanged
        bcolor = Me.ForeColor
       RenderGraphics(g)
    End Sub

RenderGraphics() is a sub with all of the GDI drawing involved in making the control.
 
I don't want to override anything, I just want to use the built in Text property.
To do that you override it. You must also apply the Browsable attribute to unhide in properties window as explained.
VB.NET:
    <System.ComponentModel.Browsable(True)> _
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property
Since UserControl has hidden this property (and done who knows else to it) you should use Reflector to see if there could be a problem using it. One would think they had set NotOverridable in that case, though. You could also write your own property for example DisplayText that you know would not conflict anything.

And use the Paint event to draw with e.Graphics, no drawing with CreateGraphics.
 
Back
Top