473,701 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6361

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
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


May 5 '06 #2

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

Similar topics

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 not find how to forbid that.
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?
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 rectangle. The optimal would be to have it go grey when the control loses focus and return to blue whe...
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") {
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 to say, that's a bad thing. :)
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 treeview control to get focus again? Any help would be appreciated. Thanks,
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 box etc will be populated and if btnSave is clicked data will then be updated and the treeview will...
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 clicks the TreeView and the form is already open with that link in it, it is supposed to set focus to...
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 control. Here my sample code
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.