110,532
社区成员
发帖
与我相关
我的任务
分享
private int p; //实际上在 VM 中,这里应该设计为属性并且可以触发 INotifyPrpertyChanged 事件
private ObservableCollection<MyUserControl> uc = new ObservableCollection<MyUserControl>();
public void 下一步()
{
p++;
for(var i=0; i<uc.Count; i++)
if(i==p)
uc[i].Visible = true;
else
uc[i].Visible = false;
}
或者是private int p; //实际上在 VM 中,这里应该设计为属性并且可以触发 INotifyPrpertyChanged 事件
private ObservableCollection<MyUserControl> uc = new ObservableCollection<MyUserControl>();
public void 下一步()
{
p++;
this.Controls.Clear();
for(var i=0; i<uc.Count; i++)
if(i==p)
this.Controls.Add(uc[i]);
}
但是这里最关键地是,用户控件实例是持久化在内存中的,不会因为什么 controls.Clear() 方法而动态创建或者被 gc 释放,而是在一个独立的集合中维系的用户控件。这样它才不会在“下一步、上一步”的操作中丢失状态。
/// <summary>
/// 数据库配置窗口
/// </summary>
private void ShowActivationForm2Step()
{
if (mActivationForm2Step == null)
{
mActivationForm2Step = new ActivationStep2Form(this);
mActivationForm2Step.FormBorderStyle = FormBorderStyle.None;
mActivationForm2Step.TopLevel = false;
mActivationForm2Step.ControlBox = false;
mActivationForm2Step.Dock = System.Windows.Forms.DockStyle.Fill;
mActivationForm2Step.AutoScroll = true;
mActivationForm2Step.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
}
this.mSubPanel.Controls.Clear();
this.mSubPanel.Controls.Add(mActivationForm2Step);
mActivationForm2Step.Show();
//数据库完成,可以跳过当前操作
mActivationForm2Step.CheckDatabaseIsIntact();
WriteLogcat();
mBtnJumpStep.Enabled = false;
mBtnPrev.Enabled = false;
mBtnNext.Enabled = false;
}
Panel panel_A, panel_B;
Label View;
public Form1()
{
InitializeComponent();
panel_A = new Panel() { Dock = DockStyle.Fill,Visible=false };
Label LabelShow = new Label() { AutoSize=true,Text="文字",Name="label_txt"};
panel_A.Controls.Add(LabelShow);
panel_A.Controls.Add(control1);
panel_B = new Panel() { Dock = DockStyle.Fill, Visible = false };
View = new Label() { AutoSize = true, Text = panel_A.Controls["label_txt"].Text };
panel_B.Controls.Add(View);
panel_B.Controls.Add(control2);
this.Controls.Add(panel_A);
this.Controls.Add(panel_B);
}
private void button_Click(object sender, EventArgs e)
{
View.Text = panel_A.Controls["label_txt"].Text;
panel_A.Visible = true;
panel_B.Visible = false;
}
}
groupBox1.Controls.Clear();
control1 uc1 = new control1();
groupBox1.Controls.Add(uc1);
uc1.Show();