Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three windows form with same CS name like Collateral Welcome.cs
Now I implemented one Back button in 3rd screen, as per business requirement it must go to 2nd screen but where as I am able to navigate it to 1st screen.I am really confused how to implement this.

It should navigate to 2nd Screen rather than 1st Screen and back button present in 3rd screen.

Please help me how to write code for this ?

Thanks in Advance

What I have tried:

C#
private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
            CollateralWelcome CW = new CollateralWelcome(); <----this is navigating to 1st Screen
            CW.Show();
            }
Posted
Updated 18-May-21 19:38pm
v2

1 solution

Create the forms at the begin of your class (outside the methods) like this:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    /// <summary>
    /// Test form with Back and Next buttons.
    /// </summary>
    public partial class Form2 : Form
    {
        static Form2 cwForm1;
        static Form2 cwForm2;
        static Form2 cwForm3;
        static int formNumber = 1;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (cwForm2 == null)
            {
                cwForm1 = this;
                cwForm1.labelNr.Text = "1";
                cwForm1.Text = "Test form 1";
                cwForm1.BackColor = Color.LightSkyBlue;
                cwForm2 = new Form2();
                cwForm2.Text = "Test form 2";
                cwForm2.labelNr.Text = "2";
                cwForm2.BackColor = Color.Beige;
                cwForm3 = new Form2();
                cwForm3.Text = "Test form 3";
                cwForm3.labelNr.Text = "3";
                cwForm3.BackColor = Color.LightGreen;
            }
        }

        private void ButtonBack_Click(object sender, EventArgs e)
        {
            if (formNumber > 1)
            {
                formNumber--;
                ChangeForm();
            }
        }

        private void ButtonNext_Click(object sender, EventArgs e)
        {
            if (formNumber < 3)
            {
                formNumber++;
                ChangeForm();
            }
        }

        private void ChangeForm()
        {
            this.Hide();

            if (formNumber == 1)
            {
                cwForm1.Show();
            }
            else if (formNumber == 2)
            {
                cwForm2.Show();
            }
            else
            {
                cwForm3.Show();
            }
        }
    }
}
 
Share this answer
 
v6
Comments
Thirumadhi T Johnson 19-May-21 2:52am    
Hi Rick
After this how do I write in
private void btnBack_Click(object sender, EventArgs e)
{
this.Hide();
CollateralWelcome CW = new CollateralWelcome();
CW.Show();
}

Please suggest me as I am new in Coding
RickZeeland 19-May-21 3:38am    
There still seem to be some problems, working on it ...
Thirumadhi T Johnson 19-May-21 5:48am    
Ok
Thirumadhi T Johnson 20-May-21 3:01am    
Hi Rick...waiting for your response.Any luck
RickZeeland 20-May-21 3:09am    
Yes, see the updated solution above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900