update status strip from child form to parent form MDI

0

Dear friends.

I have a MDI parent form with several menu options, in which I have a Status Strip control, and I give as an example when opening a form of my patient file and when making a query I want to show in my status bar of the form MDI "Executing query ..." from my client form Customers.cs

my code in form MDI when selecting the Customers option

    private void mnuConsultasClientes_Click(object sender, EventArgs e)
    {
        frmClientes frm = this.MdiChildren.OfType<frmClientes>().Where(x => x.Name == "frmClientes").FirstOrDefault();
        if (frm == null)
        {
            sbEstado.Text = "Abriendo formulario Clientes..";
            frmClientes frmClientes = new frmClientes();
            frmClientes.MdiParent = this;
            frmClientes.WindowState = FormWindowState.Maximized;
            frmClientes.Show();
            sbEstado.Text = "Listo";
        }
        else
        {
            frm.WindowState = FormWindowState.Maximized;
        }

When making a client query for examples all those that start with VAS

My code in Consult button.

    private void tsBuscar_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        errorProvider1.Clear();
        if (txtBusqueda.Text.ToString().Trim()==String.Empty)
        {
            errorProvider1.SetError(txtBusqueda, "Ingrese un criterio de busqueda...");
            return;
        }

        //MessageBox.Show(cboBuscarPor.SelectedIndex.ToString());

        switch (cboBuscarPor.SelectedIndex)
        {
            case 0:
                sb.Append(" AND CLI.CLI_NOMBRE LIKE '" + txtBusqueda.Text.ToString().Trim() + "%'");
                break;
            case 1:
                sb.Append(" AND CLI.CLI_RUC LIKE '" + txtBusqueda.Text.ToString().Trim() + "%'");
                break;
        }

        //dgvLista.AutoGenerateColumns = false;
        dgvLista.DataSource = nCliente.GetListClientes(sb.ToString());

    }

Thanks for your kind help.

    
asked by Guivan 31.08.2017 в 06:37
source

2 answers

0

One way you can do this is to expose a static variable with the instance of the form, in the main form. This variable will be used to assign the text to sbEstado :

public class MainForm : Form
{
   public static String Status {
     set { 
           instance.sbEstado.Text = value;
     }
   }

   private static MainForm instance;
   public MainForm()
   {
       //..
       instance = this;
   }
  //...
}

Then to use it in another form you only have to assign the value to the static variable of the form:

private void tsBuscar_Click(object sender, EventArgs e)
{
       MainForm.Status = "Buscando...";

       //...
       //...
       dgvLista.DataSource = nCliente.GetListClientes(sb.ToString());
       MainForm.Status = "Listo";

}

One of the limitations of this solution is that you can not assign the status from another thread because you can only modify a control from the thread of the view.

    
answered by 31.08.2017 / 14:45
source
0

Four things to keep in mind:

1.- Make sure the main form has the active IsMdiContainer property.

2.- From your child form, you can reference the parent using MdiParent.

3.- Make sure that when you instantiate the child you assign the MdiParent (You are already doing it)

4.- Make sure that the scope of the control you wish to change is not protected and allows you to make the change from the child. (Modifiers property of the lblMessage control must be Public)

Now if:

frmPadre f = (frmPadre)this.MdiParent;
f.lblMensaje.Texto = "Listo!.";

I hope you find it useful,

    
answered by 31.08.2017 в 13:46