我可以在Windows窗体中使用一种窗体中声明的对象吗? [英] can i use the object declared in one form in another form,windows forms
本文介绍了我可以在Windows窗体中使用一种窗体中声明的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用win窗体中描述的连接对象到另一个win窗体中
i want to use the connection objects declred in on win form to another win form
推荐答案
您可以将它们声明为公共或属性
you can declare them as public or as a property
例如
for example
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public object C1
{
get;
private set;
}
}
Form1
-----------
在第一个表单中将Connection对象定义为objConnection.
添加一个按钮,并在按钮单击事件"上编写代码.
SqlConnection objCon =新的SqlConnection(//定义凭据);
Form2 objfomr2 =新Form2(objCon);
objfomr2.Show();
Form2
-------------
定义参数化的构造函数,并获取与下面相同的连接对象的值.
将连接引用对象声明为conref.
SqlConnection con;
公共Form2(SqlConnection con)
{
InitializeComponent();
this.conref = con;
}
此Connection对象conref具有与Form1中相同的值.
Form1
-----------
Define Connection object as objConnection in the First Form.
Add a button and Write the code on the Button Click Event.
SqlConnection objCon=new SqlConnection(//Define credentials);
Form2 objfomr2=new Form2(objCon);
objfomr2.Show();
Form2
-------------
Define the parameterised constructor and get the value of the same connection object as below.
Declare a connection reference object as conref.
SqlConnection con;
public Form2(SqlConnection con)
{
InitializeComponent();
this.conref= con;
}
This Connection object conref has the same value as in Form1.
这篇关于我可以在Windows窗体中使用一种窗体中声明的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文