Search code examples
c#winformsparent-childmdiparent

How to increase the name of a new form when opening a new child form


How do you change the text in the Titlebar in Windows Forms?

When I open a new form I want it to say "Bob, the next time i click new and open a form it should say "Bob1" I tried to use TryParase() it to string and that wont work.

private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form2 childform2 = new Form2();
        decimal childcount;
       childform2.MdiParent= this;
       string menuname;
       menuname = "untilted" + childcount.ToString();
        childform2.Text = menuname;
        childform2.Show();
        childcount++;

    }

Solution

  • You need to keep childcount around longer than just that method. Since you declare it inside the method, it will reset to 0 each time you run this code. Declare the variable outside of the method, so something like this:

    int childcount=1;
    private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
       Form2 childform2 = new Form2();
    
       childform2.MdiParent= this;
       string menuname;
       menuname = "untilted" + childcount.ToString();
       childform2.Text = menuname;
       childform2.Show();
       childcount++;
    }