[英]Panel Control doesn't update
我的页面上有面板对象,我想用另一个面板替换它
//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具有象牙色的背景色,并且有一些控件。 当我执行上述代码时,我会看到pnl_page_active背景已经更改,并且对此没有任何控制,但这是相同的,所以我想知道这是什么问题?
您所要做的就是将new_panel变量分配给pnl_page_active。 它与控制层次结构无关。
您需要从其父面板中删除旧面板,然后插入新面板:
Control parent = pnl_page_active.Parent;
if (parent != null) {
parent.Controls.Remove(pnl_page_active);
parent.Controls.Add(new_panel);
}
您当前正在做的是修改pnl_page_active
以引用与new_panel
相同的面板...但是new_panel
从未添加到窗体中,因此您看不到颜色变化。
删除上面发布的所有代码,然后直接更改BackColor
即可:
pnl_page_active.BackColor = Color.White;
如果要用新面板替换现有面板(无论出于何种原因),除了要复制的任何属性外,还必须确保它具有相同的父面板,尺寸,位置等。
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.