Cannot change text of button on a control.
-
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.
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 -
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.
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?
-
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.
-
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 KreskowiakStill doesn't work.
-
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?
It is there... it should be under Form1. This is Twitter client that I am working on.
-
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 Subnope. not working.
-
Still doesn't work.
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 -
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 KreskowiakThat worked beautifully :D BIG THANKS M8!
-
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 ifOke 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 ifEDIT: 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.
-
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 ifEDIT: 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.
Thanks Nick. But the issue has already been resolved :)