简体   繁体   中英

C# Winform panel Shown event

Is there an event for panel that is equivalent to form event Shown? I had a few couple of panel switching within a form which will never be closed. However i couldn't find anything close to an event like Shown which is used in form. The closes i had is Paint event. However i only wish to update the panel once every time it is shown.

You could listen on the VisibleChanged event and only act on when visibility = true.

https://msdn.microsoft.com/en-us/library/system.windows.forms.panel_events%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

You could also experiment with the Enter and Invalidated events to see if these give you the results you want.

Or if disabling the panel when leaving it is an option, you might be able to use the EnabledChanged event in your toolbox.

Form.Shown is not raised every time the form is shown, rather it Occurs whenever the form is first displayed . This being said, there is no Panel.Shown event, and no event which is raised "whenever a panel is first displayed".

You can simulate this behavior with the Panel.Paint event, using a flag to keep track of whether it's been "shown" once before. This will make it behave similar to Form.Shown .

private bool panel1Painted = false;
private void panel1_Paint(object sender, PaintEventArgs e)
{
    if (!panel1Painted)
    {
        // do your shown stuff here
        panel1Painted = true;
    }
}

To keep in the spirit of Form.Shown , you may want to reset the flag if the Panel is reconstructed . This is not the same as shown .

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