在 .NET WinForms 中的 Form_Load 事件之前或之后执行组件代码?

发布于 2024-08-21 09:00:56 字数 579 浏览 5 评论 0原文

我有一个组件与处理某些内部翻译的另一个组件发生冲突。

所发生的情况是,业务逻辑组件被执行,然后翻译系统执行,它拾取表单中现在存在的文本,并尝试翻译它们。

问题是文本现在包含数字、日期、数据库相关文本等内容,但在执行业务逻辑之前,这些控件要么是空白的,要么包含准备翻译的虚拟文本。

我想知道如何让我放在表单上的组件在表单生命周期的特定时间执行其代码。

例如,如果我从业务逻辑组件中删除有问题的代码,以便它在构造时不执行任何操作,然后进行翻译工作,如果我随后调用将该代码从 Form_Load 移至的方法,那么效果也很好。

所以我想知道,有没有一种方法适合我,从组件(即源自 组件,不是可视控件),在 Form_Load 之前或之后执行代码,或者至少在设置所有其他组件和控件之后执行代码?

它似乎没有从组件内部返回到表单的任何链接,因此对我来说,挂钩 Load 事件似乎也是不可能的。

还是我找错了树?

I have a component that is in conflict with another component that handles some internal translations.

What happens is that business logic components gets executed, then the translation system executes, which picks up texts now present in the form, and attempts to translate them.

The problem is that the text now contains things like numbers, dates, database-related text, etc. but before the business logic was executed, those controls was either blank or contained dummy-text prepared for translation.

I'm wondering how I can have a component that I drop on a form execute its code at a specific time in the lifetime of the form.

For instance, if I remove the offending code from the business logic component, so that it doesn't do anything upon construction, then translation work, and if I then call the method I moved that code to from Form_Load, that too works nicely.

So I'm wondering, is there a way for me, from a component (ie. descended from Component, not a visual control), to execute code just before or after Form_Load, or at least after all other components and controls have been set up?

It doesn't appear to be any link from within the component back to the form, so for me to hook into the Load event doesn't seem possible either.

Or am I barking up the wrong tree?

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

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

发布评论

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

评论(1

风吹雪碎 2024-08-28 09:00:56

我不清楚为什么不能通过从表单的 Load 事件处理程序显式调用组件的方法来解决这个问题。

让组件知道它被放置到的表单是可能的。可以引导设计者将组件的属性初始化为表单实例。例如,这是 ErrorProvider 使用的一种技术,它在表单上绘制错误图标。然后可以扩展它以让组件订阅表单的 Load 事件。这是一个例子:

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
  public partial class Component1 : Component {
    public Component1() {
      InitializeComponent();
    }
    public Component1(IContainer container) {
      container.Add(this);
      InitializeComponent();
    }

    [Browsable(false)]
    public ContainerControl Host {
      get { return mHost; }
      set {
        if (mHost == null && value as Form != null && !DesignMode) {
          (value as Form).Load += Initialize;
        }
        mHost = value;
      }
    }
    private ContainerControl mHost;

    public override ISite Site {
      set {
        base.Site = value;
        if (value != null) {
          // Assign Host at design time
          IDesignerHost service = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
          if (service != null) {
            IComponent rootComponent = service.RootComponent;
            this.Host = rootComponent as ContainerControl;
          }
        }
      }
    }
    private void Initialize(object sender, EventArgs e) {
      Console.WriteLine("initializing...");
    }
  }
}

It isn't clear to me why you couldn't just solve this by explicitly calling a component's method from the form's Load event handler.

Making a component aware of the form on which it is dropped is possible. The designer can be coaxed to initialize a property of the component to the form instance. This is a technique used by ErrorProvider for example, it paints the error icons on the form. This could then be extended to let the component subscribe to the form's Load event. Here is an example:

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
  public partial class Component1 : Component {
    public Component1() {
      InitializeComponent();
    }
    public Component1(IContainer container) {
      container.Add(this);
      InitializeComponent();
    }

    [Browsable(false)]
    public ContainerControl Host {
      get { return mHost; }
      set {
        if (mHost == null && value as Form != null && !DesignMode) {
          (value as Form).Load += Initialize;
        }
        mHost = value;
      }
    }
    private ContainerControl mHost;

    public override ISite Site {
      set {
        base.Site = value;
        if (value != null) {
          // Assign Host at design time
          IDesignerHost service = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
          if (service != null) {
            IComponent rootComponent = service.RootComponent;
            this.Host = rootComponent as ContainerControl;
          }
        }
      }
    }
    private void Initialize(object sender, EventArgs e) {
      Console.WriteLine("initializing...");
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文