move back and forth on multiple panels

ankit goel 746 Reputation points
2022-11-09T13:25:56.457+00:00

I have a panel (named main panel) ** inside which i have adjusted 3 panels **(namely panel1, panel2 and panel3 respectively) ** with the help of document outline windows so that they all are placed at same location, one over another but not inside each other. When the form is loaded, only panel 1 is visible, rest panel 2 and panel 3 are hidden, now on clicking button named button1 inside panel 1, it opens panel2 and hides panel 1. similarly on clicking button named button2 inside panel2, it opens panel3 and hides panel2. and finally, when i click button3 or button 4 or button5 (these 3 are inside panel3) inside panel3, they open a new form. Now the problem is i have managed to move forth by clicking all these buttons(**or by capturing keystrokes) starting from panel1 but don't know how to move back to previous panel like from panel3 to panel 2 and from panel2 to panel1 using keystrokes. Please suggest me a way to achieve this using escape key .

    public partial class directionform : Form  
{  

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
    {  
        switch (keyData)  
        {  

            case Keys.I:  
                // Simulate clicks on button1.  
                //  button1.PerformClick();  
                buttonwithcue1.PerformClick();  
                break;  

            case Keys.U:  
                // Simulates clic on button2.  
                button2.PerformClick();  
                break;  
            case Keys.L:  
                buttonwithcue2.PerformClick();  
                return true;  
            //e.Handled = true;  
            // break;             
            case Keys.C:  
                buttonwithcue3.PerformClick();  
                return true;  
            //e.Handled = true;  
            // break;     
            case Keys.A:  
                buttonwithcue4.PerformClick();  
                return true;  
            //e.Handled = true;  
            // break;     
            case Keys.D:  
                buttonwithcue5.PerformClick();  
                return true;  
            //e.Handled = true;  
            // break;     

            default:  
                break;  
  if (keyData == Keys.Escape)  
        {  
            **// how to move back on pressing escape key**   
        }  
        return base.ProcessCmdKey(ref msg, keyData);  
        }  
   private void directionform_Load(object sender, EventArgs e)  
    {  
        panel5.Hide();  
        panel6.Hide();  

    }  

   private void buttonwithcue1_Click(object sender, EventArgs e)  
    {  
        panel1.Hide();  
        panel2.Show();  
     }  

      private void buttonwithcue2_Click(object sender, EventArgs e)  
    {  
        panel2.Hide();         
        panel3.Show();  

    }  

 // all three below buttons belong to panel 3 (last panel )   
            private void buttonwithcue3_Click(object sender, EventArgs e)  
        {             
        panel3.Hide();            
      // go to next form    
    }  
    private void buttonwithcue4_Click(object sender, EventArgs e)  
    {             
        panel3.Hide();             
     // go to next form    
    }  
    private void buttonwithcue5_Click(object sender, EventArgs e)  
    {  
       panel3.Hide();           
     // go to next form    
    }  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,826 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,230 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 31,516 Reputation points Microsoft Vendor
    2022-11-10T09:29:31.023+00:00

    Hi @ankit goel , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    if (keyData == Keys.Escape)  
    {  
    	**// how to move back on pressing escape key**   
    	if (panel2.Visible == true)  
    	{  
    		panel1.Show();  
    		panel2.Hide();  
    	}  
    	else if (panel3.Visible == true)  
    	{  
    		panel2.Show();  
    		panel3.Hide();  
    	}  
    }  
    

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful