如何从form3访问form1中的函数 [英] How to access a function in form1 from form3

查看:104
本文介绍了如何从form3访问form1中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......

我有三种形式(Form1,Form2,Form3).Form1包含一个函数,串口。从串行端口收到1时,将打开Form2。在form2的按钮单击中打开Form3。现在我想从form3访问form1中的函数。如何在不使用的情况下完成

Form1 f = new form1();

f.funcname();

解决方案

首先,我建议你研究谢尔盖在回复中提供给你的参考资料,特别研究他使用一个接口选择性地将一个对象/实体/表格/方法的字段,属性或方法暴露给另一个的例子。 。



此外,OriginalGriff还有一系列三篇文章,于2013年9月在CodeProject上发布,涵盖了一个表单可以与另一个表单进行通信的不同方式。这是第一篇文章的链接:[ ^ ]。我强烈建议你学习他的优秀文章。



我会告诉你另一种方式,在这个回应中,实现你的目标。使用此技术,您将在Form1的实例中插入指向Form1实例中的方法的指针,因此Form3的实例可以直接使用它。请注意,在此技术中,Form1实例中方法的定义可以声明为private。



这里是实例代码中方法的定义Form1(主窗体):

  public   void  MethodCalledFromForm3( string  s1, string  s2)
{
MessageBox.Show( 从Form3结果调用的函数: + s1 + s2);
}

为了使这个在Form3的实例中可用,你创建我们在Form3的代码中为一个方法定义一个模板,使用.NET的'Action对象[ ^ ]:

  public 操作<字符串,字符串> MethodInForm1ToCall; 

我们现在在Form3实例中创建了一种套接字,可以将引用(指针)插入任何带有两个字符串参数的Method,并返回'void。从技术上讲,动作是一种代表形式,您在学习MSDN链接时将学到它;它的对应部分采用可变数量的参数并返回某个Type的值,是'Func。



那么,我们如何在Form1中获取签名与Form3中的socket匹配插入到Form3的实例?这很简单:

 Form3 InstanceOfForm3 =  new  Form3(); 

private void Form1_Load( object sender,System.EventArgs e)
{
InstanceOfForm3.MethodInForm1ToCall = MethodCalledFromForm3;
}

你会注意到我故意没有尝试将你在代码中所做的事情与这里显示的内容相匹配;这个的原因是我希望能够提供一个更普遍有用的例子。



现在,让我们把它们放在一个大图片视图:中lang =cs> public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private Form2 InstanceOfForm2 = new Form2();
private Form3 InstanceOfForm3 = new Form3();

private void Form1_Load( object sender,System.EventArgs e)
{
InstanceOfForm2.PropertyInstanceOfForm3 = InstanceOfForm3;

InstanceOfForm3.MethodInForm1ToCall = MethodCalledFromForm3;
}

// 显示Form2的实例
private void button1_Click( object sender ,System.EventArgs e)
{
InstanceOfForm2.Show();
}

// 在Form3中调用的方法
private void MethodCalledFromForm3( string s1, string s2)
{
MessageBox.Show( 从Form3结果调用的函数: + s1 + s2);
}
}

在Form2中:

  public  部分 表单2:表单
{
public Form2()
{
InitializeComponent();
}

// 对Form1中创建的Form3实例的引用
public Form3 PropertyInstanceOfForm3 { set ; get ; }

// 显示Form3的实例
private void button1_Click( object sender,EventArgs e)
{
PropertyInstanceOfForm3.Show();
}
}

在Form3中:

  public  部分  Form3:表格
{
public Form3()
{
InitializeComponent();
}

// 模板(委托)的方法,其中a将插入方法的引用(指针)
// 将插入Form1中的 >
public 操作<字符串,字符串> MethodInForm1ToCall;

// 调用Form1中的方法
私有 void button1_Click( object sender,EventArgs e)
{
MethodInForm1ToCall( 我被触发了 通过单击Form3)实例中的Button;
}
}

在此示例中,Form1和Form3实例在Form1的代码中创建一次。 Form1的代码将Form3的实例的引用注入到Form2中,并且注入,如上面详细解释的,将Form1中的方法指针转换为Form3。



如果你能理解这里发生了什么,你应该能够轻松地为你的代码进行调整。在您的特定情况下,'Action模板(委托)将不带参数,您将需要在Form1上插入与Action匹配的Form3 每次创建它的新版本。



请随意通过对此答案的评论提问。


因为问题非常受欢迎,而且我的之前的答案通常都不太清楚,可能还不够清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



< DD> -SA

Hi...
I have three forms(Form1,Form2,Form3).Form1 Contains a function, serial port. Form2 is opened when '1' received from serial port. Form3 is opened in form2's button click. Now I want to access function in form1 from form3. How it can be done without using
Form1 f=new form1();
f.funcname();

解决方案

First, I suggest you study the references Sergey provided to you in his reply, particularly study his example of the use of an Interface to selectively expose fields, properties, or methods of one object/entity/Form/whatever to another.

Also, OriginalGriff has a series of three articles, published in September 2013, on CodeProject, that cover different ways one Form can communicate with another. Here's a link to the first article: [^]. I strongly suggest you study his excellent articles.

I'll show you another way, in this response, to achieve your goal. Using this technique, you'll insert a pointer to a method in the instance of Form1 into the instance of Form3, so the instance of Form3 can use it directly. Note that in this technique the definition of the method in the instance of Form1 can be declared as 'private.

Here's the definition of the method in the code for the instance of Form1 (the Main Form):

public void MethodCalledFromForm3(string s1, string s2)
{
    MessageBox.Show("function called from Form3 result: " + s1 + s2);
}

To make this usable within the instance(s) of Form3 you create we define a template in Form3's code for a method, using .NET's 'Action object [^]:

public Action<string, string> MethodInForm1ToCall;

We have now created a kind of "socket" in Form3 instances into which can be plugged a reference (pointer) to any Method that takes two string arguments, and returns 'void. Technically, an Action is a form of 'Delegate, which you'll learn about as you study the MSDN link; its counterpart that takes a variable number number of arguments and returns a value of some Type, is 'Func.

So, how do we get the method in Form1 whose "signature" matches-up with our "socket" in Form3 "plugged-in" to the instances of Form3 ? It's really easy:

Form3 InstanceOfForm3 = new Form3();

private void Form1_Load(object sender, System.EventArgs e)
{
    InstanceOfForm3.MethodInForm1ToCall = MethodCalledFromForm3;
}

You'll note that I have deliberately not tried to match what you are doing in your code in what's shown here; the reason for this is I hope to make a more generally useful example.

Now, let's put it all together in a "big-picture-view:"

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private Form2 InstanceOfForm2 = new Form2();
    private Form3 InstanceOfForm3 = new Form3();

    private void Form1_Load(object sender, System.EventArgs e)
    {
        InstanceOfForm2.PropertyInstanceOfForm3 = InstanceOfForm3;

        InstanceOfForm3.MethodInForm1ToCall = MethodCalledFromForm3;
    }

    // show the instance of Form2
    private void button1_Click(object sender, System.EventArgs e)
    {
        InstanceOfForm2.Show();
    }

    // the method to be called in Form3
    private void MethodCalledFromForm3(string s1, string s2)
    {
        MessageBox.Show("function called from Form3 result: " + s1 + s2);
    }
}

In Form2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    // a reference to the instance of Form3 created in Form1
    public Form3 PropertyInstanceOfForm3 { set; get; }

    // show the instance of Form3
    private void button1_Click(object sender, EventArgs e)
    {
        PropertyInstanceOfForm3.Show();
    }
}

In Form3:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    // the template (delegate) for the method into which a reference (pointer) to the method
   // defined in Form1 will be inserted
    public Action<string, string> MethodInForm1ToCall;

   // call the method in Form1
    private void button1_Click(object sender, EventArgs e)
    {
        MethodInForm1ToCall("I was triggered ", "by clicking the Button in the instance of Form3");
    }
}

In this example Form2 and Form3 instances are created once, in Form1's code. Form1's code injects a reference to the instance of Form3 into Form2, and injects, as explained in detail above, the pointer to the method in Form1 into Form3.

If you can understand what's going on here, you should be able to easily adapt this for your code. In your specific case the 'Action template (delegate) will take no arguments, and you will need to insert the Method on Form1 that matches the Action into Form3 each time you create a new version of it.

Please feel free to ask questions by comments on this answer.


As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


这篇关于如何从form3访问form1中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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