Form Focus Issues

First of all, I am using the .NET 2.0 framework with Visual Studio 2005.

I have two forms. One is a form with a TreeView control on it (used as a menu - kindof a favorites list) and the other is a form with a WebBrowser control on it.

The TreeView_Click event fires up the other form with the link passed to it via the Node. If the user clicks the TreeView and the form is already open with that link in it, it is supposed to set focus to the appropriate form. This does occur, but it appears that when the Click event completes, the form with the TreeView control on it gets the focus back. So other than the TreeView form, the proper WebBrowser form is at the front.

How do I get the form with the TreeView to remain behind the properly selected WebBrowser form

Any suggestions would be appreciated.

Mark Dahl



Answer this question

Form Focus Issues

  • AnandDeshmukh

    Thanks Chris!

    This fixed the issue. Here is the resulting code in case anyone else is interested:

    Private Sub tvwNavigator_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tvwNavigator.KeyDown
    Try
    If e.KeyCode = Keys.Enter Then
    If Not tvwNavigator.SelectedNode.Equals(Nothing) Then
    ProcessNavigatorClick(tvwNavigator.SelectedNode)
    End If
    End If
    Catch ex As Exception
    End Try
    End Sub

    Private Sub tvwNavigator_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwNavigator.MouseDown
    Try
    t
    vwNavigator.SelectedNode = tvwNavigator.GetNodeAt(e.X, e.Y)
    _SiteSelected =
    True
    Catch ex As Exception
    End Try
    End Sub

    Private Sub tvwNavigator_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwNavigator.MouseMove
    Try
    If _SiteSelected Then
    _SiteSelected = False
    ProcessNavigatorClick(tvwNavigator.SelectedNode)
    End If
    Catch ex As Exception
    End Try
    End Sub

    I can truly say that I would not have even thought of using the _MouseMove event.

    Mark Dahl


  • SubZero

    Anyone know how to better handle this

    Here is the code for the section(s) that appear to be causing the problem:

    Private Sub tvwNavigator_MouseDown(ByVal sender As Object, ByVal e
    As System.Windows.Forms.MouseEventArgs) Handles tvwNavigator.MouseDown
    tvwNavigator.SelectedNode = tvwNavigator.GetNodeAt(e.X, e.Y)
    ProcessNavigatorClick(tvwNavigator.SelectedNode)
    End Sub

    It seems like once the ProcessNavigatorClick comes back and the End Sub is hit by the code, this brings the Navigator form back into focus.

    Mark Dahl


  • Mayank Banerjee

    Chris,

    Once I do that, the WebControl Form is always on top of the Navigator Form.

    I believe the problem is with the way events are occurring. If I press Enter which uses the KeyDown event, this problem does not occur. It is only on the MouseDown event that this problem occurs.

    Mark Dahl


  • Wilfried Martens

    Try setting the Form containing your WebControl as TopMost form.

    -chris



  • Jason-Massey

    Have you tried adding a "Me.SendToBack" after your "ProcessNavigatorClick(tvwNavigator.SelectedNode) " line
  • okipatrick

    Richard,

    Thank you for the response. It is greatly appreciated.

    Actually this was mentioned by someone on Google Groups, but the problem here is that you have trouble getting the Navigator back to the front after that. Which basically has the same problem that I had with Chris Vega's suggestion above.

    Would it help you guys to see more of the code I could post almost all of it and it wouldn't be too much. I just thought that someone out there had to have had a similar problem in the past.

    Mark Dahl


  • M L B

    Hi mark,

    Have you tried handling MouseMove event You see, every window will send WM_MOUSEMOVE (MouseMove event in .NET) as the very last message received. I believe, this is where your problem is occuring, when internal Form message handling, handles WM_MOUSEMOVE message, which resulting to bringing the current HWDN (Window or Control) to the foreground.

    Note: WM_MOUSEMOVE will be sent to the window right after WM_LBUTTONUP, which will complete a "Click".

    The solution I can offer might be a little crazy idea, but who knows, it might work. So, here goes:

    1. Add a global boolen variable to your Navigator form.
    2. When you click one of your treenode containing a link, (it's best to handle AfterSelect or MouseUp events, not MouseDown), then set the global boolean variable to true.
    3. Add a handler to MouseMove even for your treeview control in Navigator form.
    4. The MouseMove handler will check for the global boolean variable for true value, which when it is true, MouseMove will Activate the form containing your WebControl, and set the global boolean variable back to false.



    ' Put this inside your TreeView's MouseUp handler

    ' Perform checking for valid node that is clicked

    If NodeClickedIsAValidLink Then
        MyGlobalBool = True
        ' Set the link here to your WebBrowser control, and let the MouseMove
        ' event handler bring the form containing your WebControl to front.
    End If


     




    ' Put this inside your navigator's MouseMove handler
    If MyGlobalBool Then
        MyGlobalBool = False
        WebControlForm.Activate()
    End If

     


    Hope this helps,

    -chris



  • Rod at Work

    A quick idea, how about in the MouseUp event bringing your Web Browser form to the front



  • RenRen le casseur

    That is a good idea. I tried that earlier this morning, but there appears to be yet another event or something is occurring that is causing a click event to keep the Navigator form's focus.

    I have also tried to set the SelectedNode in the MouseDown Event and then process the click in other events, but this seems to have the same trouble.

    Does anyone have any idea what the last event of the TreeView control is Because I have thought of just setting a flag and then firing the ProcessNavigatorClick code in that last event to be sure that it keeps the focus on the opened form.

    Also, could this be because I am using a class to hold the URL of the Node and the RollOver Text of the Node. Is processing the myTreeNode class of the SelectedNode be causing the whole TreeView control to come back to the front I wouldn't think so, but some of the behavior seems to point to this. Funny thing is, you would think that a Click and a KeyPress would be handled the same by the control.

    Mark Dahl

    Mark Dahl


  • Form Focus Issues