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

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

问题描述

我正在使用c#中的windowsFrom.我试图在用户控件中的from之一中调用mainfrom方法.我有这样的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.

谢谢.

推荐答案

快速解答

只需在辅助表单中添加对主要表单的引用:

Quick answer

Just add a reference to the primary form in your secondary form:

public partial class TempCalib : Form
{
    private  MainForm _main

    public TempCalib(MainForm main) : this()
    {
        _main = main;
    }

    /// Other stuffs
}

然后在构造辅助表单时分配值:

Then assign value when you construct your secondary form:

private TempCalib _tempCalib;
private void calibBtn_Click(object sender, EventArgs e)
{
     if (_tempCalib == null)
         _tempCalib = new TempCalib(this);

     _tempCalib.Show();
}

如果 calibBtn_Click 不在 MainForm 内(但位于 UserControl 内),则可以替换 _tempCalib 初始化:

If calibBtn_Click isn't inside MainForm (but it's inside a UserControl on it) then you can replace _tempCalib initialization with:

_tempCalib = new TempCalib((MainWindow)FindForm());

然后您就可以调用主要表格:

You'll be then able to call the primary form:

private void OkButton_Click(object sender, EventArgs e)
{
    _main.TempCommand();  
    this.Hide();
}

注意:这只是一个选项,您还可以创建一个属性来保存 MainForm 引用(这样,二级表单就可以重用,并且对设计人员更友好),此外还可以是 TempCalib 不是 UserControl ,而是 Form (很原始,但对于 UserControl ,您可以只检查其父Form并将其转换为正确的类型).

Notes: this is just one option, you may create a property to hold MainForm reference (so secondary form can be reused and it'll be more designer friendly) moreover TempCalib is not an UserControl but a Form (pretty raw but for an UserControl you may just check its parent Form and cast it to proper type).

这类参考通常是一个警告.通常,UI组件不应该如此耦合,公共的 Form 经常执行某项操作的方法是发出信号,表明您的 Form 逻辑太多.如何改善呢?

Such kind of references are often an alert. Usually UI components shouldn't not be so coupled and a public Form's method to perform something very often is the signal that you have too much logic in your Form. How to improve this?

1.十足的控制权.好吧,第一步可能是将它们分离一些,只需在 TempCalib 中添加一个事件,并将 MainForm 设置为其接收者:

1. DECOUPLE CONTROLS. Well a first step may be to decouple them a little bit, just add an event in TempCalib and make MainForm its receiver:

public partial class TempCalib : Form
{
    public event EventHandler SomethingMustBeDone;

    private void OkButton_Click(object sender, EventArgs e)
    {
        OnSomethingMustBeDone(EventArgs.Empty); / TO DO
       this.Hide();
    }
}

然后使用 MainForm :

private TempCalib _tempCalib;
private void calibBtn_Click(object sender, EventArgs e)
{
     if (_tempCalib == null)
     {
         _tempCalib = new TempCalib();
         _tempCalib.SomethingMustBeDone += _tempCalib_SomethingMustBeDone;

         // In _tempCalib_SomethingMustBeDone you'll invoke proper member
         // and possibly hide _tempCalib (remove it from OkButton_Click)
     }

     _tempCalib.Show();
}

2.来自控件的十进制逻辑.UI经常更改,逻辑不会更改(并且更改时可能与UI不并行).这只是第一步(现在 TempCalib 尚不知道谁会使用它).下一步(当表单中发生太多事情时执行)是从表单本身中删除这种逻辑.小例子(非常原始),将 TempCalib 保持为以前的状态(带有事件),然后将 MainForm 更改为 passive :

2. DECOUPLE LOGIC FROM CONTROLS. UI changes pretty often, logic not (and when it changes probably isn't in parallel with UI). This is just the first step (now TempCalib isn't aware of who will use it). Next step (to be performed when too much things happen inside your form) is to remove this kind of logic from the form itself. Little example (very raw), keep TempCalib as before (with the event) and change MainForm to be passive:

public partial class MainForm : Form
{
    public event EventHandler Calibrate;

    protected virtual void OnCalibrate(EventArgs e)
    {
        // TODO
    }
}

现在让我们创建一个类来控制流程和逻辑:

Now let's create a class to control the flow and logic:

public class MyTaskController
{
    private MainForm _main;
    private TempCalib _tempCalib;

    public void Start()
    {
        _main = new MainForm();
        _main.Calibrate += OnCalibrationRequested;
        _main.Show(); // Or whatever else
    }

    private void OnCalibrationRequested(object sender, EventArgs e)
    {
        if (_tempCalib == null)
        {
            _tempCalib = new TempCalib();
            _tempCalib.SomethingMustBeDone += OnSomethingMustBeDone();
        }

        _tempCalib.Show();
    }

    private OnSomethingMustBeDone(object sender, EventArgs e)
    {
        // Perform the task here then hide calibration window
        _tempCalib.Hide();
    }    
}

是的,您需要编写更多的代码,但这将从UI本身解耦逻辑(例如,对操作的响应).程序长大后,这将帮助您根据需要更改UI,从而使逻辑不知道该UI(并且在一个定义明确的位置).我什至没有提到,这将允许您使用不同的资源(人员)来编写逻辑和UI(或为不同的UI,WinForms和WPF进行重用逻辑),例如).无论如何,IMO最明显,最可取的收益就是... 可读性:您将始终知道逻辑在哪里以及UI管理在哪里,无需搜索,没有混乱,没有错误.

Yes, you'll need to write much more code but this will decouple logic (what to do as response to an action, for example) from UI itself. When program grows up this will help you to change UI as needed keeping logic unaware of that (and in one well defined place). I don't even mention that this will allow you to use different resources (people) to write logic and UI (or to reuse logic for different UI, WinForms and WPF, for example). Anyway IMO the most obvious and well repaid benefit is...readability: you'll always know where logic is and where UI management is, no search, no confusion, no mistakes.

3.实施过程中产生的十足逻辑.同样,您还有更多步骤要执行(需要时).您的 controller 仍然知道具体的类型( MainForm TempCalib ).如果需要在运行时选择其他形式(例如,具有复杂的界面和简化的形式,或者使用依赖注入),则必须使用界面解耦控制器.只是一个例子:

3. DECOUPLE LOGIC FROM IMPLEMENTATION. Again you have more steps to perform (when needed). Your controller is still aware of concrete types (MainForm and TempCalib). In case you need to select a different form at run-time (for example to have a complex interface and a simplified one or to use dependency injection) then you have to decouple controller using interfaces. Just an example:

public interface IUiWindow
{
    void Show();
    void Hide();
}

public interface IMainWindow : IUiWindow
{
    event EventHandler Calibrate;
}

public interface ICalibrationWindow : IUiWindow
{
    event EventHandler SomethingMustBeDone;
}

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

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