PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Command button caption problem-VBForums
Results 1 to 8 of 8

Thread: Command button caption problem

  1. #1

    Thread Starter
    Member stingran's Avatar
    Join Date
    Dec 2004
    Posts
    58

    Red face Command button caption problem

    hi all

    i have a command button with caption = "V".
    when i click, i want to toggle between "v" and "X", so here
    is what i wrote in the code:

    VB Code:
    1. Private Sub cmdFillAgain_Click()
    2.     If gFillAgain = True Then
    3.         cmdFillAgain.Caption = "X"
    4.     Else
    5.         cmdFillAgain.Caption = "V"
    6.     End If
    7.     gFillAgain = Not gFillAgain
    8. End Sub

    but in the first time i click the button, the Caption remains "x" even though
    the gFillAgain variable is changing as it should.. it looks like only the first time the button is clicked doesnt work, because after that i have no problems...

    any idea???
    thanks

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Command button caption problem

    Quote Originally Posted by stingran
    hi all

    i have a command button with caption = "V".
    when i click, i want to toggle between "v" and "X", so here
    is what i wrote in the code:

    VB Code:
    1. Private Sub cmdFillAgain_Click()
    2.     If gFillAgain = True Then
    3.         cmdFillAgain.Caption = "X"
    4.     Else
    5.         cmdFillAgain.Caption = "V"
    6.     End If
    7.     gFillAgain = Not gFillAgain
    8. End Sub



    but in the first time i click the button, the Caption remains "x" even though
    the gFillAgain variable is changing as it should.. it looks like only the first time the button is clicked doesnt work, because after that i have no problems...

    any idea???
    thanks
    I am assuming that gFillAgain is a Boolean Variable. This code worked fine for me:


    VB Code:
    1. Option Explicit
    2. Dim gFillAgain as Boolean
    3.  
    4. Private Sub Form_Load()
    5.       gFillAgain = True
    6.       cmdFillAgain.Caption = "V"
    7. End Sub
    8.  
    9. Private Sub cmdFillAgain_Click()
    10.     If gFillAgain Then
    11.         cmdFillAgain.Caption = "X"
    12.     Else
    13.         cmdFillAgain.Caption = "V"
    14.     End If
    15.     gFillAgain = Not gFillAgain
    16. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3

    Thread Starter
    Member stingran's Avatar
    Join Date
    Dec 2004
    Posts
    58

    Re: Command button caption problem

    yes, this variable is defined as a boolean in a module:
    [public gFillAgain as boolean]

    and in Form Load i assign in to TRUE.

    so why doesnt it work for me?

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Command button caption problem

    In your code, just move the re-assignment from the bottom of the proc to the top:
    VB Code:
    1. Private Sub cmdFillAgain_Click()
    2.     [b]gFillAgain = Not gFillAgain[/b]
    3.     If gFillAgain = True Then
    4.         cmdFillAgain.Caption = "X"
    5.     Else
    6.         cmdFillAgain.Caption = "V"
    7.     End If
    8.     'gFillAgain = Not gFillAgain
    9. End Sub
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Member stingran's Avatar
    Join Date
    Dec 2004
    Posts
    58

    Re: Command button caption problem

    indeed!!!! it is working...
    but why is it needed to assign the valua BEFORE???
    is there a logic explenation?

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Command button caption problem

    It just seemed, whatever you were doing, didnt work the first time, which meant the conditional statement wasn't to what it should be at the start. To show you, set the default caption of the button to "V". Then run it using your first lot of code.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,612

    Re: Command button caption problem

    You could eleminate an extra unnecessary variable and achieve the same in one line also like this:

    VB Code:
    1. Private Sub cmdFillAgain_Click()
    2.     cmdFillAgain.Caption = IIf(cmdFillAgain.Caption = "X", "V", "X")
    3. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Command button caption problem

    When you assigned the value, it called the routine, which switched the value. I had that happen to me. I always set the initial value to the opposite of what I wanted in the form load.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width