-2

我需要帮助将 Form2.textbox1 中的输入传递给 Form1.sti Form1 = 主工作窗口 Form2 = 弹出窗口,以输入路径。

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    public string sti { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = sti;
        this.Close();
    }
}

public partial class Form1 : Form
{
    int CountR = 1;

    public Form1()
    {
        InitializeComponent();
    }

    public string sti { get; set; }

    public void Form1_Load(object sender, EventArgs e)
    {
        Form2 popup = new Form2();
        popup.ShowDialog();
        popup.Dispose();
    }
   public void button1_Click(object sender, EventArgs e)
    {                
    Label7.Text = sti; 
     }

但它总是返回 Null。

我以这种方式构建它是因为我不希望用户只与 IT 管理员混淆路径。

先感谢您

4

4 回答 4

0

你为什么不创建另一个Form2用于从其中捕获值的属性Form1

在 中Form2,添加:(就像在 form1 中一样

public string sti { get; set; }

所以在你的Form1代码中,它看起来像这样

public void Form1_Load(object sender, EventArgs e)
{
    Form2 popup = new Form2();
    popup.sti = sti;
}

并且在Form2

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = sti;
    this.Close();
}
于 2012-08-18T20:44:58.720 回答
0

如果你想在表单之间传递数据,有一个很好的教程: http: //www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms

于 2012-08-18T20:47:22.650 回答
0

您正在创建一个新的Form1,它没有对Form2您创建的 的引用。

您更改了示例,创建了 2 个不同的 sti 属性,您需要在关闭 Form2 之前将Form2sti 值分配给属性。Form1

这段代码应该适合你。

表格1

public partial class Form1 : Form
{
    public string sti { get; set; } 

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Form2 popup = new Form2();
        popup.ShowDialog();
        sti  = popup.sti;
        popup.Close();
        popup.Dispose(); 

    }

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = sti;
    }

}

表格2

public partial class Form2 : Form
{
    public string sti { get; set; } 

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        sti = textBox1.Text;
        this.Hide();
    }
}

您还可以创建一个可以从两种形式引用的静态类,这样您只处理一个 sti 属性。像这样的东西:

表格1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        myProperties.sti = "Hello";
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Form2 popup = new Form2();
        popup.ShowDialog();
        popup.Dispose(); 

    }

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = myProperties.sti;
    }
}

public static class myProperties
{
    public static string sti { get; set; } 
}

表格2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        textBox1.Text = myProperties.sti;
    }

    private void button1_Click(object sender, EventArgs e)
    {

        myProperties.sti  = textBox1.Text;
        this.Close();
    }
}
于 2012-08-18T21:16:27.183 回答
0

尝试 :

public partial class Form2 : Form
{
    public String sti { get; set; }

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form3 frm = new Form3();
        frm.ShowDialog();
        sti = frm.sti;

        textBox1.Text = sti;
    }
}

其他形式:

public partial class Form3 : Form
{

    public String sti { get; set; }
    public Form3()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        sti = textBox1.Text;
        this.Close();
    }
}
于 2012-08-18T21:18:36.090 回答