TreeView.GetNodeAt 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取位於指定位置的樹狀節點。
多載
GetNodeAt(Point) |
擷取位於指定處的樹狀節點。 |
GetNodeAt(Int32, Int32) |
擷取指定座標處的樹狀節點。 |
GetNodeAt(Point)
- 來源:
- TreeView.cs
- 來源:
- TreeView.cs
- 來源:
- 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
。
備註
您可以將 事件的 和 MouseEventArgs.Y 座標 MouseDown 當做 X 新 Point 的 和 Y 值傳遞 MouseEventArgs.X 。
另請參閱
適用於
GetNodeAt(Int32, Int32)
- 來源:
- TreeView.cs
- 來源:
- TreeView.cs
- 來源:
- 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 傳遞 MouseEventArgs.X 為 x
和 y
參數。