VB 2010 - Action based on button text

Posted on 2011-10-26
Last Modified: 2012-05-12
I originally learned VB using VB6 and I am now learning VB 2010 and I learned a trick during VB6 class that I can't seem to repeat or remember the correct format. I have button1 with the text "ENTER" and when I click the button I want an action to occur, like the background changing color. Also, when the button is clicked I want the text to change on the button to "FLIP." Then when the user clicks the button it executes a different action because the text in the button is "FLIP" not "ENTER." The code below shows you what I am trying to do but it will not go work. I basically want one button to do two things and I used to be able to do it but I can't remember how.

 If Button1.Text = "ENTER" Then
            Me.BackColor = Color.Brown
            Button1.Text = "FLIP"

        ElseIf Button1.Text = "FLIP" Then
            Me.BackColor = Color.Gold
            Button1.Text = "ENTER"

        End If
Question by:Matthew_B
    LVL 69

    Accepted Solution

    it should be working.
    LVL 69

    Assisted Solution

    by:Éric Moreau
    I just tested it and it is working (as long as your initial text value is either ENTER or FLIP)
    LVL 1

    Assisted Solution

    That code should let you perform different actions depending on the text of button. However if you are trying to change the backcolor it won't work this way,
    Also where are you placing the code. It should be placed in the function invoked on button click for instance:-

     Protected Sub btnExchange_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExchange.Click
            If btnExchange.Text = "Exchange" Then
                btnExchange.Text = "1"
            ElseIf 1 = 1 Then
                btnExchange.Text = "Exchange"
            End If
        End Sub
    LVL 83

    Assisted Solution

    One change you may want to do is

    If Button1.Text.ToUpper = "ENTER" Then
                Me.BackColor = Color.Brown
                Button1.Text = "FLIP"

            ElseIf Button1.Text.ToUpper = "FLIP" Then
                Me.BackColor = Color.Gold
                Button1.Text = "ENTER"

            End If

    Author Comment

    I sincerely apologize for the confusion. I have no idea what had happened, but when I was working at home the code just would NOT work. I cam to work and tried it again after reading everyone's replies and it worked fine. I must have left out a statement on accident. Either that or I should stop writing code in the middle of the night.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now