当我的表单在 C#(.Net Compact Framework)中完全加载时发出通知?

发布于 2024-10-03 08:26:58 字数 88 浏览 0 评论 0原文

我的应用程序中有一个表单,我想在表单

完全加载时进行一些处理,但加载完成后没有可以绑定的事件或内容。

有谁有任何想法,我该怎么做?

I have a form in my application and I want to do some processing when my form has been

Fully loaded but I have no event or something which I can bind to when load is finished.

Does anyone has any idea, how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇见了你 2024-10-10 08:26:58

“满载”到底是什么意思?您的意思是“加载”事件已成功进行?

您可以这样做:

public class MyForm : Form {
    protected override void OnLoad( EventArgs e ) {
        // the base method raises the load method
        base.OnLoad( e );

        // now are all events hooked to "Load" method proceeded => the form is loaded
        this.OnLoadComplete( e );
    }

    // your "special" method to handle "load is complete" event
    protected virtual void OnLoadComplete ( e ) { ... }
}

但是如果您的意思是“完全加载”,则“表单已加载并显示”,您也需要重写“OnPaint”方法。

public class MyForm : Form {
    private bool isLoaded;
    protected override void OnLoad( EventArgs e ) {
        // the base method raises the load method
        base.OnLoad( e );

        // notify the "Load" method is complete
        this.isLoaded = true;
    }

    protected override void OnPaint( PaintEventArgs e ) {
        // the base method process the painting
        base.OnPaint( e );

        // this method can be theoretically called before the "Load" event is proceeded
        // , therefore is required to check if "isLoaded == true"
        if ( this.isLoaded ) {
            // now are all events hooked to "Load" method proceeded => the form is loaded
            this.OnLoadComplete( e );
        }
    }

    // your "special" method to handle "load is complete" event
    protected virtual void OnLoadComplete ( e ) { ... }
}

What exaclty mean "fully loaded" ? Do you mean, the "Load" event was successfully proceeded?

You can do this:

public class MyForm : Form {
    protected override void OnLoad( EventArgs e ) {
        // the base method raises the load method
        base.OnLoad( e );

        // now are all events hooked to "Load" method proceeded => the form is loaded
        this.OnLoadComplete( e );
    }

    // your "special" method to handle "load is complete" event
    protected virtual void OnLoadComplete ( e ) { ... }
}

But if you mean "fully loaded" the "form is loaded AND shown" you need override the "OnPaint" method too.

public class MyForm : Form {
    private bool isLoaded;
    protected override void OnLoad( EventArgs e ) {
        // the base method raises the load method
        base.OnLoad( e );

        // notify the "Load" method is complete
        this.isLoaded = true;
    }

    protected override void OnPaint( PaintEventArgs e ) {
        // the base method process the painting
        base.OnPaint( e );

        // this method can be theoretically called before the "Load" event is proceeded
        // , therefore is required to check if "isLoaded == true"
        if ( this.isLoaded ) {
            // now are all events hooked to "Load" method proceeded => the form is loaded
            this.OnLoadComplete( e );
        }
    }

    // your "special" method to handle "load is complete" event
    protected virtual void OnLoadComplete ( e ) { ... }
}
ま柒月 2024-10-10 08:26:58

我认为 OnLoad 事件并不是您真正想要的,因为它发生在表单显示之前。您可以将 Application.IdleOnLoad 一起使用来创建 OnLoaded 事件:

protected override void OnLoad(EventArgs args)
{
    Application.Idle += new EventHandler(OnLoaded);
}

public void OnLoaded(object sender, EventArgs args)
{
   Application.Idle -= new EventHandler(OnLoaded);
   // rest of your code 
}

I think the OnLoad event isn't really what you want, as it occurs before the form is displayed. You can use Application.Idle with OnLoad to make an OnLoaded event though:

protected override void OnLoad(EventArgs args)
{
    Application.Idle += new EventHandler(OnLoaded);
}

public void OnLoaded(object sender, EventArgs args)
{
   Application.Idle -= new EventHandler(OnLoaded);
   // rest of your code 
}
笑忘罢 2024-10-10 08:26:58

我的方法类似于 TcKs 接受的答案。
我面临的问题是,我有一个用于一组控件的事件处理程序,这些控件通过事件处理程序在面板内移动控件来响应 VisibleChanged。问题是(当然),首次加载表单时可见性会发生变化 - 但在 .Load() 事件之后。

我为 Form: 创建并设置了一个 bool 值,

private bool formShown = false;

然后将以下行添加到 Form_Load() 中

this.Paint += (s, args) => { formShown = true; };

,其中 VisibleChanged() 事件处理程序的第一行为:

if(!formShown) { return; }

简洁且实用。

My approach is similar to the accepted answer from TcKs.
The problem I faced was that I had an event handler for a group of controls that responded to VisibleChanged with the event handler moving controls about within a Panel. Trouble was (of course) that the Visibility changes when the form is first loaded - but after the .Load() event.

I created and set a bool value for the Form:

private bool formShown = false;

and then added the following line to the Form_Load()

this.Paint += (s, args) => { formShown = true; };

with the first line of my VisibleChanged() event handler as:

if(!formShown) { return; }

Succinct and functional.

属性 2024-10-10 08:26:58

您可以使用表单的Load 事件。在表单的构造函数中编写以下行

this.Load += new System.EventHandler(this.Form1_Load);

,然后编写以下方法,您可以在其中执行一些操作。

void Form1_Load(object sender, EventArgs e)
{
    // do some stuff here.
}

You can use the Load event of the form. In the constructor of your Form write following line

this.Load += new System.EventHandler(this.Form1_Load);

and then write the following method in which you can do some stuff.

void Form1_Load(object sender, EventArgs e)
{
    // do some stuff here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文