如何选择2形式的形式参数,在C# [英] How to select 2 Form as parameters in a form in c#
问题描述
我有一个法
在类
称取2 参数
是这样的:
I have a method
in a class
that take 2 parameters
like this:
public static void ShowSelectedFeeds(Form1 frm, Form2 frm2)
{
//Some code here.
}
但我不知道怎么记得它的形式,
but I don't know how to recall it in a form,
我记得方法
丝毫1 参数
这样的:
例如 Selectoin_method
是DbCon 类
,并采取1参数 Form1中FRM
I recall methods
whit 1 parameters
like this:
for example the Selectoin_method
is in DbCon Class
and take 1 parameter Form1 frm
在 Form1_Load的
:
DbCon.Selection_method(this)
但是,当它到了一个法
称取2 参数
,我想回顾一下它在 Fome2_Load
我用这个代码,但它不工作(没有显示出异常或错误,什么都没有发生)
but when it's come to a method
that take 2 parameters
and I want to recall it in a Fome2_Load
I use this code but it doesn't work(Shows NO exception or error, nothing happen at all)
private void AddFeedsbtn_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
DBConnection.ShowSelectedFeeds(frm, this);
}
我应该怎么办?
what should I do?
推荐答案
最简单的方法是创建在窗体2
Form1中变量>并将其设置在窗体2
的构造函数。这样的:
The easiest way is to create aForm1
variable inside Form2
and set it in the constructor of Form2
. Like:
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form)
{
InitializeComponent();
form1 = form;
}
}
所以,当你创建一个窗体2
你应该通过在构造一个 Form1中
实例。
So when you create a Form2
you should pass a Form1
instance in the constructor.
private void AddFeedbtn_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);// <--- Form1 instance
frm2.StartPosition = FormStartPosition.CenterScreen;
frm2.ShowDialog();
}
然后就可以通过 Form1中
中的方法变量。
private void AddFeedsbtn_Click(object sender, EventArgs e)
{
DBConnection.ShowSelectedFeeds(form1, this);
}
这篇关于如何选择2形式的形式参数,在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!