Exporter (0) Imprimer
Développer tout

TreeView.BeforeLabelEdit, événement

Mise à jour : novembre 2007

Se produit une fois le texte de l'étiquette du nœud d'arbre modifié.

Espace de noms :  System.Windows.Forms
Assembly :  System.Windows.Forms (dans System.Windows.Forms.dll)

public event NodeLabelEditEventHandler BeforeLabelEdit
/** @event */
public void add_BeforeLabelEdit (NodeLabelEditEventHandler value)
/** @event */
public void remove_BeforeLabelEdit (NodeLabelEditEventHandler value)

JScript ne prend pas en charge les événements.

Pour plus d'informations sur la gestion des événements, consultez Consommation d'événements.

L'exemple de code suivant montre comment utiliser les membres BeforeLabelEdit, AfterSelect et TopNode. Pour exécuter cet exemple, collez le code suivant dans un formulaire contenant un contrôle TreeView nommé TreeView1. Appelez la méthode InitializeTreeView dans le constructeur du formulaire ou la méthode Load.

	private void InitializeTreeView()
	{

		// Construct the TreeView object.
		this.TreeView1 = new System.Windows.Forms.TreeView();

		// Set dock, location, size name, and tab order
		// values for the TreeView object.
		TreeView1.Dock = System.Windows.Forms.DockStyle.Left;
		TreeView1.Location = new System.Drawing.Point(0, 0);
		TreeView1.Name = "TreeView1";
		TreeView1.Size = new System.Drawing.Size(152, 266);
		TreeView1.TabIndex = 1;
		
		// Associate the event-handling methods with the
		// BeforeLabeEdit and the AfterSelect events.
		TreeView1.BeforeLabelEdit += 
			new NodeLabelEditEventHandler(TreeView1_BeforeLabelEdit);
		TreeView1.AfterSelect += 
			new TreeViewEventHandler(TreeView1_AfterSelect);

		// Set the LabelEdit property to true to allow the 
		// user to edit the TreeNode text.
		this.TreeView1.LabelEdit = true;

		// Declare and create objects needed to populate 
		// the TreeView.
		string[] files = new string[]{"bigPresentation.ppt", 
			"myFinances.xls", "myResume.doc"};; 
		string filePath = "c:\\myFiles";
		System.Collections.ArrayList nodes = 
			new System.Collections.ArrayList();

		// Create a node for each file, setting the Text property to the 
		// file's name and the Tag property to file's fully-qualified name.
		foreach ( string file in files )
		{
			TreeNode node = new TreeNode(file);
			node.Tag = filePath+"\\"+file;
			nodes.Add(node);
		}

		TreeNode[] treeNodes = new TreeNode[nodes.Count];
		nodes.CopyTo(treeNodes);

		// Create a new node named topNode and add the ArrayList of 
		// nodes to topNode.
		TreeNode topNode = new TreeNode("myFiles",  treeNodes);
		topNode.Tag = filePath;

		// Add topNode to the TreeView.
		TreeView1.Nodes.Add(topNode);

		// Add the TreeView to the form.
		this.Controls.Add(TreeView1);
	}

	private void TreeView1_BeforeLabelEdit(object sender, 
		NodeLabelEditEventArgs e)
	{

		// Determine whether the user has selected the top node. If so,
		// change the LabelEdit property to false so the user cannot
		// edit this label.  
		if (e.Node == TreeView1.TopNode)

		{
			TreeView1.LabelEdit = false;
			MessageBox.Show("You are not allowed to edit the top node");
		}
		// Set the LabelEdit property to true again.
		TreeView1.LabelEdit = true;
	}


...


	// Handle the After_Select event.
	private void TreeView1_AfterSelect(System.Object sender, 
		System.Windows.Forms.TreeViewEventArgs e)
	{

		// Vary the response depending on which TreeViewAction
		// triggered the event. 
		switch((e.Action))
		{
			case TreeViewAction.ByKeyboard:
				MessageBox.Show("You like the keyboard!");
				break;
			case TreeViewAction.ByMouse:
				MessageBox.Show("You like the mouse!");
				break;
		}
	}


private void InitializeTreeView()
{
    // Construct the TreeView object.
    this.treeView1 = new System.Windows.Forms.TreeView();
    // Set dock, location, size name, and tab order
    // values for the TreeView object.
    treeView1.set_Dock(System.Windows.Forms.DockStyle.Left);
    treeView1.set_Location(new System.Drawing.Point(0, 0));
    treeView1.set_Name("TreeView1");
    treeView1.set_Size(new System.Drawing.Size(152, 266));
    treeView1.set_TabIndex(1);
    // Associate the event-handling methods with the
    // BeforeLabeEdit and the AfterSelect events.
    treeView1.add_BeforeLabelEdit(
        new NodeLabelEditEventHandler(treeView1_BeforeLabelEdit));
    treeView1.add_AfterSelect(new TreeViewEventHandler(
        treeView1_AfterSelect));
    // Set the LabelEdit property to true to allow the 
    // user to edit the TreeNode text.
    this.treeView1.set_LabelEdit(true);
    // Declare and create objects needed to populate 
    // the TreeView.
    String files[] = new String[] { "bigPresentation.ppt", "myFinances.xls",
        "myResume.doc" };
    String filePath = "c:\\myFiles";
    System.Collections.ArrayList nodes = new System.Collections.ArrayList();
    // Create a node for each file, setting the Text property to the 
    // file's name and the Tag property to file's fully-qualified name.
    for (int iCtr = 0; iCtr < files.length; iCtr++) {
        String file = files[iCtr];
        TreeNode node = new TreeNode(file);
        node.set_Tag(filePath + "\\" + file);
        nodes.Add(node);
    }

    TreeNode treeNodes[] = new TreeNode[nodes.get_Count()];
    nodes.CopyTo(treeNodes);
    // Create a new node named topNode and add the ArrayList of 
    // nodes to topNode.
    TreeNode topNode = new TreeNode("myFiles", treeNodes);
    topNode.set_Tag(filePath);
    // Add topNode to the TreeView.
    treeView1.get_Nodes().Add(topNode);
    // Add the TreeView to the form.
    this.get_Controls().Add(treeView1);
} //InitializeTreeView

private void treeView1_BeforeLabelEdit(Object sender,
    NodeLabelEditEventArgs e)
{
    // Determine whether the user has selected the top node. If so,
    // change the LabelEdit property to false so the user cannot
    // edit this label.  
    if (e.get_Node().Equals(treeView1.get_TopNode())) {
        treeView1.set_LabelEdit(false);
        MessageBox.Show("You are not allowed to edit the top node");
    }
    // Set the LabelEdit property to true again.
    treeView1.set_LabelEdit(true);
} //treeView1_BeforeLabelEdit


...


// Handle the After_Select event.
private void treeView1_AfterSelect(Object sender,
    System.Windows.Forms.TreeViewEventArgs e)
{
    // Vary the response depending on which TreeViewAction
    // triggered the event. 
    switch (e.get_Action()) {
        case TreeViewAction.ByKeyboard:
            MessageBox.Show("You like the keyboard!");
            break;
        case TreeViewAction.ByMouse:
            MessageBox.Show("You like the mouse!");
            break;
    }
} //treeView1_AfterSelect


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professionnel Édition x64, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

.NET Framework

Pris en charge dans : 3.5, 3.0, 2.0, 1.1, 1.0

Ajouts de la communauté

AJOUTER
Afficher:
© 2014 Microsoft