使用ref in with 2形式..?

时间:2012-07-19 12:05:29

标签: c# .net winforms

我有两种形式..

从第一种形式我称之为第二种形式...以第二种形式我做了一些计算,我希望在关闭第二种形式后得到第一种形式的结果。

第一表格代码

public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{
    String s = "";
    public XtraForm1()
    {
        InitializeComponent();
    }

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        s = textEdit1.Text;
        XtraForm2 x = new XtraForm2(ref s);

        x.ShowDialog();
        MessageBox.Show(s); // Here I want to get the data from 2nd form.
    }
}

第二表格代码

public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm
{
    string s2 = "";
    public XtraForm2(ref string s1)
    {
        InitializeComponent();
        s2 = "hai";
        s1 = s2;
    }

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        // here i do some operations and i want to store it in variable s1 so that i will get the result in 1st form.
        this.Close();
    }
}

5 个答案:

答案 0 :(得分:0)

你可以做很多事情。

但最简单的方法是在您的第二个表单上创建一个属性,将结果放在那里并在ShowDialog() MessageBox.Show(x.MyProperty);之后从您的第一个表单中调用它{/ 1}

答案 1 :(得分:0)

你可以使用属性,对吗?为什么要传递参数byRef?

Button_click(){
    using(var form2 = new Form2())
    {
        form2.Property = initialValue;
        form2.ShowDialog();
        MessageBox.Show(form2.Property);
    }
}

而不是使用get set属性,you can pass the value using constructorget the value from property

答案 2 :(得分:0)

以第二种形式创建公共字段/属性。

//public property
public string Data { get;set;}

private void simpleButton1_Click(object sender, EventArgs e)
{
   Data="Something you want to return back to 1st Form";
   this.Close();
}

在第1表格中,

private void simpleButton1_Click(object sender, EventArgs e)
{
  s = textEdit1.Text;
  XtraForm2 x = new XtraForm2(ref s);

  x.ShowDialog();
  MessageBox.Show(x.Data); 
}

答案 3 :(得分:0)

另一种可能性是使用静态变量:

public static string _form2Data = null;

private void simpleButton1_Click(object sender, EventArgs e)   
{   
    _form2Data = "something";     
    this.Close();   
}

答案 4 :(得分:0)

您可以创建类似于模型的类并声明所有变量 并使用set来编辑数据并获得它。

Public class cs1
{
   private int m_id=0;

public int ID
{
    get
    {
        return m_id;
    }
    set
    {
        m_id = value;
    }
}

}