TreeNode.BeginEdit Metodo

Definizione

Inizializza la modifica dell'etichetta del nodo della struttura ad albero.

public:
 void BeginEdit();
public void BeginEdit ();
member this.BeginEdit : unit -> unit
Public Sub BeginEdit ()

Eccezioni

LabelEdit è impostato su false.

Esempio

L'esempio di codice seguente consente all'utente di modificare i nodi dell'albero nonroot usando un ContextMenuoggetto . Quando l'utente fa clic con il pulsante destro del mouse, l'oggetto TreeNode in corrispondenza di tale posizione viene determinato e archiviato in una variabile denominata mySelectedNode. Se è stato selezionato un nodo albero nonroot, viene inserito in uno stato modificabile, consentendo all'utente di modificare l'etichetta del nodo. Dopo che l'utente smette di modificare l'etichetta del nodo albero, viene valutato e salvato il nuovo testo dell'etichetta. Per questo esempio, diversi caratteri non sono considerati validi nel testo dell'etichetta. Se uno dei caratteri non validi è nella stringa di etichetta o la stringa è vuota, l'utente riceve una notifica dell'errore e l'etichetta viene restituito al testo precedente.

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

Commenti

Uno scenario tipico da usare questo metodo consiste nel chiamarlo sull'evento Click di un MenuItem oggetto o ContextMenu.

Nota

Questo metodo funziona solo se la proprietà dell'oggetto LabelEditTreeView è impostata su true. Se LabelEdit è impostato su false, viene generata un'eccezione e il nodo dell'albero non verrà inserito in uno stato modificabile.

Si applica a

Vedi anche