TreeView.SelectedNode プロパティ
アセンブリ: System.Web (system.web.dll 内)


TreeView コントロール内のノードのテキストは、2 種類のモード (選択モードとナビゲーション モード) のどちらかで表示できます。既定では、ノードは選択モードになっています。ノードをナビゲーション モードにするには、ノードの NavigateUrl プロパティを、空の文字列 ("") 以外の値に設定します。ノードを選択モードにするには、ノードの NavigateUrl プロパティを空の文字列 ("") に設定します。
ノードが選択モードの場合、ユーザーはノード内のテキストをクリックしてノードを選択できます。SelectedNode プロパティを使用して、TreeView コントロールの選択されているノードを確認します。
![]() |
---|
TreeView コントロールにハイパーリンクが表示されている場合、ノードを選択することはできません。ハイパーリンクが表示されている場合、SelectedNode プロパティは常に null 参照 (Visual Basic では Nothing) を返します。 |
ユーザーが新しいノードのテキストをクリックして、TreeView コントロール内の別のノードを選択すると、既定では SelectedNodeChanged イベントが発生します。このプロパティの SelectAction プロパティを設定して、別のイベントを指定することもできます。利用可能なオプションの一覧を次の表に示します。

SelectedNode プロパティを使用して、TreeView コントロールで選択されたノードの値を確認する方法を次のコード例に示します。
<%@ Page Language="VB" %> <script runat="server"> Sub Select_Change(ByVal sender As Object, ByVal e As EventArgs) Message.Text = "You selected: " & LinksTreeView.SelectedNode.Text End Sub </script> <html> <body> <form runat="server"> <h3>TreeView SelectedNodeStyle Example</h3> <asp:TreeView id="LinksTreeView" Font-Name= "Arial" ForeColor="Blue" SelectedNodeStyle-ForeColor="Green" SelectedNodeStyle-VerticalPadding="0" OnSelectedNodeChanged="Select_Change" runat="server"> <LevelStyles> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Bold="true" Font-Size="12pt" ForeColor="DarkGreen"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-Bold="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-UnderLine="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Size="8pt"/> </LevelStyles> <Nodes> <asp:TreeNode Text="Table of Contents" SelectAction="None"> <asp:TreeNode Text="Chapter One"> <asp:TreeNode Text="Section 1.0"> <asp:TreeNode Text="Topic 1.0.1"/> <asp:TreeNode Text="Topic 1.0.2"/> <asp:TreeNode Text="Topic 1.0.3"/> </asp:TreeNode> <asp:TreeNode Text="Section 1.1"> <asp:TreeNode Text="Topic 1.1.1"/> <asp:TreeNode Text="Topic 1.1.2"/> <asp:TreeNode Text="Topic 1.1.3"/> <asp:TreeNode Text="Topic 1.1.4"/> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Chapter Two"> <asp:TreeNode Text="Section 2.0"> <asp:TreeNode Text="Topic 2.0.1"/> <asp:TreeNode Text="Topic 2.0.2"/> </asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Appendix A" /> <asp:TreeNode Text="Appendix B" /> <asp:TreeNode Text="Appendix C" /> </Nodes> </asp:TreeView> <br><br> <asp:Label id="Message" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" %> <script runat="server"> void Select_Change(Object sender, EventArgs e) { Message.Text = "You selected: " + LinksTreeView.SelectedNode.Text; } </script> <html> <body> <form runat="server"> <h3>TreeView SelectedNodeStyle Example</h3> <asp:TreeView id="LinksTreeView" Font-Name= "Arial" ForeColor="Blue" SelectedNodeStyle-ForeColor="Green" SelectedNodeStyle-VerticalPadding="0" OnSelectedNodeChanged="Select_Change" runat="server"> <LevelStyles> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Bold="true" Font-Size="12pt" ForeColor="DarkGreen"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-Bold="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-UnderLine="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Size="8pt"/> </LevelStyles> <Nodes> <asp:TreeNode Text="Table of Contents" SelectAction="None"> <asp:TreeNode Text="Chapter One"> <asp:TreeNode Text="Section 1.0"> <asp:TreeNode Text="Topic 1.0.1"/> <asp:TreeNode Text="Topic 1.0.2"/> <asp:TreeNode Text="Topic 1.0.3"/> </asp:TreeNode> <asp:TreeNode Text="Section 1.1"> <asp:TreeNode Text="Topic 1.1.1"/> <asp:TreeNode Text="Topic 1.1.2"/> <asp:TreeNode Text="Topic 1.1.3"/> <asp:TreeNode Text="Topic 1.1.4"/> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Chapter Two"> <asp:TreeNode Text="Section 2.0"> <asp:TreeNode Text="Topic 2.0.1"/> <asp:TreeNode Text="Topic 2.0.2"/> </asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Appendix A" /> <asp:TreeNode Text="Appendix B" /> <asp:TreeNode Text="Appendix C" /> </Nodes> </asp:TreeView> <br><br> <asp:Label id="Message" runat="server"/> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TreeView.SelectedNode プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As TreeView Dim value As TreeNode value = instance.SelectedNode instance.SelectedNode = value
/** @property */ public TreeNode get_SelectedNode () /** @property */ public void set_SelectedNode (TreeNode value)
ツリー ビュー コントロールで現在選択されている TreeNode。

