473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 6111
"auntiejack56" <ra*@aic.net.auschrieb
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 NullReferenceException?
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

"auntiejack56" <ra*@aic.net.aua écrit dans le message de groupe de
discussion :
ae**********************************...oglegroups.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_AfterSelect(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles scrTree.AfterSelect
Dim tTree As TreeView = sender
If Not tTree.SelectedNode Is Nothing Then
MessageBox.Show("Clicked on " & tTree.SelectedNode.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_AfterSelect( _
ByVal sender As Object _
, ByVal e As System.Windows.Forms.TreeViewEventArgs _
) Handles scrTree.AfterSelect
Dim tTree As TreeView = sender
If Not tTree.SelectedNode Is Nothing Then
MessageBox.Show("Clicked on " & tTree.SelectedNode.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
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...
7
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...
6
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....
3
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...
2
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...
8
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...
1
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...
0
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. ...
1
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
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...
0
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...

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.