Ak C
Ak C

Reputation: 53

can we open second form on same window of first form in .Net windows form?

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???

Upvotes: 0

Views: 745

Answers (2)

koviroli
koviroli

Reputation: 1453

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

Upvotes: 2

Youssef13
Youssef13

Reputation: 4986

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();

Upvotes: 0

Related Questions