Show a message box after usercontrol fully display

2019-09-09 16:45发布

问题:

I have a control form derived from a windows User Control class. I need to show a message box based on a condition once the form displayed. I tried to use the form paint event handler to do this but it seems firing twice. As a result message box displayed twice. How can this be done?

public partial class SelectAccounts : UserControl
{
    private void SelectAccounts_Paint(object sender, PaintEventArgs e)
    {
         MessageBox.Show("something");
    }
}

回答1:

I've deleted my previous answer you may try the below code. Using variable to remember if the user control is loaded or not.

    public partial class SelectAccounts : UserControl
    {
        bool _Shown = false;
        private void SelectAccounts_Paint(object sender, PaintEventArgs e)
        {
            if (!this._Shown)
            {
                this._Shown = true;
                MessageBox.Show("something");
            }
        }
    }