如何将表单中的组件用于c#中的其他表单 [英] how to use a component from a form into other forms in c#
本文介绍了如何将表单中的组件用于c#中的其他表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下来投票最爱
我的Form1中有一个名为x的Xmpp客户端连接。我希望在其他表单中使用它该项目。
i有7个Forms.But我已经在Form1中定义了连接。
我如何在其他表格中使用...
以下是我在Form1中定义的连接
< pre lang =cs> public Form1()
{
InitializeComponent();
}
XmppClientConnection x;
当我单击Form1上的按钮时,它打开Form2.I也定义为在Form2中使用x使用:
public static string sendtext = 跨度>;
private void button3_Click( object sender,EventArgs e)
{
sendtext = textBox1.Text;
Form2 form = new Form2(x);
form.Show();
}
解决方案
当你调用第二个表单时你的对象可能为null,你可以创建新的对象if它是null。
if (x == null )
{
x = new XmppClientConnection();
}
Form2 form = new Form2(x);
form.Show();
down vote favorite
I have a Xmpp client connection named x in my Form1.I want to use that in other Form of the project.
i have 7 Forms.But i have defined the connection in Form1 only.
how can i use that in other Form..
Following is connection i defined in Form1
public Form1()
{
InitializeComponent();
}
XmppClientConnection x;
When i click a button on Form1 it opens Form2.I also defined to use x in Form2 using:
public static string sendtext = "";
private void button3_Click(object sender, EventArgs e)
{
sendtext = textBox1.Text;
Form2 form = new Form2(x);
form.Show();
}
解决方案
when you call second form your object may be null, you can create new object if it is null.
if(x==null) { x= new XmppClientConnection(); } Form2 form = new Form2(x); form.Show();
这篇关于如何将表单中的组件用于c#中的其他表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文