Autor Beitrag
doubleII
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Do 15.12.16 15:52 
Hallo zusammen,


ich habe ein treeview
node 0
_______node 1 : 1
_______node 1 : 2
_______node 1 : 3

also zwei Ebene. Ich möchte die Nodes 1 umbenennen und zwar genau wie bei Windows, wenn ich zwei mal auf dem Node mit dem linke Maustaste
drücke oder Rechtsklick und Umbenennen wähle.

ich habe folgenen Code:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
  

     private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //
            //
            //
            RenameDirectory();
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            //
            //
            //
            RenameDirectory();
        }  
private void RenameDirectory()
        {
            if (treeView1.SelectedNode != null)
            {
                string sourcePath = ToolboxSubPath + "\\" + treeView1.SelectedNode.Text;
                treeView1.SelectedNode.Text = "@";
                //edit the label = true 
                treeView1.LabelEdit = true;
                if (!treeView1.SelectedNode.IsEditing)
                {
                    treeView1.SelectedNode.BeginEdit();
                }
                //save the new path name (source, destination)
                Directory.Move(sourcePath, ToolboxSubPath + "\\" + treeView1.SelectedNode.Text);
                // after edit label = false
                treeView1.LabelEdit = false;
            }
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();
                }
            }
        }     

        }

erstens:
Wie man sieht die Methode
ausblenden C#-Quelltext
1:
 private void treeView1_AfterLabelEdit					

prüft für die Sonderzeichen. Das Problem liegt daran, dass die erste
if Anweisung if(e.Label != null) ist immer null und ich kann nicht überprüfen, ob in dem neuen Namen ein Sonderzeichen existiert. Hat jemand eine Idee, wie könnte ich es machen?

zweitens:

Ich möchte den Node per Tastatur umbenennen, nicht wie oben:
 treeView1.SelectedNode.Text = "@";
wie macht man das?

Vielen Dank!

Schöne Grüße
doubleII
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 15.12.16 16:04 
Dein Code ist auch fehlerhaft:
ausblenden C#-Quelltext
1:
treeView1.SelectedNode.BeginEdit();					

startet den Editiervorgang (intern sendet es nur eine bestimmte Windows-Message) und kehrt sofort wieder zurück, d.h. die nachfolgenden Zeilen werden direkt danach ausgeführt. Wenn du dann LabelEdit wieder auf false setzt, kann es also sein, daß AfterLabelEdit nicht (oder ohne aktives Label) ausgeführt wird.
Ruf also direkt in DoubleClick die Methode treeView1.SelectedNode.BeginEdit() auf und das eigentliche Umbenennen erst im AfterLabelEdit!
doubleII Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Do 15.12.16 16:14 
Ach Th69 danke :)