|
-
Apr 22nd, 2007, 09:54 AM
#1
Thread Starter
Junior Member
[RESOLVED] [2005] Button Click Event Procedure
When the button is pressed the name VB 2005 should disappear and the caption should change to "show name of Language", and when the button is clicked again the VB 2005 appears again and the button caption reverts to Hide name of Language, I got the first part to work but reverting the process is not working
Private Sub btnDisplay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDisplay.Click
If btnDisplay.Text = "Hide name of Language" Then
lblLanguage.Hide()
btnDisplay.Text = "Show name of Language"
Else
lblLanguage.Show()
btnDisplay.Text = "Hide name of Language"
End If
End Sub
Last edited by peterdfl; Apr 22nd, 2007 at 08:12 PM.
-
Apr 22nd, 2007, 10:27 AM
#2
Re: [2005] Button Click Event Procedure
That's because buttonClicked is never False. As soon as the button clicked event happens it's assigned to True every time
-
Apr 22nd, 2007, 10:30 AM
#3
Re: [2005] Button Click Event Procedure
You never set buttonclicked to false. Also it should have a greater scope, in your version it is always being set to True whenever you click the button.
Code:
Dim buttonclicked As Boolean = True
Private Sub btnDisplay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDisplay.Click
If buttonclicked = True Then
lblLanguage.Hide()
btnDisplay.Text = "Show name of Language"
buttonclicked = False
ElseIf buttonclicked = False Then
lblLanguage.Visible=True
btnDisplay.Text = "Hide name of Language"
buttonclicked = True
End If
End Sub
You can also solve it in a different way without a boolean state check - compare the text of the button and switch to the other option:
Code:
Private Sub btnDisplay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDisplay.Click
If btnDisplay.Text = "Hide name of Language" Then
lblLanguage.Hide()
btnDisplay.Text = "Show name of Language"
ElseIf btnDisplay.Text = "Show name of Language" Then
lblLanguage.Visible=True
btnDisplay.Text = "Hide name of Language"
End If
End Sub
-
Apr 22nd, 2007, 10:31 AM
#4
Re: [2005] Button Click Event Procedure
Your variable buttonclicked will always be true whenever the button is clicked, because it created new every time.
To make it work, you can do a couple of things. The first thing you can do is to change the declaration to:
Code:
Static buttonclicked As Boolean
This will ensure that the value of buttonclicked is remembered between clicks.
or you can remove the declaration from the button event handler and declare it at the form level instead.
EDIT: I am way too slow at typing.
Last edited by Andy_P; Apr 22nd, 2007 at 10:34 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|