TreeView.GetNodeAt 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索位于指定位置的树节点。
重载
GetNodeAt(Point) |
检索位于指定点的树节点。 |
GetNodeAt(Int32, Int32) |
检索位于指定点(以坐标表示)的树节点。 |
GetNodeAt(Point)
- Source:
- TreeView.cs
- Source:
- TreeView.cs
- Source:
- TreeView.cs
检索位于指定点的树节点。
public:
System::Windows::Forms::TreeNode ^ GetNodeAt(System::Drawing::Point pt);
public System.Windows.Forms.TreeNode GetNodeAt (System.Drawing.Point pt);
public System.Windows.Forms.TreeNode? GetNodeAt (System.Drawing.Point pt);
member this.GetNodeAt : System.Drawing.Point -> System.Windows.Forms.TreeNode
Public Function GetNodeAt (pt As Point) As TreeNode
参数
返回
位于以树视图(客户端)坐标表示的指定点的 TreeNode,或在该位置没有节点时为 null
。
注解
可以将 事件的 和 坐标作为新 的 和 Y 值传递MouseEventArgs.X。XPointMouseDownMouseEventArgs.Y
另请参阅
适用于
GetNodeAt(Int32, Int32)
- Source:
- TreeView.cs
- Source:
- TreeView.cs
- Source:
- TreeView.cs
检索位于指定点(以坐标表示)的树节点。
public:
System::Windows::Forms::TreeNode ^ GetNodeAt(int x, int y);
public System.Windows.Forms.TreeNode GetNodeAt (int x, int y);
public System.Windows.Forms.TreeNode? GetNodeAt (int x, int y);
member this.GetNodeAt : int * int -> System.Windows.Forms.TreeNode
Public Function GetNodeAt (x As Integer, y As Integer) As TreeNode
参数
返回
位于以树视图(客户端)坐标表示的指定位置的 TreeNode,或在该位置没有节点时为 null
。
示例
下面的代码示例允许用户使用 ContextMenu编辑非根树节点。 当用户右键单击鼠标时, TreeNode 确定该位置的 并将其存储在名为 的 mySelectedNode
变量中。 如果选择了非根树节点,则会将其置于可编辑状态,从而允许用户编辑节点标签。 用户停止编辑树节点标签后,将计算并保存新标签文本。 在此示例中,标签文本中将多个字符视为无效。 如果标签字符串中存在一个无效字符,或者该字符串为空,则会通知用户错误,并将标签返回到以前的文本。
/* Get the tree node under the mouse pointer and
save it in the mySelectedNode variable. */
private:
void treeView1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
{
mySelectedNode = treeView1->GetNodeAt( e->X, e->Y );
}
void menuItem1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
if ( mySelectedNode != nullptr && mySelectedNode->Parent != nullptr )
{
treeView1->SelectedNode = mySelectedNode;
treeView1->LabelEdit = true;
if ( !mySelectedNode->IsEditing )
{
mySelectedNode->BeginEdit();
}
}
else
{
MessageBox::Show( String::Concat( "No tree node selected or selected node is a root node.\n",
"Editing of root nodes is not allowed." ), "Invalid selection" );
}
}
void treeView1_AfterLabelEdit( Object^ /*sender*/,
System::Windows::Forms::NodeLabelEditEventArgs^ e )
{
if ( e->Label != nullptr )
{
if ( e->Label->Length > 0 )
{
array<Char>^ temp0 = {'@','.',',','!'};
if ( e->Label->IndexOfAny( temp0 ) == -1 )
{
// Stop editing without canceling the label change.
e->Node->EndEdit( false );
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e->CancelEdit = true;
MessageBox::Show( String::Concat( "Invalid tree node label.\n",
"The invalid characters are: '@','.', ',', '!'" ),
"Node Label Edit" );
e->Node->BeginEdit();
}
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e->CancelEdit = true;
MessageBox::Show( "Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit" );
e->Node->BeginEdit();
}
}
}
/* Get the tree node under the mouse pointer and
save it in the mySelectedNode variable. */
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
if (mySelectedNode != null && mySelectedNode.Parent != null)
{
treeView1.SelectedNode = mySelectedNode;
treeView1.LabelEdit = true;
if(!mySelectedNode.IsEditing)
{
mySelectedNode.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}
private void treeView1_AfterLabelEdit(object sender,
System.Windows.Forms.NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
if(e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1)
{
// Stop editing without canceling the label change.
e.Node.EndEdit(false);
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\n" +
"The invalid characters are: '@','.', ',', '!'",
"Node Label Edit");
e.Node.BeginEdit();
}
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
}
}
' Get the tree node under the mouse pointer and
' save it in the mySelectedNode variable.
Private Sub treeView1_MouseDown(sender As Object, _
e As System.Windows.Forms.MouseEventArgs)
mySelectedNode = treeView1.GetNodeAt(e.X, e.Y)
End Sub
Private Sub menuItem1_Click(sender As Object, e As System.EventArgs)
If Not (mySelectedNode Is Nothing) And _
Not (mySelectedNode.Parent Is Nothing) Then
treeView1.SelectedNode = mySelectedNode
treeView1.LabelEdit = True
If Not mySelectedNode.IsEditing Then
mySelectedNode.BeginEdit()
End If
Else
MessageBox.Show("No tree node selected or selected node is a root node." & _
Microsoft.VisualBasic.ControlChars.Cr & _
"Editing of root nodes is not allowed.", "Invalid selection")
End If
End Sub
Private Sub treeView1_AfterLabelEdit(sender As Object, _
e As System.Windows.Forms.NodeLabelEditEventArgs)
If Not (e.Label Is Nothing) Then
If e.Label.Length > 0 Then
If e.Label.IndexOfAny(New Char() {"@"c, "."c, ","c, "!"c}) = -1 Then
' Stop editing without canceling the label change.
e.Node.EndEdit(False)
Else
' Cancel the label edit action, inform the user, and
' place the node in edit mode again.
e.CancelEdit = True
MessageBox.Show("Invalid tree node label." & _
Microsoft.VisualBasic.ControlChars.Cr & _
"The invalid characters are: '@','.', ',', '!'", _
"Node Label Edit")
e.Node.BeginEdit()
End If
Else
' Cancel the label edit action, inform the user, and
' place the node in edit mode again.
e.CancelEdit = True
MessageBox.Show("Invalid tree node label." & _
Microsoft.VisualBasic.ControlChars.Cr & _
"The label cannot be blank", "Node Label Edit")
e.Node.BeginEdit()
End If
End If
End Sub
注解
可以将 事件的 和 MouseEventArgs.Y 坐标MouseDown作为 x
和 y
参数传递MouseEventArgs.X。