2012-12-05 26 views
0

我已经在窗体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

+0

您需要在某些状态发生变化时引发事件。结帐观察员模式:http://zh.wikipedia.org/wiki/Observer_pattern –

+0

String SDb = f2.cbSrc.SelectedItem.ToString(); 错误=未将对象引用设置为对象的实例 –

回答

0
static frmConfig f2 = new frmConfig(); 

那么,如何对这些变化:

public partial class Form1 : Form 
{  
    static frmConfig f2 = new frmConfig(); 

    public Form1(frmConfig Cont) 
    { 
     f2 = Cont; 
    } 

    public String SIp; 
} 

... 

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.SIp = f2.txtSrcIP.text; 
     f1.Show(); 

     this.Close(); 
    } 
    else 
    { 
     MessageBox.Show("Enter all the details"); 
    } 
} 
+0

它帮助文本框,但组合框的值不回收 –

+0

错误=未将对象引用设置为对象的实例 –

+0

是否可以包含'frmConfig'类定义?或者至少,ComboBox定义? –

2

的所有文本框和COMBOX值创建一个公开,你需要控制的Text财产上f2公共财产。

public string TxtSrcIPValue 
{ 
    get 
    { 
     return this.txtSrcIP.text 
    } 
} 

然后使用此属性来访问该值。

Private string SIp; 
public Form1(frmConfig Cont) 
{ 
     f2 = Cont; 
     SIp = f2.TxtSrcIPValue; // Set the value once the form has been loaded 
} 
0

要使用,变“修改”属性,以便您可以从外部存取权限它的控制。

0

引用MSDN,实例字段的变量初始值无法引用正在创建的实例,因此您需要在构造函数中初始化字段。

public partial class Form1 : Form 
{  
    frmConfig f2; 
    String SIp; 

    public Form1(frmConfig Cont) 
    { 
     f2 = Cont; 
     String SIp = f2.txtSrcIP.text; 
    } 
} 
相关问题