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

    I put that code on a button_click event in a UserControl that I made. It is supposed to work as a boolean. eg if the value is 1 then it should change it to 0 else if the value is 0 it should change it to 1.

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

    The click event is not going to work for this. Click is fired after the button is pushed AND released. Similar code has to be called by the MouseDown, MouseUp, KeyDown and KeyUp handlers. The xDown events fire when the button is pushed down and the mouse or key is still being held down. The xUp events fire when the mouse or key is released.

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

    J A 2 Replies Last reply
    0
    • D Dave Kreskowiak

      The click event is not going to work for this. Click is fired after the button is pushed AND released. Similar code has to be called by the MouseDown, MouseUp, KeyDown and KeyUp handlers. The xDown events fire when the button is pushed down and the mouse or key is still being held down. The xUp events fire when the mouse or key is released.

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

      J Offline
      J Offline
      javadadabi
      wrote on last edited by
      #5

      Hello before selecting another event to let your code, make a breakpoint after bottun_click() line,run your control in dubug mode check if break point stop your program,therefore event has no problem,then check your condition state.

      D A 2 Replies Last reply
      0
      • J javadadabi

        Hello before selecting another event to let your code, make a breakpoint after bottun_click() line,run your control in dubug mode check if break point stop your program,therefore event has no problem,then check your condition state.

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

        I think you meant to reply to the OP.

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

        1 Reply Last reply
        0
        • J javadadabi

          Hello before selecting another event to let your code, make a breakpoint after bottun_click() line,run your control in dubug mode check if break point stop your program,therefore event has no problem,then check your condition state.

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

          I added breakpoint on button1_click That event does get fired! And the other code that is in that event gets fired up correctly. The problem it that, that it doesn't change the text of button1. Here I have attached the source code as well: http://www.mediafire.com/?onhramkfh4zy6zx[^]

          1 Reply Last reply
          0
          • D Dave Kreskowiak

            The click event is not going to work for this. Click is fired after the button is pushed AND released. Similar code has to be called by the MouseDown, MouseUp, KeyDown and KeyUp handlers. The xDown events fire when the button is pushed down and the mouse or key is still being held down. The xUp events fire when the mouse or key is released.

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

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

            Button1_click gets fired. But the text of button1 isn't changing... Here I have added the source code: http://www.mediafire.com/?onhramkfh4zy6zx[^] Can you please look into it?

            1 Reply Last reply
            0
            • A Ammar_Ahmad

              I put that code on a button_click event in a UserControl that I made. It is supposed to work as a boolean. eg if the value is 1 then it should change it to 0 else if the value is 0 it should change it to 1.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #9

              If you mean the Button1 handler should modify the Button1 text, then I see no problem, that works just fine. Mind you, you will see the results only after the Click handler has finished (unless you add a call to Refresh). So changing the text initially, performing a long operation, then changing the text back to its original, all in the one Click handler, will not show any change at all. But then, you shouldn't be doing long operations inside a handler on the GUI thread to begin with... Threads (and BackgroundWorkers) were invented to decouple long operations from the GUI thread. :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              A 1 Reply Last reply
              0
              • L Luc Pattyn

                If you mean the Button1 handler should modify the Button1 text, then I see no problem, that works just fine. Mind you, you will see the results only after the Click handler has finished (unless you add a call to Refresh). So changing the text initially, performing a long operation, then changing the text back to its original, all in the one Click handler, will not show any change at all. But then, you shouldn't be doing long operations inside a handler on the GUI thread to begin with... Threads (and BackgroundWorkers) were invented to decouple long operations from the GUI thread. :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                I did add refresh() at the end... but that didn't work :(

                L 1 Reply Last reply
                0
                • A Ammar_Ahmad

                  I did add refresh() at the end... but that didn't work :(

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #11

                  Then you are doing something wrong. Maybe you have more than one Button1 in your project, and you're talking to the wrong one (it helps to use meaningful names). Maybe its initial text isn't neither "0" nor "1". Maybe ... You will need to provide a more comprehensive description of what you have if you want to get more effective help around here. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  A 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Then you are doing something wrong. Maybe you have more than one Button1 in your project, and you're talking to the wrong one (it helps to use meaningful names). Maybe its initial text isn't neither "0" nor "1". Maybe ... You will need to provide a more comprehensive description of what you have if you want to get more effective help around here. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                    I actually uploaded the entire source code of the application over here: http://www.mediafire.com/?onhramkfh4zy6zx[^] I think I am doing everything right... can you please check what I am doing wrong?

                    L 1 Reply Last reply
                    0
                    • A Ammar_Ahmad

                      I actually uploaded the entire source code of the application over here: http://www.mediafire.com/?onhramkfh4zy6zx[^] I think I am doing everything right... can you please check what I am doing wrong?

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #13

                      Sorry, I am not going to download, unpack, and investigate a bunch of code, and neither would anyone else around here. If you want effective help, put some effort in describing the problem properly and provide the relevant pieces of code (inside your message, and using PRE tags), not just all of it. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      A 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Sorry, I am not going to download, unpack, and investigate a bunch of code, and neither would anyone else around here. If you want effective help, put some effort in describing the problem properly and provide the relevant pieces of code (inside your message, and using PRE tags), not just all of it. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                        Alright. So here is the exact issue with all the pictures and everything. I have a Form which has a FlowLayoutPanel, which has a UserControl on it. That Usercontrol has a Button on it. The initial text of that Button is "0" But I want to change that to "1" if it is a "0" and change it back to "0" if it is a "1" So this is the code I used on the UserControl which has the Button on it:

                        Public Class UserControl1

                        Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                            If Button1.Text = "0" Then
                                Button1.Text = "1"
                            ElseIf Button1.Text = "1" Then
                                Button1.Text = "0"
                            End If
                        End Sub
                        

                        End Class

                        But when I click on Button1, the event does get fired but the text of the Button1, that is on the UserControl which is drawn on FlowLayoutPanel doesn't get changed.

                        D J A 3 Replies Last reply
                        0
                        • A Ammar_Ahmad

                          Alright. So here is the exact issue with all the pictures and everything. I have a Form which has a FlowLayoutPanel, which has a UserControl on it. That Usercontrol has a Button on it. The initial text of that Button is "0" But I want to change that to "1" if it is a "0" and change it back to "0" if it is a "1" So this is the code I used on the UserControl which has the Button on it:

                          Public Class UserControl1

                          Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                              If Button1.Text = "0" Then
                                  Button1.Text = "1"
                              ElseIf Button1.Text = "1" Then
                                  Button1.Text = "0"
                              End If
                          End Sub
                          

                          End Class

                          But when I click on Button1, the event does get fired but the text of the Button1, that is on the UserControl which is drawn on FlowLayoutPanel doesn't get changed.

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

                          First, you don't need ElseIf in this case. It should just be:

                          If Button1.Text = "0" Then
                              Button1.Text = "1"
                          Else
                              Button1.Text = "0"
                          End If
                          

                          Second, Button1 is a horrible name for a button. Change it to something meaningful, like "OkButton", so you know without thinking which button it's referring to on your form.

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

                          A 1 Reply Last reply
                          0
                          • A Ammar_Ahmad

                            Alright. So here is the exact issue with all the pictures and everything. I have a Form which has a FlowLayoutPanel, which has a UserControl on it. That Usercontrol has a Button on it. The initial text of that Button is "0" But I want to change that to "1" if it is a "0" and change it back to "0" if it is a "1" So this is the code I used on the UserControl which has the Button on it:

                            Public Class UserControl1

                            Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                If Button1.Text = "0" Then
                                    Button1.Text = "1"
                                ElseIf Button1.Text = "1" Then
                                    Button1.Text = "0"
                                End If
                            End Sub
                            

                            End Class

                            But when I click on Button1, the event does get fired but the text of the Button1, that is on the UserControl which is drawn on FlowLayoutPanel doesn't get changed.

                            J Offline
                            J Offline
                            javadadabi
                            wrote on last edited by
                            #16

                            Dear ammar ahmad there was no user control name usercontrol1 in your solution. I saw your coding,you are more expert than me. I didnt run your program at all. Would you please tell me what this code is for?

                            A 1 Reply Last reply
                            0
                            • A Ammar_Ahmad

                              Alright. So here is the exact issue with all the pictures and everything. I have a Form which has a FlowLayoutPanel, which has a UserControl on it. That Usercontrol has a Button on it. The initial text of that Button is "0" But I want to change that to "1" if it is a "0" and change it back to "0" if it is a "1" So this is the code I used on the UserControl which has the Button on it:

                              Public Class UserControl1

                              Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                  If Button1.Text = "0" Then
                                      Button1.Text = "1"
                                  ElseIf Button1.Text = "1" Then
                                      Button1.Text = "0"
                                  End If
                              End Sub
                              

                              End Class

                              But when I click on Button1, the event does get fired but the text of the Button1, that is on the UserControl which is drawn on FlowLayoutPanel doesn't get changed.

                              A Offline
                              A Offline
                              Ally5
                              wrote on last edited by
                              #17

                              Try changing the sub to:

                              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                              If Button1.Text = "0" Then
                              Button1.Text = "1"
                              Else
                              Button1.Text = "0"
                              End If
                              End Sub

                              A 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                First, you don't need ElseIf in this case. It should just be:

                                If Button1.Text = "0" Then
                                    Button1.Text = "1"
                                Else
                                    Button1.Text = "0"
                                End If
                                

                                Second, Button1 is a horrible name for a button. Change it to something meaningful, like "OkButton", so you know without thinking which button it's referring to on your form.

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

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

                                Still doesn't work.

                                D 1 Reply Last reply
                                0
                                • J javadadabi

                                  Dear ammar ahmad there was no user control name usercontrol1 in your solution. I saw your coding,you are more expert than me. I didnt run your program at all. Would you please tell me what this code is for?

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

                                  It is there... it should be under Form1. This is Twitter client that I am working on.

                                  1 Reply Last reply
                                  0
                                  • A Ally5

                                    Try changing the sub to:

                                    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                    If Button1.Text = "0" Then
                                    Button1.Text = "1"
                                    Else
                                    Button1.Text = "0"
                                    End If
                                    End Sub

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

                                    nope. not working.

                                    1 Reply Last reply
                                    0
                                    • 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
                                          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