在 C# 中调用对话框方法形成静态类

发布于 2024-09-19 16:07:10 字数 142 浏览 5 评论 0原文

我必须从父对话框捕获一个事件,该事件使用静态类中的方法来更新数组。

我从这个对话框中调用一个子对话框,在列表中显示数组。

我知道使用变量当前对话框是子对话框还是父对话框,但是从静态类中的方法如何在捕获事件时调用子对话框内的方法来更新列表?

I have to catch an event from a parent dialog that uses a method from a static class to update an array.

From this dialog I call a child dialog that shows the array in a list.

I know with a variable if the current dialog is the child or the parent dialog, but from the method in the static class how can I call the method inside the child dialog to update the list when I catch the event?

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

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

发布评论

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

评论(3

戏蝶舞 2024-09-26 16:07:10

您可以将对话框的实例传递到静态方法中,如下所示。但是,如果您对对话框的方法进行静态方法调用,我肯定会重新访问您的体系结构。

public static class MyStaticClass
{
   public static void SomeMethod(Form myDialog)
   {
      myDialog.SomeMethodOnTheDialog();
   }
}

You could pass the instance of the dialog into the static method like below. I would definitely revisit your architecture if you're having a static method call into your dialog's methods however.

public static class MyStaticClass
{
   public static void SomeMethod(Form myDialog)
   {
      myDialog.SomeMethodOnTheDialog();
   }
}
夕色琉璃 2024-09-26 16:07:10

您必须修改静态方法以将对话框的实例作为参数:

public static void UpdateArray(Dialog instance)
{
     // Initialize what you need

     instance.DoSomething();
}

然后将其全部放入事件处理程序中,如下所示:

public void Dialog_EventHandler(object sender, EventArgs e)
{
    Dialog dialog = (Dialog) sender;
    UpdateArray(dialog);
}

You'll have to modify your static method to take an instance of the Dialog as a parameter:

public static void UpdateArray(Dialog instance)
{
     // Initialize what you need

     instance.DoSomething();
}

Then you would all it in your Event Handler like:

public void Dialog_EventHandler(object sender, EventArgs e)
{
    Dialog dialog = (Dialog) sender;
    UpdateArray(dialog);
}
梦回梦里 2024-09-26 16:07:10

在子对话框的类中,您必须创建公共方法来执行您需要它们执行的操作。

然后,您只需从静态类中调用这些公共方法

这是一个小示例,我从静态类中调用了公共方法(在我的主窗体中)。我认为这与你正在做的事情相似。


主表单片段

public static void WriteToTextBox(string message)
{
    TextBox myLog = (TextBox)CITX12Parser.Main.ActiveForm.Controls.Find("txtLog", true).First();
    myLog.Text = message;
}

静态类片段

public class LoggingUtils
{
    public static void TestLog(string msg)
    {
        Main.WriteToTextBox(msg + Environment.NewLine);
    }
}

Inside the class for the child dialog, you have to create public methods that do what you need them to do.

Then, you just call those public method from the static class.

This is a mini-example where I called a public method (in my Main form) from a static class. I think this is similar to what you are doing.


Main Form Snippet

public static void WriteToTextBox(string message)
{
    TextBox myLog = (TextBox)CITX12Parser.Main.ActiveForm.Controls.Find("txtLog", true).First();
    myLog.Text = message;
}

Static class snippet

public class LoggingUtils
{
    public static void TestLog(string msg)
    {
        Main.WriteToTextBox(msg + Environment.NewLine);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文