2

I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.

Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.

Here is code for my windows:

First Window

public partial class Presenation_1 : Window
{
    Presentation_2 p2 = new Presentation_2();
    MainWindow M = new MainWindow();

    public Presenation_1()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p2.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        M.Show();
    }
}

Second Window

public partial class Presentation_2 : Window
{
    Presentation_3 p3 = new Presentation_3();
    Presenation_1 p1 = new Presenation_1();
    public Presentation_2()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p3.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p1.Show();
    }
}

Third Window

public partial class Presentation_3 : Window
{
    Presentation_4 p4 = new Presentation_4();
    Presentation_2 p2 = new Presentation_2();

    public Presentation_3()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p4.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p2.Show();
    }
}

Fourth Window

public partial class Presentation_4 : Window
{
    Presentation_3 p3 = new Presentation_3();
    MainWindow M = new MainWindow();

    public Presentation_4()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        M.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p3.Show();
    }
}

Thanks in advance

1

2 Answers 2

4

Don't create your Windows before the button is clicked, you can create them in the event handler:

private void btnForward_Click(object sender, RoutedEventArgs e)
{
    var p2 = new Presentation_2();
    this.Close();
    p2.Show();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this appeared to work fine, I don't know why I didn't think of creating it here.
1

When you create a windows, you create 2 other windows with

new Presentation_X()

This new windows is automaticaly show and itself open 2 other windows.

You can create this windows once in the Mainwindow (auto hide this one), pass the reference in constructor and not close these windows. Quick example (not tested) :

public partial class Presenation_X : Window
{
    private Window preview;
    private Window next;

    public Presenation_X(Window w1, Window w2)
    {
        this.preview = w1;
        this.next = w2;

        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.next.Show();
        this.Hide();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.preview.Show();
        this.Hide();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.