471,852 Members | 1,163 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,852 software developers and data experts.

set focus from treeview to control


I'm using a treeview as a menu, and when the user clicks on a node I
want the focus to set to a control (ie textbox) in a panel to the
right. The problem I'm having is the focus will not stay on the
control, but instead jumps back to the treeview.

I'm setting the focus in the AfterSelect event of the treeview, and if
I step through the code the desired control does have .Focused = True
after I have set it, but then somewhere after the AfterSelect event the
focus changes back to the listview. I have no other code after the
focus is set to the control.

Is there a better event I should be doing this in, or something else I
need to do to stop the focus jumping back to the treeview.


May 4 '06 #1
1 5852

I was having a similar problem in an app and my workaround may or may
not work for you. If you put the thread to sleep for a second during
the TextBox.GotFocus event you'll see that the TreeView hasn't fully
updated when the AfterSelect is fired. The node you click is still
highlighted in gray instead of the dark blue. I'm assuming this is
part of the reason it steals the focus back - to redraw itself.

My method.

I have a generic handler, FocusHandler( ) that handles the GotFocus
event for all the controls that get focus from the TreeView. I set a
form-level boolean (GiveMeBackMyFocus) to true in this Handler.
In the TreeView.GotFocus event, I check this boolean. If it's set to
True, then the TreeView gives control back and sets it to False to
prevent it from firing again.

I track the control that needs focus with a form-level
System.Windows.Forms.Control variable that is set during the
AfterSelect event.

Here's a quick example of how I implemented it:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

Private textBoxNode As TreeNode
Private listNode As TreeNode
Private otherNode As TreeNode
Private GiveMeBackMyFocus As Boolean
Private controlToFocus As Control
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

textBoxNode = Me.TreeView1.Nodes.Add("TextBox Node")
listNode = Me.TreeView1.Nodes.Add("Listbox Node")
otherNode = Me.TreeView1.Nodes.Add("Other Node")

End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles

If e.Node Is textBoxNode Then
Me.controlToFocus = Me.TextBox1
ElseIf e.Node Is Me.listNode Then
Me.controlToFocus = Me.ListBox1
End If

Me.ActiveControl = Me.controlToFocus

End Sub

Private Sub FocusHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus, ListBox1.GotFocus
Me.GiveMeBackMyFocus = True
End Sub

Private Sub TreeView1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TreeView1.GotFocus
If Me.GiveMeBackMyFocus Then
Me.GiveMeBackMyFocus = False
End If
End Sub

End Class


May 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Franck Pommereau | last post: by
3 posts views Thread by Dave | last post: by
1 post views Thread by Rasmus | last post: by
2 posts views Thread by Stephan Rose | last post: by
reply views Thread by Elliot | last post: by
14 posts views Thread by Fr15ky | last post: by
1 post views Thread by Tee | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.