C#-以父级形式调用函数 [英] C# - Call a function in a parent form

查看:49
本文介绍了C#-以父级形式调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一种形式以一种主要形式调用一个函数...已经通过以一种主要形式声明它的public static已经调用了一个简单的函数,但是我无法调用所需的一个.调用的函数:

I'm trying to call a function in a main form from another form... Already got to call a simple function, by declaring it public static in main form, yet I can't call the needed one. The function to call:

    public static void spotcall()
    {
        string dial = Registry.CurrentUser.OpenSubKey("SOFTWARE").OpenSubKey("INTERCOMCS").GetValue("DIAL").ToString();
        MainForm.txtSendKeys.Text = dial;// Here it asks me for a reference to an object.


        foreach (char c in txtSendKeys.Text)
        {
            sideapp.Keyboard.SendKey(c.ToString(), checkBoxPrivate.Checked);
        }
        txtSendKeys.Clear();
    }

我用来从子表单中调用它的过程:

The procedure I use to call it from a child form:

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Text = "Hoho";
        MainForm.spotcall();
    }

我完全承认我缺乏有关C#的理论,但是由于经常发生,我只需要为自己的工作做这些,因此,如果我偶然遇到自己无法解决的问题,我希望能得到帮助.谢谢:)

I completely admit that I lack some theory about C#, but as it often happens, I just have to do it for my work, so I expect to get help if by chance I don't get to the solution by myself. Thank you :)

推荐答案

我认为正确的方法是使用委托.这样,您的表单(窗口)不必了解父表单(可以从不同的父表单打开表单).

I think the correct way to do this is to use delegates. This way your form (window) does not have to know anything about the parent form (the form can be opened from different parent forms).

假设我们要在子窗体关闭时调用父窗体中的函数(未将窗体显示为模态).

Let's say we want to call a function in the parent form when the child form is closed (not showing the form as modal).

在子窗体的顶部创建一个委托:

At the top of your child form create a delegate:

    public delegate void CloseEvent();
    public CloseEvent WindowClosed;

创建表单关闭事件并让其调用您的委托人:

Create the form closing event and have it call your delegate:

    private void child_FormClosing(object sender, FormClosingEventArgs e)
    {
      WindowClosed();
    }

父表单中的按钮可以显示子表单并设置回调:

A button in the parent form can show the child form and set the callback:

    private ChildForm childform = null;

    private void buttonShowChildForm_Click(object sender, EventArgs e)
    {
      if (childform == null)
      {
        childform = new ChildForm();
        childform.WindowClosed += childClosed;
        childform.Show();
      } else
      {
        childform.BringToFront();
      }
    }

    private void childClosed()
    {
        childform = null;
    }

在此示例中,我们使用按钮打开不会阻止父表单的新表单.如果用户尝试第二次打开该表单,则只需将现有表单显示在最前面即可向用户显示.关闭表单后,将对象设置为null,以便下次单击按钮时将打开一个新表单,因为关闭时会丢弃旧表单.

In this example we use a button to open a new form that does not block the parent form. If the user tries to open the form a second time, we just bring the existing form to the front to show it to the user. When the form is closed we set the object to null so that next time we click the button a new form is opened because the old was disposed when closed.

最诚挚的问候汉斯·米林...

Best regards Hans Milling...

这篇关于C#-以父级形式调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