473,856 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

treeview selected node

Hi, I've got a treeview, and I want to display the node text property
that I have

a) clicked on

or

b) highlighted using the up or down arrows
Could anyone please tell me how to do this? I guess there are two
separate events involved? If I use After Select, the form errors out
on opening.

thanks,

Ray
Jun 27 '08 #1
9 6135
"auntiejack 56" <ra*@aic.net.au schrieb
Hi, I've got a treeview, and I want to display the node text
property that I have

a) clicked on

or

b) highlighted using the up or down arrows
Could anyone please tell me how to do this? I guess there are two
separate events involved? If I use After Select, the form errors out
on opening.
AfterSelect event is the right place. Which error do you get in which
line?
Armin

Jun 27 '08 #2
auntiejack56 wrote:
I've got a treeview, and I want to display the node text property
that I have (a) clicked on or (b) highlighted using the up or down arrows
[TreeView].SelectedNode will get you the currently selected Node /if/
there /is/ one.

If not, you'll get a null reference, which your code has to deal with.
I guess there are two separate events involved?
No; it's the same event, AfterSelect, because in both cases, you're
selecting a Node; the TreeView control doesn't care /how/ you chose to
do so.
If I use AfterSelect, the form errors out on opening.
Presumably with a NullReferenceEx ception?
If there isn't a currently selected Node, you have to deal with this by
testing for Nothing first. That said, does AfterSelect even fire until
you select a Node? I can't remember. Post the code that's failing and
we'll have a look.

HTH,
Phill W.
Jun 27 '08 #3
Well do we really have to tell you that the error message could help ?

--
Patrice

"auntiejack 56" <ra*@aic.net.au a écrit dans le message de groupe de
discussion :
ae************* *************** **...legroup s.com...
Hi, I've got a treeview, and I want to display the node text property
that I have

a) clicked on

or

b) highlighted using the up or down arrows
Could anyone please tell me how to do this? I guess there are two
separate events involved? If I use After Select, the form errors out
on opening.

thanks,

Ray

Jun 27 '08 #4
Well do we really have to tell you that the error message could help ?
>
Thanks, first I just wanted to know that I was coding to the right
event. Then indeed the error message helps.

What I've put in is:

Private Sub scrTree_AfterSe lect(ByVal sender As Object, ByVal e As
System.Windows. Forms.TreeViewE ventArgs) Handles scrTree.AfterSe lect
Dim tTree As TreeView = sender
If Not tTree.SelectedN ode Is Nothing Then
MessageBox.Show ("Clicked on " & tTree.SelectedN ode.Text)
End If
End Sub

Which works fine, thanks all for your help.

I have a couple of other questions though, sorry to plague you:

When I comment out the messagebox line, the form with the treeview
opens as expected (obviously with no message).

When I uncomment the code, the messagebox pops up before the form
displays. All good so far. I click yes. The form doesn't display. It's
there, but I have to click on the taskbar to get it to show. I have a
feeling that I'm breaching some norm of form opening in VB?

Question 2 is, should I be using the local variable declaration to
pick up the treeview object from 'sender' or is there a better or more
appropriate way?

Thanks - Jack

Jun 27 '08 #5
auntiejack56 wrote:
Private Sub scrTree_AfterSe lect( _
ByVal sender As Object _
, ByVal e As System.Windows. Forms.TreeViewE ventArgs _
) Handles scrTree.AfterSe lect
Dim tTree As TreeView = sender
If Not tTree.SelectedN ode Is Nothing Then
MessageBox.Show ("Clicked on " & tTree.SelectedN ode.Text)
End If
End Sub
You're not using "Option Strict On" and I would strongly recommend that
you should. The statement ...

Dim tTree As TreeView = sender

.... wouldn't compile because you're implicitly casting an "Object"
(that's any old object) into a TreeView. There's /no/ guarantee that
you're always going to get a TreeView calling this routine (I know; it's
a TreeView event handler, but this is the Brave New World of event
handling in .Net).

Dim tTree as TreeView = Nothing
If TypeOf sender Is TreeView Then
tTree = DirectCast( sender, TreeView )
. . .
End If

(You might want to test for Nothing in there as well, to be really,
/really/ paranoid).
When I uncomment the code, the messagebox pops up before the form
displays. All good so far. I click yes. The form doesn't display. It's
there, but I have to click on the taskbar to get it to show. I have a
feeling that I'm breaching some norm of form opening in VB?
It's a /bit/ unusual to be popping up a dialog from the Load processing
of a Form, but that still /shouldn't/ cause the Form to be minimised as
a result.
Question 2 is, should I be using the local variable declaration to
pick up the treeview object from 'sender' or is there a better or more
appropriate way?
No, that's fine.
You can avoid [coding] the variable using a "With" block ...

With DirectCast( sender, TreeView )
. . .
End With

.... but I /think/ the compiler just creates a variable for itself, so
you're not gaining anything.

And, before you ask, I prefer DirectCast over CType because DirectCast
/only/ does "pointer casting"; CType /can/ do more for you, largely when
you don't expect it (and, probably, don't want it to. YMMV).

HTH,
Phill W.
Jun 27 '08 #6
When I uncomment the code, the messagebox pops up before the form
displays. All good so far. I click yes. The form doesn't display. It's
there, but I have to click on the taskbar to get it to show. I have a
feeling that I'm breaching some norm of form opening in VB?
Works fine here. I see the message and the form displays. You may perhaps
have a style or you handle another event when the form loses focus that
minimize the form ?

