Search code examples
c#winformsstatusbar

what is the best way populate the status bar of non- parent form


How do i to access or update the controls of non-parent or non-mdi form from another form in C#.

IF yes, the I have a form - Form1 and button on it. When i click button, new form - Form**2 opens which is not child form.What i want to try is when i do some activity on **Form2, some information should be displayed on status-bar of Form1.

If sample available it will be good.

Thanks,


Solution

  • You can use a mediator pattern basically you have to create both form sharing the same mediator object.

    Update sample code

    public class SimpleMediator{
    
        public Form1 MainForm {get; set;}
    
        public void DisplayStatus(string message){
            MainForm.StatusBar.Text = message;
        }
    }
    
    public class Form2 : Form{
    
        public SimpleMediator Mediator {get; set;}
    
        //...
    }
    

    then when you open Form2

        public void OpenSubForm(){
            var mediator = new Mediator{
                MainForm = this;
            };
            var f2 = new Form2(){
                Mediator = mediator;
            };
            f2.Show();
        }
    

    Now you can access Mediator.DisplayStatus() method inside Form2