简体   繁体   中英

C# - Form2 value to Form1

I'm having trouble passing values ​​entered in form2(citacao) to 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;

} 

But when I click the button nothing happens , the values ​​are not inserted into the richedit I like it.

I already have this on form (Pass DataGrid to ComboBox)

Form1 (principal)

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

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

let's see whether I understood your situation :)

declare your variable in Form 1 as a class variable

private citacao cita;

then initialize it in the button press event

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

EDIT:

Ok after looking at the entire code: please remove the button3! and this entire code:

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

The function AppendText probably needs a string as parameter and you give the entire array! If you subscribe to the closing event in Form1 / principal and also implement the event, your data will be transmitted automatically as soon as the Form 2 disappears from the screen :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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