dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: If statement problem.....

  1. #1
    Join Date
    Nov 2006
    Posts
    36

    Question If statement problem.....

    For example i have 2 textbox, button called save and another button called exit.....now want to do and if statement, that states if the textbox is changed and button save is not press when user wants to exit a msgbox will appear.

    The problem is the condition for the if statement........i have tried

    if text1_change or text2_change and not save_click then
    ........
    ......
    end if

    But an error highlights the text1_change....what wrong here ?
    Any other alternative coding ?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: If statement problem.....

    Put a Module level boolean variable which you can set to true or false in the Change event of the text boxes. Then in the save button you can set it back to true or false.

    And in the the Exit button's Click event you can check this Variable and show the message box depending upon if it is true or false.

  3. #3
    Join Date
    Nov 2006
    Posts
    36

    Re: If statement problem.....

    I am sorry Suhja...but i don`t understand what you mean by "a Module level boolean variable". Can you explain ? Thanks

  4. #4
    Join Date
    Jul 2007
    Posts
    71

    Re: If statement problem.....

    Create a new boolean variable on the gentral declaration.

    use that variable inside your Text_Change event. when you change the text , make that variable to true and then handle that variable in your buttons. In the button event you can check the variable is true or false.
    after the button event handled again make the variable false.
    Last edited by pragashan; July 20th, 2007 at 05:14 AM.

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: If statement problem.....

    Here is a sample code
    Code:
    Option Explicit
    Dim bTextChanged As Boolean
    
    Private Sub cmdExit_Click()
        'if text is changed and save button was not clicked
        If bTextChanged Then
            'show a msgbox to confirm if user wants to exit without saving
            If MsgBox("Your changes are not saved, do you want to Exit?", vbYesNo + vbQuestion) = vbYes Then
                Unload Me
            End If
        Else
            'unload the form
            Unload Me
        End If
    End Sub
    
    Private Sub cmdSave_Click()
        'code for saving the text boxes
        'set the boolean as false
        bTextChanged = False
    End Sub
    
    Private Sub Form_Load()
        'first time load, nothing will be changed
        bTextChanged = False
    End Sub
    
    Private Sub Text1_Change()
        'text in textbox1 has been changed
        bTextChanged = True
    End Sub
    
    Private Sub Text2_Change()
        'text in textbox2 has been changed
        bTextChanged = True
    End Sub

  6. #6
    Join Date
    Nov 2006
    Posts
    36

    Re: If statement problem.....

    Oh....thanks alot to Shuja and pragashan !...

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)