I hate to ask this question, but I seriously need help. There are already 3 panels in the code that the buttons switch through. What I need is for it to switch through 7 different panels. The reason why im asking help on this is because ive been up over 48 hours working on a project and I need some sleep and its due by next monday...Also I have a lot of family stuff to do this weekend so ill only get 1-2 days to finish the rest including animations so if anyone can please do this for me it would make my life a lot easier.
private void Form4_Load(object sender, EventArgs e)
{
btn1.Enabled = false;
PanelSwitcher();
btn1.Click += btn1_Click; // back button
btn2.Click += btn2_Click; // forward button
}
int counter = 0;
int maximum = 2; //Maximium = total panels started from 0, 1, 2
private void btn2_Click(object sender, EventArgs e)
{
int original = counter;
counter++; //the ++ after a integer raises the value by 1
if (counter > maximum) counter = maximum;
btn1.Enabled = true; // counter must be at least 1, so enable back button
if (counter == maximum) //checks to see if counter is equal to that of maximum
{
btn2.Enabled = false; // disable forward button
}
if (counter != original) PanelSwitcher(); // switch panel if counter has changed
}
private void btn1_Click(object sender, EventArgs e)
{
int original = counter;
counter--;
if (counter < 0) counter = 0;
btn2.Enabled = true; // counter must be less than maximum, so enable forward button
if (counter == 0)
{
btn1.Enabled = false; // disable back button
}
if (counter != original) PanelSwitcher(); // switch panel if counter has changed
}
private void PanelSwitcher() // no longer a timer Tick handler
{
switch (counter)
{
case 0:
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
break;
case 1:
panel1.Visible = false;
panel2.Visible = true;
panel3.Visible = false;
break;
case 2:
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = true;
break;
}
}
}
}
Loading
VulpesPosted Jan 2, 2012, 4:58 AM
My panels are in a stack, are exactly the same size but different colors.
Whilst I can't see what difference it would make, if you've now settled on 4 panels, then you could try the longer version of the code instead:
private void PanelSwitcher()
{
switch (counter)
{
case 0:
panel1.BringToFront();
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
panel4.Visible = false;
break;
case 1:
panel1.Visible = false;
panel2.BringToFront();
panel2.Visible = true;
panel3.Visible = false;
panel4.Visible = false;
break;
case 2:
panel1.Visible = false;
panel2.Visible = false;
panel3.BringToFront();
panel3.Visible = true;
panel4.Visible = false;
break;
case 3:
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = false;
panel4.BringToFront();
panel4.Visible = true;
break;
}
}
The maximum field should, of course, now be set to 3.
Caleb DunnPosted Jan 2, 2012, 2:11 PM
VulpesPosted Jan 2, 2012, 12:01 PM
Caleb DunnPosted Jan 2, 2012, 11:47 AM
private void PanelSwitcher()
{
switch (counter)
{
case 0:
panel1.BringToFront();
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
panel4.Visible = false;
panel5.Visible = false;
break;
case 1:
panel1.Visible = false;
panel2.BringToFront();
panel2.Visible = true;
panel3.Visible = false;
panel4.Visible = false;
panel5.Visible = false;
break;
case 2:
panel1.Visible = false;
panel2.Visible = false;
panel3.BringToFront();
panel3.Visible = true;
panel4.Visible = false;
panel5.Visible = false;
break;
case 3:
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = false;
panel4.BringToFront();
panel4.Visible = true;
panel5.Visible = false;
case 4:
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = false;
panel4.Visible = false;
panel5.BringToFront();
panel5.Visible = true;
break;
}
}
Caleb DunnPosted Jan 1, 2012, 8:01 PM
VulpesPosted Jan 1, 2012, 6:07 PM
Caleb DunnPosted Jan 1, 2012, 4:44 PM
private void Form4_Load(object sender, EventArgs e)
{
btn1.Enabled = false;
PanelSwitcher();
btn1.Click += btn1_Click; // back button
btn2.Click += btn2_Click; // forward button
}
int counter = 0;
int maximum = 6; // Maximum = total panels started from 0, 1, 2
private void btn2_Click(object sender, EventArgs e)
{
int original = counter;
counter++; //the ++ after a integer raises the value by 1
if (counter > maximum) counter = maximum;
btn1.Enabled = true; // counter must be at least 1, so enable back button
if (counter == maximum) //checks to see if counter is equal to that of maximum
{
btn2.Enabled = false; // disable forward button
}
if (counter != original) PanelSwitcher(); // switch panel if counter has changed
}
private void btn1_Click(object sender, EventArgs e)
{
int original = counter;
counter--;
if (counter < 0) counter = 0;
btn2.Enabled = true; // counter must be less than maximum, so enable forward button
if (counter == 0)
{
btn1.Enabled = false; // disable back button
}
if (counter != original) PanelSwitcher(); // switch panel if counter has changed
}
private void PanelSwitcher() // no longer a timer Tick handler
{
foreach (Control c in this.Controls) // iterate through controls collection
{
if (c is Panel)
{
int number = int.Parse(c.Name.Substring(5, 1)); // find the number of the panel, 1 to 7
c.Visible = (number == counter + 1); // make it visible if it equals (counter + 1) else invisible
switch (counter)
{
case 0:
panel1.BringToFront();
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
break;
case 1:
panel1.Visible = false;
panel2.BringToFront();
panel2.Visible = true;
panel3.Visible = false;
break;
case 2:
panel1.Visible = false;
panel2.Visible = false;
panel3.BringToFront();
panel3.Visible = true;
break;
}
}
}
}
}
}
VulpesPosted Jan 1, 2012, 5:47 AM
Caleb DunnPosted Dec 31, 2011, 10:21 PM
VulpesPosted Dec 31, 2011, 5:08 PM
When you have overlapping panels, if you want them to retain their colors then you'll need to bring them to the top of the z-order ( the order in which overlapping controls are placed on the form). You can do this by calling the panel's BringToFront method before making it visible. For example:
Caleb DunnPosted Dec 31, 2011, 1:27 PM
VulpesPosted Dec 31, 2011, 1:23 PM
Caleb DunnPosted Dec 31, 2011, 12:25 PM
VulpesPosted Dec 31, 2011, 11:58 AM
Caleb DunnPosted Dec 31, 2011, 9:39 AM
VulpesPosted Dec 31, 2011, 8:13 AM
private void Form4_Load(object sender, EventArgs e)