C# updating Status Bar in Main Window from User Control

Hey,

I am having problem passing the changed value from a UserControl.cs file to the mainForm's(MainForm.cs) status bar.
eg: UserControl.cs -> user clicks on a button and it says "ON" and this should update the status bar with "ON" in the MainForm.cs.

anybody plz suggest me how can i do this ?
i have googled it and found quite a lot of tutorials but i'm confused .
plz help.Thanks
ferociousAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...that error will occur if you have NO SUBSCRIBERS to your event.

You can either wire it up programatically:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            usrControl1.Status +=new usrControl.StatusDelegate(usrControl1_Status);
        }

        private void usrControl1_Status(string message)
        {
            toolStripStatusUpdate.Text = message;
        }

    }

Open in new window


Or you can do it via the IDE:
(1) Select the UserControl on the Form.
(2) In the Properties Pane, click on the "Lightning Bolt" Icon to get the Events listing.
(3) Find the Status entry and change the drop down on the right to "usrControl_Status".
0
 
jkofteCommented:
hello there,

I think this code should help you:

X.STATUSBAR sb = this.Page.FindControl("STATUSBARNAME");
sb.PROPERTYTOUPDATE = "UPDATEVALUE";

basically you should use this.Page.Findcontrol()
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Make your UserControl raise a Custom Event that the Main Form subscribes to.  When the Event is received in the Main Form it updates the StatusBar itself.

A simple UserControl:
public partial class UserControl1 : UserControl
    {
        public event StatusDelegate Status;
        public delegate void StatusDelegate(string status);

        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Status("On");
        }
    }

Open in new window


MainForm has subscribed to the Status() event:
public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void userControl11_Status(string status)
        {
            this.toolStripStatusLabel1.Text = status;
        }

    }

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
ferociousAuthor Commented:
@jkofte:
I am sorry im bit confused there.


@Idle_Mind

i did exactly how u have it
it gives me this error message.
"Object Reference not set to an instance of a object"
complaining about this <this.Status("On")>


0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I successfully tested that example so you must have implemented it differently...

Can you show me the UserControl code?
0
 
ferociousAuthor Commented:
hmm yeh maybe i have done something wrong . this is wat i got

public partial class usrControl : UserControl
{

public event StatusDelegate Status;
public delegate void StatusDelegate(string status);
public usrControl ()
        {
            InitializeComponent();
        }

 private void btnStart_Click(object sender, EventArgs e)
        {
            this.Status("ON");
}
private void btnStop_Click(object sender, EventArgs e)
        {
            this.Status("OFF");
       }
}

and in my MainForm i have

 public partial class MainForm : Form
{
public MainForm ()
        {
            InitializeComponent();
       }
private void usrControl _Status(string message)
        {
            toolStripStatusUpdate.Text = message;
        }

}
0
 
ferociousAuthor Commented:
idle_mind

Its working now and i think was making a mistake with calling another class

I want to ask you like.is there a way to make a class and do all status update there
and just call it inside the MainForm because im afraid my design is not very OOP atm.
anyway
thanks for your help
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The design I presented is "loosely coupled".  The UserControl has no idea who has subscribed to it and doesn't care.  It simply raises the event and those who have subscribed get notified.  You can re-use that same UserControl in many forms without any changes.  The container/form decides what to do with the data when the event is received.

On the flip side, a "tightly coupled" solution receives a reference to something and stores it internally so that it can manipulate it directly.  In this case you'd pass a reference to the Form into the UserControl.  Note that for this approach to work the Form would need a PUBLIC option for updating the StatusBar.  This can be accomplished by either making a public wrapper for it, or by changing the Modifiers() property of the control in question to Public so that it can be accessed from outside the class.  A tightly coupled solution is not as flexible since you are passing in a specific type and expecting a particularly named control to be present.  If you originally designed it with Form1 in mind, you'd have to manually make changes to get it to work with Form2.

One approach isn't necessarily better than the other...they just have different pros/cons.

Here's a simplified example of the "tightly coupled" solution:
public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            userControl11.MainForm = this; // <-- setting the reference in the usercontrol
        }

    }

Open in new window


 
public partial class UserControl1 : UserControl
    {

        public Form1 MainForm = null;

        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MainForm != null)
            {
                MainForm.toolStripStatusLabel1.Text = "ON";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MainForm != null)
            {
                MainForm.toolStripStatusLabel1.Text = "OFF";
            }
        }

    }

Open in new window

0
 
ferociousAuthor Commented:
Thanks a lot
i have learnt a lot from this.

cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.