N
N
noob2222020-12-03 15:04:27
C++ / C#
noob222, 2020-12-03 15:04:27

How to do it in c# windows forms?

Good day. There is a form, like the main button in it, and by clicking the button, another form is called in panel
. This is how I add the form

private void openFormBody(object formhi)
        {
            if (this.panel2.Controls.Count > 0)
                this.panel2.Controls.RemoveAt(0);
            Form f = formhi as Form;
            f.TopLevel = false;
            f.Dock = DockStyle.Fill;
            this.panel2.Controls.Add(f);
            this.panel2.Tag = f;
            f.Show();
        }

it all works fine, but when I decided to add a logout button in the form that I add to the panel and if I click on this button, another form (authorization) will open, and that form will simply remain, but the form with the button will close output. Perhaps it is difficult and unclear to explain, but I apologize.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
noob222, 2020-12-04
@noob222

Figured it out myself! You need to transfer another form to the form and then simply the form that you have already transferred already attribute this to .Hide();

D
DeOxygen, 2020-12-03
@DeOxygen

Option 1: Use a global form variable
Option 2: Use something like this in the close button

foreach (Control item in panel1.Controls)
    {
        item.Close()
        panel1.Controls.Remove(item); 
    }

R
Rasse, 2020-12-03
@Rasse

if (this.panel2.Controls.Count > 0)
                this.panel2.Controls.RemoveAt(0);
            Form f = formhi as Form;
            f.TopLevel = false;
            f.Dock = DockStyle.Fill;
            this.panel2.Controls.Add(f);
            this.panel2.Tag = f;
            f.Show();
        f.Closed += (s, args) => this.Close();

Pay attention to the last line in general)
Kill the main form by closing the additional

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question