0

我正在动态地将表单分配给标签页。它工作正常,除非我尝试将表单的更新版本(分配给控件的不同文本值)分配给标签页。实际上,当我创建表单的多个实例并将它们分配给标签页时它不会崩溃,我实际上有点惊讶(如果新的只是堆叠在旧的上面,你会认为新的会是在顶部,新值可见)。

那么......我需要做什么才能在添加新版本之前删除我添加到标签页的先前表单?或者我可以访问现有表单并更改其值。

我想如果我只是显示代码会更清楚:

主要形式:

    private enum TabControls {
        BasicInfo,
        ConfidentialInfo,
        RolesAndSecurity,
        InactiveInfo
    }

    string currentNode = string.Empty;

    public Form1() {
        InitializeComponent();
        CenterToScreen();
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
        currentNode = e.Node.Name;
        UpdateActiveTabForNode();
    }

    // This is called both by treeView1_AfterSelect (sic, I changed the name to 
    // treeViewSearchWorker) and by tabControlWorker_SelectedIndexChanged(). 
    // currentNode is set/overwritten on clicking a node and saved in a form var.
    // The active tab page is always known via the TabControl's SelectedIndex
    // property, so there is no need to pass in either value.
    private void UpdateActiveTabForNode() {
        int ActiveTabPage = tabControlWorker.SelectedIndex;

        switch (ActiveTabPage) {
            case (int)TabControls.BasicInfo:
                if (tabPageBasicInfo.Controls.Count > 0) {
                    ;// tabPageBasicInfo.Controls.Remove(0);<-- What to pass in here?
                }
                    BasicInfoPseudoTab bipt = new BasicInfoPseudoTab(currentNode);
                    tabPageBasicInfo.Controls.Add(bipt);
                tabPageBasicInfo.BringToFront();
                    bipt.Show();
                break;
            case (int)TabControls.ConfidentialInfo:
                ConfidentialInfoPseudoTab cipt = new ConfidentialInfoPseudoTab(currentNode);
                tabPageConfidentialInfo.Controls.Add(cipt);
                cipt.Show();
                break;
            case (int)TabControls.RolesAndSecurity:
                RolesAndSecurityPseudotab raspt = new RolesAndSecurityPseudotab(currentNode);
                    tabPageRolesAndSecurity.Controls.Add(raspt);
                    raspt.Show();
                break;
            case (int)TabControls.InactiveInfo:
                InactiveInformationPseudoTab iipt = new InactiveInformationPseudoTab(currentNode);
                    tabPageInactiveInfo.Controls.Add(iipt);
                    iipt.Show();
                break;
            default: {
                    break;
                    // TODO: Do something?
                }
        }
    }

    private void tabControlWorker_SelectedIndexChanged(object sender, System.EventArgs e) {
        UpdateActiveTabForNode();
    }
}

==== 作为伪标签页之一的表单:

public partial class BasicInfoPseudoTab : Form {
    String _aNodeName = String.Empty;

    public BasicInfoPseudoTab(String ANodeName) {
        InitializeComponent();
        // Without this, you get "TopLevel control cannot be added to a control"
        this.TopLevel = false;
        this.FormBorderStyle = FormBorderStyle.None;
        this.Visible = true;
        this.Dock = DockStyle.Fill;
        _aNodeName = ANodeName;
        SetDisplayVals();
    }

    private void SetDisplayVals() {
        if (_aNodeName == "NodeBuckingham") {
            textBoxFirstName.Text = "Buckingham";
            textBoxLastName.Text = "Piranha";
            textBoxNickname.Text = "B.P.";
        }
        else if (_aNodeName == "NodeVolcano") {
            textBoxFirstName.Text = "Volcano";
            textBoxLastName.Text = "Jerry";
            textBoxNickname.Text = "V.J.";
        } else if (_aNodeName == "NodeParsons") {
            textBoxFirstName.Text = "Parsons";
            textBoxLastName.Text = "Spalding";
            textBoxNickname.Text = "P.S.";
        } else {
            textBoxFirstName.Text = String.Empty;
            textBoxLastName.Text = String.Empty;
            textBoxNickname.Text = String.Empty;
        }
    }

更新:

我通过在事件处理程序之外声明表单变量并在继续之前将它们不为空时处理它们来使其工作。

    string currentNode = string.Empty;
    BasicInfoPseudoTab bipt;
    ConfidentialInfoPseudoTab cipt;
    RolesAndSecurityPseudotab raspt;
    InactiveInformationPseudoTab iipt;

    . . .

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
        currentNode = e.Node.Name;
        UpdateActiveTabForNode();
    }

    private void UpdateActiveTabForNode() {
        int ActiveTabPage = tabControlWorker.SelectedIndex;

        switch (ActiveTabPage) {
            case (int)TabControls.BasicInfo:
                if (null != bipt) {
                    bipt.Dispose();
                }
                bipt = new BasicInfoPseudoTab(currentNode);
                tabPageBasicInfo.Controls.Add(bipt);
                bipt.Show();
                break;
            . . .
4

1 回答 1

1

如您所愿,使用模块级变量,每个表单一个,如果为空,则为新它,否则如上所述。如果您不断地在彼此之上创建表单,您将如何清理?在您不断创建表单时检查您的工作集的大小。

于 2012-03-23T17:00:05.977 回答