• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Button Text

Hello,

Here is the scenario.  I am trying to make a buttton that functions like an aircraft switch/light.  The actuall switch has two lines of text that light up independently.  Is there any way I can simulate this on a regular button?  I tried adding a label but of course I lose the animation of the button click.  I tried adding another button to the button but that didnt look right either.  I need to be able to have two lines of text that can be highlighted different colors.  Any ideas?!?  Thanks in advance for your help!!
0
anitahelp
Asked:
anitahelp
  • 5
  • 4
  • 4
  • +2
1 Solution
 
tzxie2000Commented:
firstly
you can use two label

and write the click action in mouseup event
0
 
Fahad MukhtarDistinguished EngineerCommented:
can you post/upload the picture somewhere of what you want to acheive?
0
 
tzxie2000Commented:
you can also double click the label in IDE
then you will find label also has click event
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
anitahelpAuthor Commented:
tzxie2000:
Mouse up for the label?  Does this somehow need to call the mouse up event of the button?

Desp:
Unfortunately I can't post a picture but it is basically as I stated, example:  Button, line one text = "LAND" line two text = "SEA".  Push the button once and the line one text becomes highlighted, push the button again and both become highlighted, once more and line one turns off and line two is highlighted.
0
 
tzxie2000Commented:
I just found that label also have click event

you can also double click the label in IDE
then you will find label also has click event


0
 
anitahelpAuthor Commented:
I understand that a label has a click event, problem is that you don't have the animation of the button being pressed.
0
 
Fahad MukhtarDistinguished EngineerCommented:
How about having three icons in the same button and use the Image Property of the button to the set appropriate icons stored in an imagelist
0
 
tzxie2000Commented:
how about write this in your button paint event

 Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
        Dim g As Graphics = e.Graphics
        Dim f As Font = New Font("Arial", 8)

        g.DrawString("run", f, System.Drawing.Brushes.Blue, 0, 0)
        g.DrawString("stop", f, System.Drawing.Brushes.Red, 0, 8)

    End Sub
0
 
anitahelpAuthor Commented:
The problem i see with that is the fact that I have a number of these switch lights and they all have different text.  I would need hundreds of images contained within lists.  It is a possibility but since it is text I would prefer to build some sort of control so that I can just set the text properties based on the received values.
0
 
RonaldBiemansCommented:
Try this

call like this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f As New mybutton
        f.Text = "hallo"
        f.text2 = "hallo2"
        f.Height = 40
        f.Width = 100
        f.Top = 100
        f.Left = 100
        f.TextBackcolor1 = Color.Red
        f.TextBackcolor2 = Color.Blue
        AddHandler f.Click, AddressOf Myclick
        Me.Controls.Add(f)
end sub

Private Sub MyClick(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(sender, mybutton).OnOff = Not CType(sender, mybutton).OnOff
        CType(sender, mybutton).Invalidate()
    End Sub


Public Class mybutton
    Inherits Button
    Dim _text2 As String
    Dim _textbackcolor1 As Color
    Dim _textbackcolor2 As Color
    Dim _ONOFF As Boolean = True

    Public Property OnOff() As Boolean
        Get
            Return _ONOFF
        End Get
        Set(ByVal Value As Boolean)
            _ONOFF = Value
        End Set
    End Property

    Public Property text2() As String
        Get
            Return _text2
        End Get
        Set(ByVal Value As String)
            _text2 = Value
        End Set
    End Property

    Public Property TextBackcolor1() As Color
        Get
            Return _textbackcolor1
        End Get
        Set(ByVal Value As Color)
            _textbackcolor1 = Value
        End Set
    End Property

    Public Property TextBackcolor2() As Color
        Get
            Return _textbackcolor2
        End Get
        Set(ByVal Value As Color)
            _textbackcolor2 = Value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(pevent)
        Dim textheight As SizeF = pevent.Graphics.MeasureString(Me.Text, Me.Font)
        If ONOFF Then
            pevent.Graphics.FillRectangle(New SolidBrush(TextBackcolor1), 4, 4, Me.Width - 8, textheight.Height)
            pevent.Graphics.FillRectangle(New SolidBrush(Me.BackColor), 4, textheight.Height + 4, Me.Width - 8, Me.Height - textheight.Height - 10)
        Else
            pevent.Graphics.FillRectangle(New SolidBrush(Me.BackColor), 4, 4, Me.Width - 8, textheight.Height)
            pevent.Graphics.FillRectangle(New SolidBrush(TextBackcolor2), 4, textheight.Height + 4, Me.Width - 8, Me.Height - textheight.Height - 10)
        End If
        pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Color.Black), 4, 4)
        pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(Color.Black), 4, Me.Height - textheight.Height - 8)

    End Sub


