简体   繁体   English

面板控件未更新

[英]Panel Control doesn't update

I have Panel Object on my page , I want to replace it with another Panel 我的页面上有面板对象,我想用另一个面板替换它

//Info.panel[cb_page_number.SelectedIndex] = pnl_page_active;
Panel new_panel = new Panel();
new_panel.BackColor = Color.White;
//new_panel.Name ="page_"+ (cb_page_number.SelectedIndex+1).ToString();
//cb_page_number.Items.Add(new_panel.Name);
//cb_page_number.SelectedIndex = cb_page_number.Items.Count-1;
pnl_page_active = new_panel;
pnl_page_active.Refresh();
pnl_page_active.Update();
Application.DoEvents();

pnl_page_active has a ivory background color,And there is some controls on that. pnl_page_active具有象牙色的背景色,并且有一些控件。 When I execute above code I expext to see pnl_page_active background has been changed and there is no control on that,But it's the same,So I'm wondering what's the problem? 当我执行上述代码时,我会看到pnl_page_active背景已经更改,并且对此没有任何控制,但这是相同的,所以我想知道这是什么问题?

All you have done is assigning new_panel variable to pnl_page_active. 您所要做的就是将new_panel变量分配给pnl_page_active。 It has nothing to do with Control hierarchy. 它与控制层次结构无关。

You need to remove the old panel from it's parent and insert the new one: 您需要从其父面板中删除旧面板,然后插入新面板:

Control parent = pnl_page_active.Parent;
if (parent != null) {
    parent.Controls.Remove(pnl_page_active);
    parent.Controls.Add(new_panel);
}

What you're currently doing is modifying pnl_page_active to reference the same Panel that new_panel is... but new_panel was never added to the Form, so you don't see the color change. 您当前正在做的是修改pnl_page_active以引用与new_panel相同的面板...但是new_panel从未添加到窗体中,因此您看不到颜色变化。

Remove all the code you posted above, and just change the BackColor directly: 删除上面发布的所有代码,然后直接更改BackColor即可:

pnl_page_active.BackColor = Color.White;

If you want to replace the existing Panel with the new one (for whatever reason), you'll have to make sure it has the same parent, size, location, etc, in addition to whatever attributes you're copying. 如果要用新面板替换现有面板(无论出于何种原因),除了要复制的任何属性外,还必须确保它具有相同的父面板,尺寸,位置等。

Panel new_panel = new Panel();
new_panel.BackColor = Color.White;
new_panel.Size = pnl_page_active.Size;
new_panel.Location = pnl_page_active.Location;
new_panel.Parent = pnl_page_active.Parent;
new_panel.Show();

pnl_page_active.Hide();  // or Dispose if you don't want it anymore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM