0

TextBox にいくつかのデータを入力し、form2 のコンボボックスで項目を選択しましたが、form1 で同じデータを使用したいのですが、どうすればよいでしょ
うか....このコード frmConfigは form2 でtxtSrcIP、TextBox です。

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; います。フィールド初期化子は非静的フィールドメソッドまたはプロパティを参照できません

frmConfig 本体 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 にすべてのテキストボックスとコンボックスの値が必要です

4

4 に答える 4

2

必要なコントロールf2のプロパティを公開するパブリック プロパティを作成します。Text

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
}
于 2012-12-05T11:14:49.593 に答える
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");
    }
}
于 2012-12-05T11:15:21.240 に答える
0

使用するコントロールは、「modifier」プロパティを変更して、外部からアクセスできるようにします。

于 2012-12-05T11:16:25.193 に答える
0

MSDNを引用すると、インスタンス フィールドの変数イニシャライザは作成中のインスタンスを参照できないため、代わりにコンストラクタでフィールドを初期化する必要があります。

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

    public Form1(frmConfig Cont)
    {
        f2 = Cont;
        String SIp = f2.txtSrcIP.text;
    }
}
于 2012-12-05T11:33:01.480 に答える