C# - Form1的Form2值

时间:2016-06-14 12:42:20

标签: c# winforms

我无法将在form2(citacao)中输入的值传递给form1(主体)。

Principal.cs(form1)

richEditControl1.Document.AppendText(citacao.valor_edit[0]);

Citacao.cs(form2)

public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
    valor_edit[0] = memoEdit1.Text;
    valor_edit[1] = comboBox1.SelectedItem.ToString();
    valor_edit[2] = textEdit1.Text;

} 

但是当我点击按钮时没有任何反应,值不会插入我喜欢的richedit中。

我在表单上已经有了这个(将DataGrid传递给ComboBox)

Form1(校长)

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    citacao cita = new citacao(this);
    cita.Show();
}

窗口2(citação)

public citacao(principal gridForm)
{
    InitializeComponent();
    frm1 = gridForm;
}

// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
    comboBox1.Items.Clear();
    foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
    {
        comboBox1.Items.Add(row.Cells[0].Value.ToString());
    }
    comboBox1.SelectedIndex = 0;
}

1 个答案:

答案 0 :(得分:4)

让我们看看我是否了解你的情况:)

将表单1中的变量声明为类变量

private citacao cita;

然后在按钮按下事件

中初始化它
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    cita = new citacao(this);
    // subscribe to the closing event
    cita.FormClosing += form_FormClosing;
    cita.Show();
}

// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
    // BUT! you have to use the variable name!
    richEditControl1.Document.AppendText(cita.valor_edit[0]);    
}

修改

看完整个代码后好了: 请删除button3!以及整个代码:

private void button3_Click(object sender, EventArgs e)
{
    cita = new citacao(this);
    richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}

函数AppendText可能需要string作为参数,您可以给出整个数组! 如果您订阅Form1 / principal中的结束事件并执行 一旦表格2从屏幕上消失,您的数据将自动传输:)