How do I make child form values in parent form C #?

11

I have a C # signup. There is a "search street" button that opens a form search child. When you run the search, it displays the result in the datagrid of that form child. I would like the event cell_click of the datagrid to complete the combobox in the parent form.

    
asked by anonymous 30.01.2014 / 12:31

5 answers

12

You can solve your problem by passing the reference from parent to child. I imagine you create an instance of this Form daughter within Form parent, so just pass the reference as follows:

In% of parent%

FormFilha container = new FormFilha(this);

In% child%

private FormPai parent = null;
public FormFilha(FormPai _parent){
  this.parent = _parent;
}

From this it is possible to control the parent Form from within the child Form, including elements that are internal to Form , such as Form , for example, as long as the reference to this element has FormPai .

    
30.01.2014 / 13:15
6

In this case you can pass the parent form as a parameter to the class, and through this parameter (in the case of the parent form object) you can interact with the parent form.

Form1 code:

public Form1()
{
       InitializeComponent();
}
    //botao que ABRE o FORM B
private void button1_Click(object sender, EventArgs e)
{
    Form2 formB = new Form2(this); //this, significa que estou passando ESSA classe (instância dela) como param
    formB.Show();
}

Form2 code:

public partial class Form2 : Form
{
    Form1 instanciaDoForm1; //objeto do tipo FORM 1, que será usado dentro da classe
    //inicializador do FORM
    public Form2(Form1 frm1) //recebo por parametro um objeto do tipo FORM1
    {
        InitializeComponent();
        instanciaDoForm1 = frm1; //atribuo a instancia recebida pelo construtor a minha variavel interna
        //associo o mesmo texto do tbxTextBoxFormA ao meu FORM B
        txtTextBoxFormB.Text = instanciaDoForm1.txtTextBoxFormA.Text.ToString();
    }
    //botao alterar
    private void button1_Click(object sender, EventArgs e)
    {
        //passo para a textbox do FORM A o mesmo texto que está na minha do FORM B
        instanciaDoForm1.txtTextBoxFormA.Text = txtTextBoxFormB.Text.ToString();
        instanciaDoForm1.txtTextBoxFormA.Refresh(); //recarrego ela
    }
}

So you can interact with the grid in the same way you did with the textbox.

Article adapted from my friend Fernando Passaia link

    
30.01.2014 / 12:45
4

It is not a good practice to delegate this responsibility to the child form. In general it would be the responsibility of the parent form (containing the combobox) to modify the contents of this combo. You can create the child form as a data source (his job would be to list the items and say which one was selected)

When displaying the form with a "ShowDialog" you get an enum with the result of that form (OK, Cancel, etc) and can use this flag to know if the user did not close the screen without selecting anything. To get the selected item you can, through properties, expose the contents of the child form. This allows this child form to be used by any parent form.

public class Cidade
{
    public string Nome { get; set; }
}

public partial class FormPai : Form
{
    private void btnObterCidade_Click(object sender, EventArgs e)
    {
        var seletorCidade = new SeletorCidade();
        var resultadoSeletor = seletorCidade.ShowDialog();

        if (resultadoSeletor == DialogResult.OK)
        {
            var cidadeSelecionada = seletorCidade.CidadeSelecionada;
            comboCidade.SelectedItem = cidadeSelecionada;
        }
    }
}

public partial class SeletorCidade : Form
{
    public Cidade CidadeSelecionada { get; set; }

    private void btnOk_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }

    private void btnCancelar_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }
}
    
30.01.2014 / 13:33
4

The easiest way to do this is to create a public variable in FormField and use ShowDialog () to call it with DialogResult, so when you click on the datagrid you call this code:

Declaration in formName

public int codigo { get; set; }

Click command

codigo = valor;
DialogResult = DialogResult.OK;

Calling the formName in FormPai

Form2 formFilho = new Form2();
if (formFilho.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
     comboBox1.SelectedValue = formFilho.codigo;
}
    
30.01.2014 / 13:41
3

A good way to do this in C # is to create an event in the child form. For example:

public event Action<String> EnderecoSelecionado;

When you create the child form on the parent form, you register for this event:

formFilho.EnderecoSelecionado += enderecoSelecionado_event;

When the user clicks on the child form's grid, you check whether there are registered functions in the event, if any you run it:

if( EnderecoSelecionado != null ) {
    EnderecoSelecionado( o_endereco_selecionado );
}

In this way the interface between Father and Son will be very explicit and the same event can be reused in other parts of your application. This is a common idiom of C #, and I rarely get used to it.

    
30.01.2014 / 13:37