473,575 Members | 3,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set focus from treeview to control

Hi,

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.

Regards,
Mong

May 4 '06 #1
1 6341
Mong,

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.GotFocu s 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 (GiveMeBackMyFo cus) to true in this Handler.
In the TreeView.GotFoc us 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 GiveMeBackMyFoc us As Boolean
Private controlToFocus As Control
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

textBoxNode = Me.TreeView1.No des.Add("TextBo x Node")
listNode = Me.TreeView1.No des.Add("Listbo x Node")
otherNode = Me.TreeView1.No des.Add("Other Node")

End Sub
Private Sub TreeView1_After Select(ByVal sender As System.Object,
ByVal e As System.Windows. Forms.TreeViewE ventArgs) Handles
TreeView1.After Select

If e.Node Is textBoxNode Then
Me.controlToFoc us = Me.TextBox1
ElseIf e.Node Is Me.listNode Then
Me.controlToFoc us = Me.ListBox1
Else
Return
End If

Me.ActiveContro l = Me.controlToFoc us

End Sub

Private Sub FocusHandler(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles TextBox1.GotFoc us, ListBox1.GotFoc us
Me.GiveMeBackMy Focus = True
End Sub

Private Sub TreeView1_GotFo cus(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TreeView1.GotFo cus
If Me.GiveMeBackMy Focus Then
Me.controlToFoc us.Focus()
Me.GiveMeBackMy Focus = False
End If
End Sub

End Class

John

May 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2107
by: Franck Pommereau | last post by:
Hi all, I'm building a PyGTK interface in which I would like that no widget would be able to get the focus (and so to be activated by pressing the Return key). For this purpose, for each widget, I do: widget.set_property("can-focus", gtk.FALSE) My problem is a TreeView which has a clickable column, it get the default focus and I did...
3
1819
by: Dave | last post by:
I use the treeview to open a new form but the treeview reclaims focus after MouseUp. Any solutions to this problem?
5
3308
by: (Pete Cresswell) | last post by:
I've got a TreeView on the left of my screen. As the user navigates the tree, I load corresonding detail info into the rest of the screen. Works ok, but when the user clicks on a detail field, the TreeView's selection rectangle goes away and the user is left wondering what happened. Seems like there ought TB a way to preserve that...
1
15537
by: Rasmus | last post by:
I need to grab focus to a specific node i my treeview when I click on my listbox. Here's the code that differs between the different event i the listBox: private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { string selected; selected = this.listBox1.SelectedItem.ToString(); if(selected == "Error in Supercell 1")...
2
4010
by: Stephan Rose | last post by:
Ok here's my situation... Got an MDI application with a panel to my right that has a tree view for the project stuff, etc. Now if I select an item in the tree view, the tree view gains input focus. After this happens, my MDI children no longer are able to obtain keyboard focus and therefore no longer receive keyboard commands. Needless...
0
1135
by: Elliot | last post by:
The following code creates 2 node objects in a treeview control. When I click on the second node, a process object corectly launches "http://www.cnn.com" but focus immediately returns to the treeview control. It shouldn't. It should stay with the Internet Page. The Page is being launched from the AfterSelect Event, could that be causing the...
3
10462
by: Kay | last post by:
Hi all, Can anyone tell me how do I set focus on a particular treenode at run time? If you need to know what I'm try to do please read on ... I have a treeview to represent a Country/Product scenario (i.e. a country, as parent node, produce 1 to many product, as childnode). When the user click on one of the node a group of textbox/combo...
14
1697
by: Fr15ky | last post by:
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...
1
8452
by: Tee | last post by:
Hi, In my C# application i have a treeview control and listview control, the treeview is on the left side of the from and the listview is on the right. When i click on an item in treeview i got code to force the listview to selected the appropriate item and set focus on the listview, but the focus never work it always stay on the treeview...
0
7775
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8143
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6515
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5338
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3778
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1382
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.