[RESOLVED] Can a menu bar cause validation?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: [RESOLVED] Can a menu bar cause validation?

  1. #1
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    [RESOLVED] Can a menu bar cause validation?

    Hello again,

    My project's tester just found that while my infamous custom controls properly fire their validation methods when he saves data from a toolbar button, the menu command to save a record does NOT fire validation.

    As the title says: How do I set a menu command to fire a control's validation event?

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Can a menu bar cause validation?

    Should we guess what you've done in the control and menu? How about some code?
    Code:
    ' and ALWAYS
    '    remember
    '         code
    '              tags
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Can a menu bar cause validation?

    I don't think this is possible unless anyone proves me wrong. What you can do is to perform the validations before allowing to save after clicking the menu...

  4. #4
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    Re: Can a menu bar cause validation?

    I had forgotten about this until today, when we got a defect report that led to the same issue: Controls that require going through their Validate event do not fire Validate when the user clicks on the menu bar (such as the classic File|Save).

    The menu bar was made in VB 6 through the form editor in Visual Studio.

    And my latest searches agree with dee-u, there appears to be no way to have the menu cause validation.

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Can a menu bar cause validation?

    There is a click event for each menu item, so I suppose you could use that to trigger the Validate event. There you could set focus to a control which doesn't require validation, such as a button.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    Re: Can a menu bar cause validation?

    Returning to this issue one more time, now with a code extract.

    Here is my handler:
    Code:
    '**
    'Menu Click Event Handler
    Private Sub MnuSave_Click()
    On Error Resume Next
    'Manually fire validation for the current control
    'If validation passes, call ToolAction
        SendKeys "{TAB}" 'Does not actually pass focus or fire control validation
        Call m_objForm.ToolAction(tbrBntSAVE_REC)
    End Sub
    The call to SendKeys does not actually move focus to another control.

    Would it help if I did something like:
    dim ctlTemp as Control
    set ctlTemp = Me{form}.ActiveControl
    Me.ActiveControl = (some other control -- this line needs work)
    if ctlTemp = Me.ActiveControl then
    .... do the save processing
    end if

  7. #7
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Can a menu bar cause validation?

    How about trying it out and perhaps share us the results?

  8. #8
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    Re: Can a menu bar cause validation?

    I gave it a shot and had two issues.

    1. I was trying to use pointer comparisons in VB6; I can't do an equality comparison between control objects directly. I'll have to use control.Name and control.Index instead.

    2. The line I tagged as needing work needs work. ActiveControl is read-only, and I didn't find a quick way to manipulate the Controls collection to move focus. Any suggestions?

  9. #9
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Can a menu bar cause validation?

    Try the SetFocus method for a control instead of SendKeys. Or you can change the focus via API.

    Try evaluating objects with the Is operator instead of the equals sign.

    BTW, top level menus have a click event too, so you can use one event to do the change in focus for all the items in that menu's list. You could also make a sub to pass focus, and simply call it from whatever menu click events you need.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  10. #10
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    Re: Can a menu bar cause validation?

    Quote Originally Posted by WizBang View Post
    Try the SetFocus method for a control instead of SendKeys. Or you can change the focus via API.
    The question is, how do I pick the control to use for ctl.SetFocus?

    Edited to add: The Is operator worked properly for the comparison. Thanks; that's part of the issue resolved.
    Last edited by ke3om; December 11th, 2008 at 01:19 PM.

  11. #11
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Can a menu bar cause validation?

    SPY++ will give you addresses of any contol. It's built into VB6 IDE
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Can a menu bar cause validation?

    Upon a brief testing your idea of using SendKeys seem to work, what problem are you actually encountering with that?

  13. #13
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    Re: Can a menu bar cause validation?

    dglienna: I want to move focus to the next control in the tab order that can take focus.

    dee-u: Maybe the TAB was being thrown off by stepping through code in the debugger. I'll put the breakpoint after it, and try again.

  14. #14
    Join Date
    Mar 2007
    Location
    15 doors east
    Posts
    47

    Re: Can a menu bar cause validation?

    The SendKeys didn't work. Here's the current code:
    Code:
    '**
    'Menu Click Event Handler
    Private Sub MnuSave_Click()
    'On Error Resume Next
        'Manually fire validation for the current control
        'If validation passes, call ToolAction
        Dim ctlTemp As Control
        Dim ctlTemp2 As Control
        Set ctlTemp = Me.ActiveControl
        SendKeys "{TAB}"
        Set ctlTemp2 = Me.ActiveControl
        If Not (ctlTemp Is ctlTemp2) Then
            Call m_objForm.ToolAction(tbrBntSAVE_REC)
        End If
        
        Set ctlTemp = Nothing
        Set ctlTemp2 = Nothing
    End Sub
    I tested this and made no changes to data, so validation would have passed. I set my breakpoint on the IF statement, and found that ctlTemp and ctlTemp2 were pointing to the same control.

    What can I try next?

  15. #15
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Can a menu bar cause validation?

    A mere

    Code:
    SendKeys "{TAB}"
    worked for me. How many controls do you have in your form? Or could you attach a sample form?

Page 1 of 2 12 LastLast

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center