Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Cannot change text of button on a control.

Cannot change text of button on a control.

Scheduled Pinned Locked Moved Visual Basic
24 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Ammar_Ahmad

    Still doesn't work.

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #21

    I have no idea what you're doing wrong because this works fine. I wiped up this little example in about a minute:

    Private Sub TestButton\_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TestButton.MouseDown
        ChangeButtonText(TestButton)
    End Sub
    
    Private Sub TestButton\_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TestButton.MouseUp
        ChangeButtonText(TestButton)
    End Sub
    
    Private Sub ChangeButtonText(ByRef target As Button)
        If target.Text = "0" Then
            target.Text = "1"
        Else
            target.Text = "0"
        End If
    End Sub
    

    If you want the button to change text like a toggle, just remove the MouseUp handler. Truthfully, I'd be creating my own button control to wrap this functionality and overriding the OnMouseDown and OnMouseUp methods to do this. I wouldn't be doing it using event handlers. But, since this method is easier to understand, here you go...

    A guide to posting questions on CodeProject[^]
    Dave Kreskowiak

    A 1 Reply Last reply
    0
    • D Dave Kreskowiak

      I have no idea what you're doing wrong because this works fine. I wiped up this little example in about a minute:

      Private Sub TestButton\_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TestButton.MouseDown
          ChangeButtonText(TestButton)
      End Sub
      
      Private Sub TestButton\_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TestButton.MouseUp
          ChangeButtonText(TestButton)
      End Sub
      
      Private Sub ChangeButtonText(ByRef target As Button)
          If target.Text = "0" Then
              target.Text = "1"
          Else
              target.Text = "0"
          End If
      End Sub
      

      If you want the button to change text like a toggle, just remove the MouseUp handler. Truthfully, I'd be creating my own button control to wrap this functionality and overriding the OnMouseDown and OnMouseUp methods to do this. I wouldn't be doing it using event handlers. But, since this method is easier to understand, here you go...

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      A Offline
      A Offline
      Ammar_Ahmad
      wrote on last edited by
      #22

      That worked beautifully :D BIG THANKS M8!

      1 Reply Last reply
      0
      • A Ammar_Ahmad

        I made a control which has a button on it. But I need to change the text of it when it is clicked. this is what I have tried but it didn't work:

        If Button1.Text = "1" then
        Button1.Text = "0"
        Elseif Button1.Text = "0" then
        Button1.Text = "1"
        End if

        N Offline
        N Offline
        Nick Otten
        wrote on last edited by
        #23

        Oke i know this sounds a bit basic but have you checked for spaces? strings are quite picky with those. "0 " is not the same as "0" for them. tough its likely a human will read over that last space. you could add a replace to the .text to get rid of this problem.

        If Button1.Text.Replace(" ","") = "1" then
        Button1.Text = "0"
        Elseif Button1.Text.Replace(" ","") = "0" then
        Button1.Text = "1"
        End if

        EDIT: oke reading some more and i hear some rumors that your using several threads. if this thing is executed in a thread you might want to enforce the update to your gui.

        Private Sub updateUI()
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf updateUI))
            Else
                Me.Update()
            End If
        End Sub
        

        if your using that button thing in a sub/background thread then add the above sub and try calling it after the button event is done. be aware that this isn't good code and will enforce a full refresh of your screen, its a nice way to test tough.

        A 1 Reply Last reply
        0
        • N Nick Otten

          Oke i know this sounds a bit basic but have you checked for spaces? strings are quite picky with those. "0 " is not the same as "0" for them. tough its likely a human will read over that last space. you could add a replace to the .text to get rid of this problem.

          If Button1.Text.Replace(" ","") = "1" then
          Button1.Text = "0"
          Elseif Button1.Text.Replace(" ","") = "0" then
          Button1.Text = "1"
          End if

          EDIT: oke reading some more and i hear some rumors that your using several threads. if this thing is executed in a thread you might want to enforce the update to your gui.

          Private Sub updateUI()
              If Me.InvokeRequired Then
                  Me.Invoke(New MethodInvoker(AddressOf updateUI))
              Else
                  Me.Update()
              End If
          End Sub
          

          if your using that button thing in a sub/background thread then add the above sub and try calling it after the button event is done. be aware that this isn't good code and will enforce a full refresh of your screen, its a nice way to test tough.

          A Offline
          A Offline
          Ammar_Ahmad
          wrote on last edited by
          #24

          Thanks Nick. But the issue has already been resolved :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups