Custom Button Control

GrnEyedDvl

Member
Joined
Dec 24, 2009
Messages
6
Programming Experience
Beginner
I am building a custom user control (button) that works fine with the added properties, with one exception. It fails to load the image selected via the new property when the form starts up, but all the mouse events (click, enter, leave, etc)

Basically there are several new properties:

Orientation, can be one of the following:
UpperLeft
UpperRight
LowerLeft
LowerRight


These will be one of several colors, currently I have 3.
MainColor
MouseColor
ClickColor


For the mouse events I do something like this to swap out the images. I can stick all that in a module later and just pull the variable but for how this is fine.
VB.NET:
    Private Sub Elbo_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
        Me.ForeColor = colTextMouseColor

        'detect MouseOver color
        'Orientation UpperLeft
        If Me.Orientation = ElboOrientation.UpperLeft Then

            'Blue section
            If Me.MouseOverColor = ElboColor.Blue Then
                imgMouseOver = My.Resources.UL_Blue

            ElseIf Me.MouseOverColor = ElboColor.LightBlue Then
                imgMouseOver = My.Resources.UL_LightBlue

            ElseIf Me.MouseOverColor = ElboColor.RoyalBlue Then
                imgMouseOver = My.Resources.UL_RoyalBlue
            End If


            'Orientation UpperRight
        ElseIf Me.Orientation = ElboOrientation.UpperRight Then

            'Blue section
            If Me.MouseOverColor = ElboColor.Blue Then
                imgMouseOver = My.Resources.UR_Blue

            ElseIf Me.MouseOverColor = ElboColor.LightBlue Then
                imgMouseOver = My.Resources.UR_LightBlue

            ElseIf Me.MouseOverColor = ElboColor.RoyalBlue Then
                imgMouseOver = My.Resources.UR_RoyalBlue
            End If

            'orientation LowerLeft
        ElseIf Me.Orientation = ElboOrientation.LowerLeft Then

            'Blue section
            If Me.MouseOverColor = ElboColor.Blue Then
                imgMouseOver = My.Resources.BL_Blue

            ElseIf Me.MouseOverColor = ElboColor.LightBlue Then
                imgMouseOver = My.Resources.BL_LightBlue

            ElseIf Me.MouseOverColor = ElboColor.RoyalBlue Then
                imgMouseOver = My.Resources.BL_RoyalBlue
            End If

            'orientation LowerRight
        ElseIf Me.Orientation = ElboOrientation.LowerRight Then

            'Blue section
            If Me.MouseOverColor = ElboColor.Blue Then
                imgMouseOver = My.Resources.BR_Blue

            ElseIf Me.MouseOverColor = ElboColor.LightBlue Then
                imgMouseOver = My.Resources.BR_LightBlue

            ElseIf Me.MouseOverColor = ElboColor.RoyalBlue Then
                imgMouseOver = My.Resources.BR_RoyalBlue
            End If
        End If
        Me.BackgroundImage = imgMouseOver
        Me.ForeColor = colTextMouseColor
    End Sub


That works just fine, and then on MouseLeave it sets it back to the main color selected via the property sheet in design mode. All of the mouse events work just fine.

The problem is here:
VB.NET:
    Public Sub New()
        Me.Size = New Point(177, 55)
        Me.FlatStyle = Windows.Forms.FlatStyle.Flat
        Me.FlatAppearance.BorderSize = 0
        Me.FlatAppearance.BorderColor = Color.Black
        Me.FlatAppearance.MouseDownBackColor = Color.Transparent
        Me.FlatAppearance.MouseOverBackColor = Color.Transparent
        Me.BackColor = Color.Transparent
        Me.BackgroundImageLayout = Windows.Forms.ImageLayout.Center
        Me.TextAlign = ContentAlignment.BottomCenter

        'need to determine color at form load ??
        [COLOR="Red"]Me.BackgroundImage = My.Resources.UL_LightBlue[/COLOR]
        

    End Sub

That red line is the one that loads the image when you place the control on a form, and is also the image that loads when the application is started.

If I replace that line with an if statement to similar to the one for MouseEnter above, it will not pull the info and select the proper image when the application loads. It loads a blank image, when I mouse over it it puts the proper mouse over image, and when I leave it loads the proper main image.

Any ideas? Building controls is fairly new for me so I am sure this is pretty obvious, but its eluding me.
 
I think you are going to have to overide the BackgroundImage property - which you can, and set it to a private variable that points to your Image which will make it the default image.
 
That doesnt work.

If I replace that line with an if statement to similar to the one for MouseEnter above, it will not pull the info and select the proper image when the application loads. It loads a blank image, when I mouse over it it puts the proper mouse over image, and when I leave it loads the proper main image.


As I said in the first post I need a way to force the selection at form_load or when the control is first painted. It doesnt matter whether you use Me.BackgroundImage = imgMainColor or MyBase.BackgroundImage = imgMainColor.

EDIT: However I figured it out. Placing the statement that sets the image in Public Sub New() does not work, placing it inside (or Calling it from) the Public Property Get function does.
 
Last edited:
Well it did work - it set the image in the designer, which is what I thought you meant. You can any control's property at runtime, anytime, anywhere, anyhow - thanks for playing.
 
Here is the first version of the control if anyone is interested. I am building a StarTrek LCARS interface to be used on a touch screen monitor.
 

Attachments

  • Release.zip
    65.1 KB · Views: 36
Back
Top