C# 对象面板

C# Object Panels

我遇到的问题是我可以在哪里成功使用我创建的 Room 对象的 RoomID,但不能成功使用面板。这是我将标签设置为房间名称的功能。 (这是我的Form1.cs)

public partial class Form1 : Form
{
    Room ThisRoom = new Room(); 
    public Form1()
    {
        InitializeComponent();           
        this.Text = "Aquinas College Master Controller";
    }

    private void roomDesignerToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new RoomDesigner().Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        roomsToolStripMenuItem.DropDownItems.Clear();
        foreach (var Room in Global.Aquinas.Aquinas)
        {
            ToolStripMenuItem NewItem = new ToolStripMenuItem(Room.RoomID);
            NewItem.Name = Room.RoomID;
            NewItem.Click += new EventHandler(ItemClick);
            roomsToolStripMenuItem.DropDownItems.Add(NewItem);
        }
    }

    void ItemClick(object sender, EventArgs e)
    {
        ToolStripItem item = (ToolStripItem)sender;
        label2.Text = item.Name;
        foreach (var Room in Global.Aquinas.Aquinas)
        {
            if (Room.RoomID == item.Name)
            {
                ThisRoom = Room;
                break;
            }
        }
        Panel RoomPanel =  ThisRoom.Panel;
        RoomPanel.Size = new Size(607, 304);
        RoomPanel.Location = new Point(144, 27);
        RoomPanel.BackColor = Color.White;
        this.Controls.Add(RoomPanel);
    }
}

这是我的一些房间对象代码。 (这是在 Room.cs)

public class Room
{

    private List<CtrlComputer> _Computers;

    public List<CtrlComputer> Computers
    {
        get { return _Computers; }
        set { _Computers = value; }
    }

    private string _RoomID;

    public Room()
    {
        _Computers=new List<CtrlComputer>();
    }

    public string RoomID
    {
        get
        {
            return _RoomID;
        }

        set
        {
            _RoomID = value;
        }
    }

    public Panel Panel
    {
        get
        {
            return _Panel;
        }

        set
        {
            _Panel = value;
        }
    }

    private Panel _Panel;
}

这里是我将刚刚设计好的面板注册到新房间的地方。 (这是我的一些RoomDesigner.cs)

public partial class RoomDesigner : Form
{
    Room NewRoom = new Room();
    Panel RoomDesignerPanel = new Panel();
    public RoomDesigner()
    {
        InitializeComponent();
        RoomDesignerPanel.Size = new Size(607, 304);
        RoomDesignerPanel.Location = new Point(144, 27);
        RoomDesignerPanel.BackColor = Color.White;
        this.Controls.Add(RoomDesignerPanel);
        textBox1.ForeColor = SystemColors.GrayText;
        textBox1.Text = "Enter Computer ID Here";
        this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
        this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
        textBox2.ForeColor = SystemColors.GrayText;
        textBox2.Text = "Enter Room ID Here";
        this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
        this.textBox2.Enter += new System.EventHandler(this.textBox2_Enter);
    }

    private void textBox1_Leave(object sender, EventArgs e)
    {
        if (textBox1.Text.Length == 0)
        {
            textBox1.Text = "Enter Computer ID Here";
            textBox1.ForeColor = SystemColors.GrayText;
        }
    }

    private void textBox1_Enter(object sender, EventArgs e)
    {
        if (textBox1.Text == "Enter Computer ID Here")
        {
            textBox1.Text = "";
            textBox1.ForeColor = SystemColors.WindowText;
        }
    }
    private void textBox2_Leave(object sender, EventArgs e)
    {
        if (textBox2.Text.Length == 0)
        {
            textBox2.Text = "Enter Room ID Here";
            textBox2.ForeColor = SystemColors.GrayText;
        }
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        if (textBox2.Text == "Enter Room ID Here")
        {
            textBox2.Text = "";
            textBox2.ForeColor = SystemColors.WindowText;
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        CtrlComputer NewComputer = new CtrlComputer();
        NewComputer.ComputerID = textBox1.Text;
        NewComputer.Text = NewComputer.ComputerID;
        NewComputer.Parent = RoomDesignerPanel;
        RoomDesignerPanel.Controls.Add(NewComputer);
        NewRoom.Panel = RoomDesignerPanel;
        NewRoom.Add(NewComputer);
    }

当我尝试重新加载该面板时,该对象的 RoomID 返回正常,但面板却没有。有什么想法吗?

编辑:很抱歉我的 post 不够清晰,第一个 post 并不总是那么容易。 该面板是在设计器中创建的,但我尝试 private Panel Panel1 = new Panel(); 但没有成功。我目前的问题是,在 Form1.cs 中,程序会抛出 "Cannot access a disposed object",但我遵循了此处的指南 https://msdn.microsoft.com/en-us/library/82785s1h(v=vs.110).aspx

您要做的是在一组 Panel 对象(有点像您自己的 TabPages 版本)之间交换。为此,您需要创建每个 Panel 对象(您在 RoomDesigner 中执行此操作,这很好,但有一个问题:请参阅下面的 *)。有了这个集合,当你想在表单上显示它时,你需要删除设计器代码创建的面板 1 (Controls.Remove(panel1)) 并插入你的新面板 (Controls.Add(RoomPanel) ).

由于控件会占用系统资源,因此请确保处置您将不再使用的任何控件。这是一个示例方法:

    //Initial case where no room has been displayed yet and panel1 is still valid
    private Panel CurrentPanel = null;

    public void SwapPanel(Panel p)
    {
        //If no panel has been placed yet, get rid of the default one
        if (panel1 != null)
        {
            //we should never need the designer panel again, so dispose it
            panel1.Dispose();
            this.Controls.Remove(panel1);
        }
        else
        {
            //we may return to this panel later, so don't dispose it
            this.Controls.Remove(CurrentPanel);
        }
        CurrentPanel = p;
        this.Controls.Add(p);
    }

当您最终关闭并且不再需要将面板存储在您的房间集合中时,请确保处置它们。 (但是,如果你的整个程序在那个时候退出,那没关系,但无论如何都要这样做作为一种好的做法)

*当您关闭任何 RoomDesigner 窗体(或任何窗体)时,它包含在 'Controls' 中的所有控件都将被丢弃,即使您在其他地方使用它们也是如此。为防止出现这种情况,请在关闭之前从 RoomDesigner 控件集合中删除要保留的控件。