This event fires when a node is selected. This is the case when you
initialize the treeview content. Is this the real world thing you want to do
in this event (for example if you want to populate a label somewhere to
remind the selected value it won't cause any problem. If you really want to
display a message box you may want to add an additional check so that this
is not done when the form loads (you could use a boolean flag you could test
in this event).
Question 2 is, should I be using the local variable declaration to
pick up the treeview object from 'sender' or is there a better or more
appropriate way?
It's largely a matter of personal preference :
- using sender would allows to use the same event for another treeview, this
is not possible if you directly use the variable name
- you could use directly the treeview variable
- if you split the code from the event (i.e. the event handler just calls
the "real" code) you could do one or the other and still keep the ability to
apply the same code to another treeview control (and even possibly in other
forms)...

Also you way want to check the "option strict" option...

--
Patrice
Jun 27 '08 #7
Your replies much appreciated, thanks - lots to think about there!

Jack

Jun 27 '08 #8
On Wed, 30 Apr 2008 12:48:49 +0100, "Phill W."
<p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote:
>
Dim tTree as TreeView = Nothing
If TypeOf sender Is TreeView Then
tTree = DirectCast( sender, TreeView )
. . .
End If
I usually do it this way because it involves less typing:
Dim tTree as TreeView = TryCast(sender, TreeView)
If tTree IsNot Nothing Then
. . .
End If
Is there any reason to prefer one over the other?
Jun 27 '08 #9
Jack Jackson wrote:
On Wed, 30 Apr 2008 12:48:49 +0100, "Phill W."
<p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote:
> Dim tTree as TreeView = Nothing
If TypeOf sender Is TreeView Then
tTree = DirectCast( sender, TreeView )
. . .
End If
I usually do it this way because it involves less typing:
> Dim tTree as TreeView = TryCast(sender, TreeView)
If tTree IsNot Nothing Then
. . .
End If
Is there any reason to prefer one over the other?
I wouldn't have thought so, no.

TryCast probably uses TypeOf and DirectCast under the covers anyway (or
TypeOf and /CType/ which, IIRC, in turn uses DirectCast).

Regards,
Phill W.
Jun 27 '08 #10

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

Similar topics

6
6734
by: Tim | last post by:
Hi I have a form with a treeview on it. When I select a particular node it adds/shows a tab on the form. The treeview node remains selected as it should. The user can close the tab or remove the focus to another tab. Clicking on the same already selected node should add/show the tab, however the after select event does not fire as the node is already selected. I trapped the click event of the treeview but that is not node specific. In...
7
2844
by: vsiat | last post by:
I am trying to create a treeview out of a database table with the typical structure ID, NAME, PARENTID, TYPE, EXTRA_INFO, where is linked to the . What I want to achieve is create a tree made of custom, extended nodes, which include all the extra information contained in the table and not just typical TreeNode objects. To do that, I first created a structure with all the extra
6
4936
by: L.M | last post by:
Hello, I knew how to use the treeview under VB6. After migrating to .NET, well, I'm lost. I try to add a new node, either to the same level or as a child to a selected node in the treeview. However, either it only add it to the root level or it only add it on level below, doesn't matter what I select. And in some case, I just get an exception.
3
2261
by: Shawn | last post by:
Hi. I'm working with the TreeView control in my ASP.NET 1.1 application. I have a problem I haven't been able to figure out. When I click on a node (not expand), whether it's a parent node, a child node or a leaf node then a DataGrid is populated based on the node's ID property. If I expand a node then the DataGrid is not populated. This works fine. The problem is that this doesn't work if I collapse the tree. If I collapse the tree...
2
9795
by: Claus | last post by:
Hello, I have a long treeview with scrollbars. When I scroll down and press a treeview node, then the Load event fires and navigate in an iframe to another page. The problem is, that afterwards the treeview is collapsed and the node isn't selected. Expanding and selecting the node via code in the load event works, but how do I achieve, that the treeview scrolls down to the selected node? Thanks for any help Claus
8
12790
by: Matt MacDonald | last post by:
Hi All, I have a form that displays hierarchical categories in a treeview. Ok so far so good. What I was to do is have users be able to select a node in the treeview as part of filling out the form. I only want to allow single selection, so using checkboxes is out of the question. It works as is, but it makes the form very cumbersome if every time that a user selects a node, the whole page has to reload. Is there a way to have a node...
1
3610
by: Nikron | last post by:
Hi, I'm having an issue with the ASP.NET 2.0 Treeview control and persisting its' state accross requests. My Control is embedded within a master page and is used for site navigation. My problem is that the user wants to know which page they are currently on and therefore I need to highlight the selected node. The problem is I lose state whenever the user selects a node and is redirected to another page. Thanks in advance
0
3318
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. Provide following view properties to listview, through View menu a. Tile b. Icon
1
3944
by: =?Utf-8?B?Rmx5Z3V5?= | last post by:
I have a TreeView with so many nodes that it requires scroll bars. When the user selects a node the screen is refreshed and the selected node is no longer visible. The TreeView is opened to the selected node but the screen is not scrolled to it. How can I get the TreeView to show the selected node after it has been refreshed?
3
4714
by: dutsnekcirf | last post by:
I have a treeview control on a custom task pane in Excel. I've enable the ability to use Drag & Drop (by following this how-to) on the treeview to change the order of the nodes. The problem though is if I close the application and then go back in, the nodes are back in the order they were before I had reordered them. Is there a way that I can modify my DragDrop event to permanently change the index of the nodes? Here's my code if you're...
0
11065
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10700
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10795
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,...
0
10390
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7934
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4581
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
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
bsmnconsultancy
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.