End Class
0
 
frodomanCommented:
Why not just use a normal button and change the text based on it's current value?

        If Me.Text = "air" Then
            Me.Text = "sea"
            Me.ForeColor = Color.Blue
        Else
             If Me.Text = "sea" Then
                   ....
             End If
        End If
0
 
RonaldBiemansCommented:
if you just want the front color of the text change color then change to

Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(pevent)
        Dim textheight As SizeF = pevent.Graphics.MeasureString(Me.Text, Me.Font)
        pevent.Graphics.FillRectangle(New SolidBrush(Me.BackColor), 4, 4, Me.Width - 8, Me.Height - 8)
        If OnOff Then
            pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(TextBackcolor1), 4, 4)
            pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(Color.Black), 4, Me.Height - textheight.Height - 8)
        Else
            pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Color.Black), 4, 4)
            pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(TextBackcolor2), 4, Me.Height - textheight.Height - 8)
        End If


    End Sub
0
 
anitahelpAuthor Commented:
RonaldBiemans,

That works great, I wish that it would appear as if the text is being depressed with the switch though.  (while the buttton gives the effect that it is being pressed down the text doesn't move).
0
 
RonaldBiemansCommented:
I'll see what I can do, just a moment
0
 
RonaldBiemansCommented:
this should do it

Public Class mybutton
    Inherits Button
    Dim _text2 As String
    Dim _textbackcolor1 As Color
    Dim _textbackcolor2 As Color
    Dim _ONOFF As Boolean = True
    Dim tclick As Boolean = False

    Public Property OnOff() As Boolean
        Get
            Return _ONOFF
        End Get
        Set(ByVal Value As Boolean)
            _ONOFF = Value
        End Set
    End Property

    Public Property text2() As String
        Get
            Return _text2
        End Get
        Set(ByVal Value As String)
            _text2 = Value
        End Set
    End Property

    Public Property TextBackcolor1() As Color
        Get
            Return _textbackcolor1
        End Get
        Set(ByVal Value As Color)
            _textbackcolor1 = Value
        End Set
    End Property

    Public Property TextBackcolor2() As Color
        Get
            Return _textbackcolor2
        End Get
        Set(ByVal Value As Color)
            _textbackcolor2 = Value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(pevent)
        Dim textheight As SizeF = pevent.Graphics.MeasureString(Me.Text, Me.Font)

        pevent.Graphics.FillRectangle(New SolidBrush(Me.BackColor), 4, 4, Me.Width - 8, Me.Height - 8)
        If tclick Then
            If OnOff Then
                pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(TextBackcolor1), 5, 5)
                pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(Color.Black), 5, Me.Height - textheight.Height - 7)
            Else
                pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Color.Black), 5, 5)
                pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(TextBackcolor2), 5, Me.Height - textheight.Height - 7)
            End If
        Else
            If OnOff Then
                pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(TextBackcolor1), 4, 4)
                pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(Color.Black), 4, Me.Height - textheight.Height - 8)
            Else
                pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Color.Black), 4, 4)
                pevent.Graphics.DrawString(Me.text2, Me.Font, New SolidBrush(TextBackcolor2), 4, Me.Height - textheight.Height - 8)
            End If
        End If
        tclick = True

    End Sub

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        tclick = False
        MyBase.OnClick(e)
    End Sub
End Class
0
 
anitahelpAuthor Commented:
Works like a charm!  Thanks for your time and effort, I truely appreciate it!!!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now