Have control of a StatusStrip that belongs to a Form1 from Form2 C #

1

I have a% main% co that contains a status bar Form with several StatusStrip . I must have control of StatusLabel from another form to change the StatusStrip message depending on what I'm doing.

For example, if I put the focus in a textbox of StatusLabel , the message should change in a Form2 .

    
asked by avargasma 31.03.2016 в 16:07
source

1 answer

1

You do not need to have access to the control of the other form if you can communicate these uncoupled using interface

Communicate Forms

In the article I explain how you could do it, but basically define an interface

interface IFormState{
  void SetState(string msg);
}

then implemetas is in the main form

public class FormPrincipal : Form, IFormState{

   //aqui implementas la interface

   public button1_Click(..){
         FormHijo frm = new FormHijo(this);
         frm.Show();
   }

}

From the form child you could invoke this method defined in the interface if you have the main form instance

public class FormHijo : Form{

    private IFormState _frm;

    public FormHijo(IFormState frm){ 
       _frm = frm;
    }

     public button1_Click(..){
        _frm.SetState("mensaje");
     }
}

By passing it on to the architect, you could have the instance of the main form that implements the interface.

This way you can send a data to the main form without having to access the control of the form.

    
answered by 31.03.2016 в 16:17