Results 1 to 8 of 8

Thread: [RESOLVED] Form getting unwanted focus

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] Form getting unwanted focus

    Hey.

    I've got this little problem in my app which im trying to solve, but have no idea how...
    I'm not even sure if i understand the problem but here goes:

    I've got two forms, (let's call them Form1 and Form2).

    On Form1, the user selects a customer from a database.
    He then presses a button, which:
    - Opens Form2
    - Saves the customer name in a variable in Form2 (Form2.Cust_Name = ..)

    Form2 now displays the customer name and some other emtpy fields, ready to be edited.

    The user also has to choose an ID number.
    For this I created a new form (frmID) which asks the user to enter the ID number, press OK and bring it to Form2. This is a modal form, which pops up as soon as the user presses the button to go to Form2.

    This all works fine, except for one thing:

    As soon as I display the 'sort of inputbox' (frmID), Form1 kicks into focus behind frmID.
    I don't want this... I want Form2 to stay visible, and Form1 on the background.

    I presume it's the way i coded it, which is something like the following:
    Form1:
    Code:
    Private Sub cmdGoToForm2_Click()
    Form2.Show
    
    'Copy variables to Form2
    Form2.Cust_Name = txtCust_Name
    Form2.txtCust_Name = Form2.Cust_Name
    
    'Here some database show code to show the entrie of the selected Cust_Name
    
    'Set Focus to Form2 explicitly and call the sub in Form2 which opens the frmID modal form
    Form2.SetFocus
    Call Form2.cmdNEW_Click
    
    End Sub
    Form2:
    Code:
    Public Sub cmdNEW_Click()
    frmID.Show (vbModal)
    End Sub
    The problem, i THINK, is that Form1 kicks back into focus when the "Call Form2.cmdNEW_Click" statement is made...
    How can I stop this from happening? eg, call this sub without getting Form1 to the foreground (but still behind frmID, probably cause its a Modal form)..?

  2. #2
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: Form getting unwanted focus

    Quote Originally Posted by NickThissen
    Hey.

    I've got this little problem in my app which im trying to solve, but have no idea how...
    I'm not even sure if i understand the problem but here goes:

    I've got two forms, (let's call them Form1 and Form2).

    On Form1, the user selects a customer from a database.
    He then presses a button, which:
    - Opens Form2
    - Saves the customer name in a variable in Form2 (Form2.Cust_Name = ..)

    Form2 now displays the customer name and some other emtpy fields, ready to be edited.

    The user also has to choose an ID number.
    For this I created a new form (frmID) which asks the user to enter the ID number, press OK and bring it to Form2. This is a modal form, which pops up as soon as the user presses the button to go to Form2.

    This all works fine, except for one thing:

    As soon as I display the 'sort of inputbox' (frmID), Form1 kicks into focus behind frmID.
    I don't want this... I want Form2 to stay visible, and Form1 on the background.

    I presume it's the way i coded it, which is something like the following:
    Form1:
    Code:
    Private Sub cmdGoToForm2_Click()
    Form2.Show
    
    'Copy variables to Form2
    Form2.Cust_Name = txtCust_Name
    Form2.txtCust_Name = Form2.Cust_Name
    
    'Here some database show code to show the entrie of the selected Cust_Name
    
    'Set Focus to Form2 explicitly and call the sub in Form2 which opens the frmID modal form
    Form2.SetFocus
    Call Form2.cmdNEW_Click
    
    End Sub
    Form2:
    Code:
    Public Sub cmdNEW_Click()
    frmID.Show (vbModal)
    End Sub
    The problem, i THINK, is that Form1 kicks back into focus when the "Call Form2.cmdNEW_Click" statement is made...
    How can I stop this from happening? eg, call this sub without getting Form1 to the foreground (but still behind frmID, probably cause its a Modal form)..?
    Have you tried adding Form2.Show before the End Sub in frmID form?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Form getting unwanted focus

    Before the end sub in frmID form..? How is that relevant?
    Form1 is showing as soon as frmID is shown, how has this anything to do with what appens after processes in frmID are completed...?

    or maybe you meant in Form2...

    I tried it, Form2 code is now:

    Form2.Show
    frmID.Show (vbModal)

    It simply moves Form2 to another position, then shows frmID, then behind frmID it shows Form1 again... same problem



    In case you didn't mean Form2, maybe i wasn't clear enough as to what's happening:

    - User clicks cmdGoToForm2.
    - Form2 shows, the variable from Form1 copied over to Form2.
    - frmID shows, on top of Form2.
    - Behind frmID (and on top of Form2) Form1 suddenly shows aswell. <-- WRONG

    I want Form1 to stay in the background, or atleast behind Form2.



    EDIT
    Ok, im starting to think something more is involved, but i have no clue what...

    I just tried the following:

    Form1 calls a new (public) sub in Form2:
    Call Form2.CallNEW

    Then, in Form2, the sub "CallNEW" simply calls the cmdNEW_Click:
    Code:
    Public Sub CallNEW()
    Call cmdNEW_Click
    End Sub
    This should have gotten rid of the problem if it was the "Call" statemtn in Form1 that gave Form1 the focus back, right?
    But it's still the same... nothing changed.
    Last edited by NickThissen; May 1st, 2007 at 03:04 PM.

  4. #4
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Form getting unwanted focus

    When showing 'sub forms' why not use VBModal ?
    That should avoid any focus going back to the caller whilst the callee is showing
    Rob C

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Form getting unwanted focus

    What do you mean with 'sub forms'?

    frmID already is shown modal, but in the background behind it Form1 still jumps infront of Form2...
    It's like it's trying to get focus, but because frmID is a modal form it can't so it just waits behind it...

  6. #6
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Form getting unwanted focus

    What do you mean with 'sub forms'?
    I just mean a form that is being called to get some input (or show some detail) and it is the intention to return to the calling form.
    You can use the SetWindowPos to ensure that the 'sub form' is On Top of the caller
    (Focus and TopMost are not 'joined at the hip')
    Rob C

  7. #7
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Form getting unwanted focus

    PS
    Definitely Modal and TopMost will solve your problem.
    However
    You might try a simpler solution -
    frmImport.Show vbModal, Me
    might get the desired results, as it ensures the caller cannot getfocus, and from my memory, it forces the called form to sit on top of the caller.
    Rob C

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Form getting unwanted focus

    Thanks!

    frmID.Show vbModal, Me

    did the trick!

    Thanks alot, it looks alot better now :P

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