TreeView.GetNodeAt Méthode

Définition

Récupère le nœud d’arbre situé à l’emplacement spécifié.

Surcharges

GetNodeAt(Point)

Récupère le nœud d'arbre situé au point spécifié.

GetNodeAt(Int32, Int32)

Récupère le nœud d'arbre situé au point correspondant aux coordonnées spécifiées.

GetNodeAt(Point)

Récupère le nœud d'arbre situé au point spécifié.

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

Paramètres

pt
Point

Point à évaluer et pour lequel vous souhaitez récupérer le nœud.

Retours

TreeNode au point spécifié, en coordonnées d'arborescence (client), ou null s'il n'y a aucun nœud à cet emplacement.

Remarques

Vous pouvez passer les MouseEventArgs.X coordonnées et MouseEventArgs.Y de l’événement MouseDown comme X valeurs et Y d’un nouveau Point.

Voir aussi

S’applique à

GetNodeAt(Int32, Int32)

Récupère le nœud d'arbre situé au point correspondant aux coordonnées spécifiées.

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

Paramètres

x
Int32

Position X à évaluer et à laquelle vous souhaitez récupérer le nœud.

y
Int32

Position Y à évaluer et à laquelle vous souhaitez récupérer le nœud.

Retours

TreeNode à l'emplacement spécifié, en coordonnées d'arborescence (client), ou null s'il n'y a aucun nœud à cet emplacement.

Exemples

L’exemple de code suivant permet à l’utilisateur de modifier des nœuds d’arborescence non racine à l’aide d’un ContextMenu. Lorsque l’utilisateur clique avec le bouton droit sur la souris, le TreeNode à cette position est déterminé et stocké dans une variable nommée mySelectedNode. Si un nœud d’arborescence non racine a été sélectionné, il est placé dans un état modifiable, ce qui permet à l’utilisateur de modifier l’étiquette du nœud. Une fois que l’utilisateur a cessé de modifier l’étiquette du nœud d’arborescence, le nouveau texte d’étiquette est évalué et enregistré. Pour cet exemple, plusieurs caractères sont considérés comme non valides dans le texte de l’étiquette. Si l’un des caractères non valides se trouve dans la chaîne d’étiquette ou si la chaîne est vide, l’utilisateur est averti de l’erreur et l’étiquette est retournée à son texte précédent.

   /* 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

Remarques

Vous pouvez passer les MouseEventArgs.X coordonnées et MouseEventArgs.Y de l’événement MouseDown comme x paramètres et y .

Voir aussi

S’applique à