Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Button Click Event Procedure

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    22

    Resolved [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.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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
    VB 2005, Win Xp Pro sp2

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width