繁体   English   中英

C#-Form2值到Form1

[英]C# - Form2 value to Form1

我在将在form2(citacao)中输入的值传递给form1(principal)时遇到麻烦。

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;
}

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

在Form 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 /主体中订阅了关闭事件并实现了该事件,则当Form 2从屏幕上消失后,您的数据将自动传输:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM