下面是可用於同時具有多個面板的代碼,並在添加到表單中的下一個按鈕之間進行切換。
public Form1()
{
InitializeComponent();
panel1.Visible = true;
panel3.Visible = false;
panel2.Visible = false;
}
private void btnNext_Click(object sender, EventArgs e)
{
if (panel1.Visible)
{
panel1.Visible = false;
panel2.Visible = true;
panel3.Visible = false;
}
else if (panel2.Visible)
{
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = true;
}
else if (panel3.Visible)
{
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
}
}
當然,如果你升序標記您的面板/降格式的標籤之間沒有間隙例如1,2,3,4或5,4,3,2不是1,2,4諸位可以使用此代碼
public Form1()
{
InitializeComponent();
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
}
private void btnNext_Click(object sender, EventArgs e)
{
TogglePanels();
}
public void TogglePanels()
{
List<Panel> allPanelsInForm = new List<Panel>();
foreach (var control in Controls)
{
if (control is Panel)
allPanelsInForm.Add(control as Panel);
}
Panel visiblePanel = allPanelsInForm.Where(o => o.Visible).FirstOrDefault();
int nextPanelId = Convert.ToInt32(visiblePanel.Tag) + 1;
bool nextPanelExists = allPanelsInForm.Exists(o => Convert.ToInt32(o.Tag) == nextPanelId);
nextPanelId = nextPanelExists ? nextPanelId : 1;
foreach (Panel panel in allPanelsInForm)
{
panel.Visible = Convert.ToInt32(panel.Tag) == nextPanelId ? true : false;
}
}
我希望它能幫助你。
我很抱歉,但這也是我的代碼。這是不行的,因爲它應該是。 – jayz
好的兄弟,但只是爲了您的保證我自己檢查這個代碼與VS2013,他們都像一個魅力。你可以用一個空的項目來試試這個代碼嗎?也許這是一種幫助 – mostafakvd