Solved

C# updating Status Bar in Main Window from User Control

Posted on 2011-03-03
9
1,637 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:ferocious
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:jkofte
ID: 35026480
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35028271
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
 

Author Comment

by:ferocious
ID: 35030181
@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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35030195
I successfully tested that example so you must have implemented it differently...

Can you show me the UserControl code?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ferocious
ID: 35030325
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35030464
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
 

Author Comment

by:ferocious
ID: 35030682
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35030838
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
 

Author Comment

by:ferocious
ID: 35030964
Thanks a lot
i have learnt a lot from this.

cheers
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now