Question Naming and Saving Child Windows

Alex_D

New member
Joined
Dec 26, 2019
Messages
4
Programming Experience
Beginner
Hi,

I am studying C#/WPF. I am trying to create child windows upon click event as follows

Child Window:
private void NewWindow(object sender, RoutedEventArgs e)
    {
        
        Window newWindow = new Window();
        newWindow.Owner = this;
        newWindow.Title = "Window 1";
        newWindow.ShowInTaskbar = false;
        newWindow.Topmost = true;
        newWindow.Show();
        newWindow.Width = 500;
        newWindow.Height = 300;
    }

However I can not figure out how can I individually name the child windows in order to store them.

my thinking was to generate variable upon click event, but can not generate a variable from inside the method.

Can anybody give me any hints or direct me towards proper resources.

Any help is highly appreciated.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,175
Location
Chesapeake, VA
Programming Experience
10+
Do you need the number incrementing between sessions, or just need the number to start from 1 at the start of each session and increment from there?

In general the concept is easy: Have a static variable that you bump up and store. Pseudocode below:

C#:
class MyView
{
    static int WindowNumber { get; set; }

    MyView()
    {
         WindowNumber = 1;
         // or load the last number used from settings file
    }

    void CreateNewWindow( .... )
    {
        :
        newWindow.Title = $"Window {WindowNumber}";
        :
        IncrementWindowNumber();
    }

    void IncrementWindowNumber()
    {
        // this actually bumps up the window number
        WindowNumber++;

        // save this new number into the settings file if you want the number to cross sessions
    }
}
 
Top Bottom