0

我有这个树视图菜单在此处输入图像描述

使用此代码:

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 窗体,具有与窗体相同的命名空间。汉斯,但我把 dispose() 放在哪里???我添加了 brintofront() 正在工作,但是我把 dispose() 放在最后一个关闭的地方

4

1 回答 1

0

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

每当用户选择“服务器”标签时,您都希望弹出一个新表单。

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

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;
}
于 2013-08-08T21:37:35.593 回答