简体   繁体   中英

How do I refresh a form after it is shown again?

I am creating a program in C# Windows Form Application.

Let me give you a scenario of what I am doing:

  1. Log into the program (login system)
  2. The program will determine the user's permission value (let's say I'm 3)
  3. Depending on the permission value, the main menu will show buttons 3a. If the user has permission value greater than 2, user will view all buttons 3b. If the user has permission value less than 2, user will see only 1 button
  4. When I logout, I am using .hide to hide the main menu and showing the login form again.
  5. I log in another user (with permission value = 1)
  6. All the buttons will show, not just only 1 like it should be.

Does anyone know how to "redo" the main menu after logging in, depending on permission value?

Maybe this?

        const int firstButtonY = 20;
        const int padding = 20;
        int currentY = firstButtonY;

        foreach (var control in this.Controls)
        {
            if (control.GetType() != typeof(System.Windows.Forms.Button))
                continue;

            var curButton = (Button) control;

            if (!curButton.Visible)
                continue;

            curButton.Top = currentY;
            currentY += padding + curButton.Height;

        }

Instead of open new instance (in my case, Form3) in Form1 in Form1_Load

frm3 = new Form3(this);

and show after specified event trigger

frm3.Show();

and cancel the Form3_Closing

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
     e.Cancel = true;
     this.Hide();
}

We do like this for every event triggered

frm3 = new Form3(this);
frm3.Show();

and comment the create new instance in Form1_Load

//frm3 = new Form3(this);

and comment the Hide form3 part

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
    //e.Cancel = true;
    //this.Hide();
}

because frm3.Show() after the form3 this.Hide() WON'T triggered

private void Form3_Load(object sender, EventArgs e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM