英語で読む

次の方法で共有


TreeView.LabelEdit プロパティ

定義

ツリー ノードのラベル テキストを編集できるかどうかを示す値を取得または設定します。

C#
public bool LabelEdit { get; set; }

プロパティ値

ツリー ノードのラベル テキストを編集できる場合は true。それ以外の場合は false。 既定値は、false です。

次のコード例では、 を使用して、ユーザーが非ルート ツリー ノードを ContextMenu編集できます。 ユーザーがマウスを右クリックすると、その位置にある TreeNode が決定され、 という名前 mySelectedNodeの変数に格納されます。 非ルート ツリー ノードが選択されている場合は、編集可能な状態になります。これにより、ユーザーはノード ラベルを編集できます。 ユーザーがツリー ノード ラベルの編集を停止すると、新しいラベル テキストが評価されて保存されます。 この例では、ラベル テキストでは複数の文字が無効と見なされます。 無効な文字のいずれかがラベル文字列内にある場合、または文字列が空の場合、ユーザーにエラーが通知され、ラベルが前のテキストに返されます。

C#
/* 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();
     }
   }
}

注釈

メソッドはBeginEdit、 プロパティが のLabelEdittrue場合にのみ機能します。 LabelEditfalse ラベルを編集しようとすると、例外がスローされ、ツリー ノードは編集可能な状態になりません。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください