如何在用户控件中以另一种形式调用mainform方法。 [英] How to call mainform method in another form in user control.

查看:320
本文介绍了如何在用户控件中以另一种形式调用mainform方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#中的windowsFrom。我试图在用户控制中的一个中调用mainfrom方法。

我有这样的主要内容



I am working with windowsFrom in c#. I am trying to call mainfrom method in one of the from in user control.
I have mainfrom like this

namespace Project
   {
     public partial class MainForm : Form
      {
       public MainForm()
       {
           InitializeComponent();
       }

       public void TempCommand()
         {
          StartTemp();
         }
      }
    }



我在用户控件中点击了按钮。当我点击该按钮时,它将打开另一个表单。我在用户控件中有这样的代码。




I have the button click in the user control. When i click that button then it will open another form. I have the code like this in the user control.

private TempCalib _tempCalib = new TempCalib();
 
       private void calibBtn_Click(object sender, EventArgs e)
        {
            _tempCalib.Show();
        }



它将打开另一个,我有一个按钮。当我点击此处的确定按钮时,我需要调用mainfrom方法。




it will open another from and i have one button in that from. I need to call mainfrom method when i click "Ok" button in this from.

namespace Project
  {
  public partial class TempCalib : Form
  {

      public TempCalib()
      {
          InitializeComponent();
      }

      private void OkButton_Click(object sender, EventArgs e)
      {
         // I need to call the mainfrom "TempCommand" method here.
          this.Hide();
      }


     }
   }



任何人都可以帮我怎么做。



谢谢。


Can anyone help me how to do this.

Thanks.

推荐答案

我希望usercontrol引发一个事件并订阅它主要形式。当事件被提出时,你打电话给你的方法。



格里夫在这里给你一般的想法:

在两种表格之间转移信息,第2部分:从孩子到父母 [ ^ ]
I would have the usercontrol raise an event and subscribe to it in the main form. When the event is raised, you call your method.

Griff gives you the general idea here:
Transferring information between two forms, Part 2: Child to Parent[^]


这篇关于如何在用户控件中以另一种形式调用mainform方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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