Selectednode

  • Thread starter Thread starter Jerry
  • Start date Start date
J

Jerry

Hello,

I am able to GET the selected node with my code. I would however like to SET
any node with code. Something like
me.treeview.selectednode="First"
But this doesn't work. I can't find anything on setting this property. Can
you help?


Thanks,

Jerry
 
Jerry said:
I am able to GET the selected node with my code. I would however like to
SET any node with code. Something like
me.treeview.selectednode="First"
But this doesn't work. I can't find anything on setting this property. Can
you help?

You'll have to assign a tree node object to the property instead of a
string.
 
I'm not too firm with VB yet. Could you give me an example?

The SelectedNode property is not a string, so you cannot assign a string to
it.

If you need to search a node using its key you can use Nodes.Find() or
Nodes.IndexOf() to get the node and then you can select it via SelectedNode
property.
 
Jerry said:
I'm not too firm with VB yet. Could you give me an example?

To select the first node as the selected node:

\\\
Me.TreeView.SelectedNode = Me.TreeView.Nodes(0)
///

To select a different node you will need to locate the appropriate Node
within the Nodes collection, and then pass that to the SelectedNode
property.
 
Hi jerry,

Sub TreeView_EnsureFirstNodeIsSelectedIfExist(ByVal Treeview As
TreeView)
If Treeview.Nodes.Count = 0 Then Exit Sub
Treeview.SelectedNode = Treeview.Nodes(0)
End Sub


usually one wishes that the AfterSelect event be fired even though the
node is selected already (for 2003 replace isNot with if not...is):


Sub TreeView_ForceSelectFirstNodeIfExist(ByVal Treeview As
TreeView)
If Treeview.Nodes.Count = 0 Then Exit Sub
If Treeview.SelectedNode IsNot Nothing Then
Treeview.SelectedNode = Nothing
Treeview.SelectedNode = Treeview.Nodes(0)
End Sub

example

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Me.TreeView1.HideSelection = False
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(New TreeNode("Hi Jerry"))

TreeView_ForceSelectFirstNodeIfExist(Me.TreeView1)

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles
TreeView1.AfterSelect
MsgBox("Selected " & e.Node.Text)
End Sub

End Class

-tommaso




Jerry ha scritto:
 
Hi,
I have a revision already :)

(i have added an if to the first sub)
Please suggest improvements if any...

-tommaso


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Me.TreeView1.HideSelection = False
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(New TreeNode("Hello"))

TreeView_EnsureFirstNodeIsSelectedIfExist(Me.TreeView1)

'Force selection again
TreeView_ForceSelectFirstNodeIfExist(Me.TreeView1)

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles
TreeView1.AfterSelect
MsgBox("Selected " & e.Node.Text)
End Sub


Sub TreeView_EnsureFirstNodeIsSelectedIfExist(ByVal Treeview As
TreeView)
If Treeview.Nodes.Count = 0 Then Exit Sub
If Treeview.SelectedNode IsNot Treeview.Nodes(0) Then
Treeview.SelectedNode = Treeview.Nodes(0)
End Sub

Sub TreeView_ForceSelectFirstNodeIfExist(ByVal Treeview As
TreeView)
If Treeview.Nodes.Count = 0 Then Exit Sub
If Treeview.SelectedNode IsNot Nothing Then
Treeview.SelectedNode = Nothing
Treeview.SelectedNode = Treeview.Nodes(0)
End Sub

End Class







(e-mail address removed) ha scritto:
 
I hear what your saying, but I'm afraid I can't do it.

I have one parent node, node1 and three child nodes, child1, child2, child3.
They are fixed and don't change. They all have a tag, parent, one, two and
three. Making things happen when I select them is no problem. But when the
form loads, I want child1 to be selected, not parent. So lets say I want
code to select child3 by tag or lable what is the code for doing so?

Forgive me, but I am learning by copying code and changing it to my needs.
"objects" "classes" and so on are only words to me. I know I have to learn,
but for now I'd like to keep it simple.


Thanks,

Jerry
 
Now that's something I can work with, thanks!

Me.tvwMenu.SelectedNode = Me.tvwMenu.Nodes(0).Nodes(0) did the trick.

Thank you,

Jerry
 
Back
Top