TreeView节点只能工作一次

时间:2013-08-08 19:30:02

标签: c# treeview

我有这个树视图菜单enter image description here

使用此代码:

TreeNode treeNode4 = new TreeNode("Importação CT-I", array5);
treeView1.Nodes.Add(treeNode4);

TreeNode nodeusu1 = new TreeNode("Usuários");
TreeNode nodeusu2 = new TreeNode("Servers");
TreeNode nodeusu3 = new TreeNode("Permissões");
TreeNode nodeusu4 = new TreeNode("Alterar Senha");
TreeNode nodeusu5 = new TreeNode("Sobre");
TreeNode nodeusu6 = new TreeNode("Encerrar");
TreeNode[] array6 = new TreeNode[] { nodeusu1, nodeusu2, nodeusu3, nodeusu4, nodeusu5, nodeusu6 };


private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    if (treeView1.SelectedNode.Text == "Grupo")
    {
        frm_grupo grupo = new frm_grupo();
        grupo.TopLevel = false;
        grupo.AutoScroll = true;
        panelmain.Controls.Add(grupo);
        grupo.Show();
    }

    else if (treeView1.SelectedNode.Text == "Servers")
    {
        Conecta Servidor = new Conecta();
        Servidor.TopLevel = false; 
        Servidor.AutoScroll = true;
        panelmain.Controls.Add(Servidor);
        Servidor.Show();
            Servidor.BringToFront();
    }
}

所有节点都工作正常,但节点“服务器”只能工作一次,如果我换到另一个节点,节点“服务器”不再工作,我做错了什么?

Conecta是一个Windows窗体,具有与窗体相同的命名空间。汉斯,但我把处置()?我添加了brintofront()正在工作,但我把dispose()关闭到最后一个

1 个答案:

答案 0 :(得分:0)

我正在努力了解你在这里想要做什么(而且西班牙语没有帮助)所以如果我弄错了,请纠正我:

只要用户选择“Servers”标签,您就会弹出一个新表格。

看起来您的代码很好,并且您不想调用“Dispose”,否则您的表单将会消失。您需要做的是使Servidor成为一个实例对象,以便您可以跟踪它。实例化它,然后注册它的Closed事件以知道用户关闭它,以便您可以创建新实例或再次弹出上一个实例。您也不需要将其作为主窗体控件库的一部分。如果您希望它是子表单(如模式),则将主表单引用(this)作为参数传递给“Show”方法。

TreeNode treeNode4 = new TreeNode("Importação CT-I", array5);
treeView1.Nodes.Add(treeNode4);

TreeNode nodeusu1 = new TreeNode("Usuários");
TreeNode nodeusu2 = new TreeNode("Servers");
TreeNode nodeusu3 = new TreeNode("Permissões");
TreeNode nodeusu4 = new TreeNode("Alterar Senha");
TreeNode nodeusu5 = new TreeNode("Sobre");
TreeNode nodeusu6 = new TreeNode("Encerrar");
TreeNode[] array6 = new TreeNode[] { nodeusu1, nodeusu2, nodeusu3, nodeusu4, nodeusu5, nodeusu6 };

Conecta Servidor = null;


private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    if (treeView1.SelectedNode.Text == "Grupo")
    {
        frm_grupo grupo = new frm_grupo();
        grupo.TopLevel = false;
        grupo.AutoScroll = true;
        panelmain.Controls.Add(grupo);
        grupo.Show();
    }

    else if (treeView1.SelectedNode.Text == "Servers")
    {
        if(Servidor == null)
        {
            Servidor = new Conecta();
            Servidor.TopLevel = false; 
            Servidor.AutoScroll = true;
            Servidor.Closed += new FormClosedEventHandler(ServidorClosed);
        }
        Servidor.Show();
            Servidor.BringToFront();
    }
}
private void ServidorClosed(object sender, EventArgs e)
{
    if(Servidor != null)
        Servidor.Dispose();
    Servidor == null;
}