我已经在窗体2中的文本框和组合框中输入了一些数据,并希望在窗体1中使用相同的数据我该怎么做......
我尝试这个代码 frmConfig
是窗口2和txtSrcIP
是文本框从c中的一个窗体的文本框中获取文本
public partial class Form1 : Form
{
frmConfig f2 = new frmConfig();
public Form1(frmConfig Cont)
{
f2 = Cont;
}
String SIp = f2.txtSrcIP.text;
}
的误差在这条线表示String SIp = f2.txtSrcIP.text;
为A字段初始不能引用非静态字段方法或属性
frmConfig博dy public partial class frmConfig:Form { private Form1 f1;
public frmConfig()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
// Open connection to the database
string conString = "server="+txtSrcIP.Text+";uid="+txtSrcUserId.Text+";pwd="+txtSrcPwd.Text;
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
// Set up a command with the given query and associate
// this with the current connection.
using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con))
{
using (IDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
cbSrc.Items.Add(dr[0].ToString());
}
}
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (cbSrc.SelectedItem != null && cbSrc.SelectedItem != "" && cbDest.SelectedItem != null && cbDest.SelectedItem != "")
{
this.Hide();
//Form1 f1 = new Form1();
f1.Show();
this.Close();
}
else
{
MessageBox.Show("Enter all the details");
}
}
}
这是我在做什么,所以我想在Form1
您需要在某些状态发生变化时引发事件。结帐观察员模式:http://zh.wikipedia.org/wiki/Observer_pattern –
String SDb = f2.cbSrc.SelectedItem.ToString(); 错误=未将对象引用设置为对象的实例 –