0

Can we open second form on first form's window (hiding first form ) in windows form (.net c#) ??? Detailed problem: suppose I have Form-A. Form-A has next button on click of which I want to open Form-B (on same window, hiding Form-A) Also on Form-B, I can have back button on click of which we will get previous form (Form-A in this case).

I found this in MS access. Can we implement same in .Net windows form???

Akshay C
  • 53
  • 6
  • 1
    So you want to make a wizard dialog ? – TheGeneral Jul 29 '19 at 08:58
  • 2
    Form has a `Hide` method but you may also want to consider using some `UseControl`s to load different pages of the wizard in the same form. You can also take advantage of a tab control which doesn't show tab headers as container for wizard pages. You can see such control [here](https://stackoverflow.com/a/2342320/3110834) or a bit enhanced [here](https://stackoverflow.com/a/40633229/3110834). – Reza Aghaei Jul 29 '19 at 08:59
  • if you want to make wizard dialog, it's better to use from `UserControl`. Nevertheless, in the `Form`, you can to use `Hide` and `Show` Methods in `Form`. – AminRostami Jul 29 '19 at 09:03

2 Answers2

2

Are you sure you'd like to do this with multiple different Forms?

With WinForms you could achieve this like when click next, you hide your current form, let it be FormA and show the other form, let it be FormB at FormA's position.

private void buttonNext_FormA(object sender, EventArgs e)
{
  frmB.StartPosition = FormStartPosition.Manual;
  frmB.Location = new Point(this.Location.X, this.Location.Y);
  frmB.Show();
}

private void buttonBack_FormB(object sender, EventArgs e)
{
  frmA.StartPosition = FormStartPosition.Manual;
  frmA.Location = new Point(this.Location.X, this.Location.Y);
  frmA.Show();
}

A little different but a general solution for navigation next-back-next is the TabControl: Place a TabControl to your Form, place Next and Back Buttons below it. You can add multiple Tabs to your TabControl and you can place any Control inside your Tabs that you could put on a Form. (To hide Tab headers, set the following properties):

tabControl.Appearance = TabAppearance.FlatButtons;
tabControl.ItemSize = new Size(0, 1);
tabControl.SizeMode = TabSizeMode.Fixed;

Subscribe Back button click and Next button click event like this:

private void btnBack_Click(object sender, EventArgs e)
{
  if (tabControl.SelectedIndex > 0)
    tabControl.SelectedIndex--;
}

private void btnNext_Click(object sender, EventArgs e)
{
  if(tabControl.SelectedIndex < tabControl1.TabPages.Count)
    tabControl.SelectedIndex++;
}

But you could achieve this by WPF(Windows Presentation Foundation) NavigationWindow, that's also a cool technology and it's made exactly for this purpose. There is a little tutorial ad MSDN for implement navigation window: here

koviroli
  • 1,422
  • 1
  • 15
  • 27
0

You have two separate forms, so what you can do is:

  • When clicking the Next button in Form-A, Form-B is shown and Form-A is hidden.
  • When clicking the Previous button in Form-B, exactly the opposite happens.

This can be easily achieved using the Show and Hide methods of Forms.

  • In Next button click event handler:
this.Hide();
FormB.Show();
  • In Previous button click event handler:
this.Hide();
FormA.Show();
Youssef13
  • 3,836
  • 3
  • 24
  • 41