0

I'm trying to make multiple pages on the form using panel control in c#. First i set the index variable inside the class Form1

List<Panel> listPanel = new List<Panel>();
int index;

And to go back and forward i created buttons. To go to the next panel:

private void btnNext_Click(object sender, EventArgs e)
    {
        if (index < listPanel.Count - 1)
    
        listPanel[++index].BringToFront();
    
    }

And for going back:

private void btnBack_Click(object sender, EventArgs e)
    {
        if (index > 0)
    
        listPanel[--index].BringToFront();
    }

And this set of codes to decide which panel should be seen on top according to index:

    private void Form1_Load(object sender, EventArgs e)
    {
        
        listPanel.Add(panel1);
        listPanel.Add(panel2);
        listPanel.Add(panel3);
        listPanel[index].BringToFront();
    }

But it doesn't work. I can not go to next or previous panel by clicking the buttons. Where am i doing wrong?

  • 1
    What do you mean by it doesn't work ? – Paul Karam Mar 25 '22 at 14:04
  • Are you adding your Panels to a Container Control, at some point? One at a time (the one pointed by `index`) or all of them? Are the Panels already child of a Container, all set in the Designer. -- As already asked, what exactly is *not working*? (add the information to the body of the question) – Jimi Mar 25 '22 at 14:12
  • You've described how you're trying to solve the issue that you're facing, but failed to describe what the issue is that you're trying to solve. Please describe what you are trying to achieve. The following may be useful: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Tu deschizi eu inchid Mar 25 '22 at 14:22
  • I created multiple panels and i want to switch between them. The issue is i can not go to next or previous panel by clicking the buttons. (i thought i was clear enough but i wasn't i guess so i edited my question. thanks for the warning) – Diana Kurban Mar 25 '22 at 15:03
  • Is there some reason you do not use a `TabControl` as opposed to a bunch of panels stacked on top of each other? – JohnG Mar 25 '22 at 15:05
  • @DianaKurban: Not sure that your comments add any clarity. Why have you created multiple panels? Why are you trying to switch between the panels? Are you developing a quiz (or something similar)? Sometimes there is more than one way to achieve a similar outcome-some approaches may be easier than others. It's said that a picture is worth a thousand words. Adding an image of your Form may be helpful. – Tu deschizi eu inchid Mar 25 '22 at 15:11
  • Panel is not ideal, too likely to cause a design-time accident. Where the added panel is *inside* the first panel instead of overlapping it. So can't be visible when the first panel is not in front. Fix with View > Other Windows > Document Outline. Or consider UserControls instead. Or use [this trick](https://stackoverflow.com/a/2798241/17034). – Hans Passant Mar 25 '22 at 15:12
  • The posted code appears to work as expected. As Hans suggested… Are you sure that one or more of the panels is NOT contained inside one of the other panels? If you are doing this in the designer, then you can check the designer code to see if one panel is getting added to another panel. It would look something like… `this.panel1.Controls.Add(this.panel2)` … – JohnG Mar 25 '22 at 15:36

0 Answers0