現在選択されている TreeNode がない場合は、SelectedNode プロパティが null 参照 (Visual Basic では Nothing) になります。
このプロパティを設定すると、画面がスクロールして指定されたノードが表示され、親ノードがある場合はその親ノードが展開して指定されたノードが表示されます。
選択されたノードの親ノードまたは先祖ノードが、プログラム上またはユーザーのアクションによって折りたたまれた場合、折りたたまれたノードが選択されたノードとなります。

TreeView の PathSeparator プロパティを設定し、SelectedNode の TreeNodeCollection に含まれている子ツリー ノードの数を表示するコード例を次に示します。ツリー ビュー コントロール内のすべてのツリー ノードに対する子ツリー ノードの割合も表示されます。この例は、Form 上に、Button と TreeView コントロール が配置され、その TreeView に、複数の TreeNode オブジェクト (3 レベル以上を推奨) を持つ TreeNodeCollection が設定されていることを前提にしています。
Private Sub myButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles myButton.Click ' Set the tre view's PathSeparator property. myTreeView.PathSeparator = "." ' Get the count of the child tree nodes contained in the SelectedNode. Dim myNodeCount As Integer = myTreeView.SelectedNode.GetNodeCount(True) Dim myChildPercentage As Decimal = CDec(myNodeCount) / _ CDec(myTreeView.GetNodeCount(True)) * 100 ' Display the tree node path and the number of child nodes it and the tree view have. MessageBox.Show(("The '" + myTreeView.SelectedNode.FullPath + "' node has " _ + myNodeCount.ToString() + " child nodes." + Microsoft.VisualBasic.ControlChars.Lf _ + "That is " + String.Format("{0:###.##}", myChildPercentage) _ + "% of the total tree nodes in the tree view control.")) End Sub
private void myButton_Click(object sender, System.EventArgs e) { // Set the tre view's PathSeparator property. myTreeView.PathSeparator = "."; // Get the count of the child tree nodes contained in the SelectedNode. int myNodeCount = myTreeView.SelectedNode.GetNodeCount(true); decimal myChildPercentage = ((decimal)myNodeCount/ (decimal)myTreeView.GetNodeCount(true)) * 100; // Display the tree node path and the number of child nodes it and the tree view have. MessageBox.Show("The '" + myTreeView.SelectedNode.FullPath + "' node has " + myNodeCount.ToString() + " child nodes.\nThat is " + string.Format("{0:###.##}", myChildPercentage) + "% of the total tree nodes in the tree view control."); }
void myButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { // Set the tre view's PathSeparator property. myTreeView->PathSeparator = "."; // Get the count of the child tree nodes contained in the SelectedNode. int myNodeCount = myTreeView->SelectedNode->GetNodeCount( true ); Decimal myChildPercentage = ((Decimal)myNodeCount / (Decimal)myTreeView->GetNodeCount( true )) * 100; // Display the tree node path and the number of child nodes it and the tree view have. MessageBox::Show( String::Concat( "The '", myTreeView->SelectedNode->FullPath, "' node has ", myNodeCount, " child nodes.\nThat is ", String::Format( "{0:###.##}", myChildPercentage ), "% of the total tree nodes in the tree view control." ) ); }
private void myButton_Click(Object sender, System.EventArgs e) { // Set the tre view's PathSeparator property. myTreeView.set_PathSeparator("."); // Get the count of the child tree nodes contained in the SelectedNode. int myNodeCount = myTreeView.get_SelectedNode().GetNodeCount(true); System.Decimal myChildPercentage = Decimal.Multiply(Decimal.Divide(new Decimal(myNodeCount) , new Decimal(myTreeView.GetNodeCount(true))), new Decimal(100)); // Display the tree node path and the number of child nodes it and the // tree view have. MessageBox.Show("The '" + myTreeView.get_SelectedNode().get_FullPath() + "' node has " + ((Int32)myNodeCount).ToString() + " child nodes.\nThat is " + String.Format("{0:###.##}" , myChildPercentage) + "% of the total tree nodes in the tree view control."); } //myButton_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTreeView.SelectedNodeを検索する場合は、下記のリンクをクリックしてください。

- TreeView.SelectedNodeのページへのリンク