Skip to content

How public static declaration for Statusbar Control ?

Dong Lam Trien

Asked Jun 7, 2015, 1:45 AM EDT1 answer
Share
LinkedInX
In C# assuming I have 2 forms: form1 and form2. I open Form1 drag Statusbar Control into Form1 with the following declaration:
[code]
C#
public static void LogStatusBar(string status)
  {
       try
            {
                statusBar1.Text = "Thông báo: " + status; // error message here
            }
            catch (Exception ex)
            {

            }
  }
  private void form1_Load(object sender, EventArgs e)
   {
       ....
       LogStatusBar(Message);
....
}
[/code]
Form1 run before and open form2, form2 will be used together with function LogStatusBar Form1 (...) I have declared above, and in this form2. I declare the following code:
[code]
Code
try
  {
     ....
  }
 catch (Exception ex)
  {
      form1.LogStatusBar(ex.Message);
  }
[/code]
My question posed here as Rebuild Solution in statusBar1.Text error will not be public static because of it, according to my understanding of this, verbatim error: Error 1 An object reference is required for the nonstatic field, method, or property 'Vidu.form1.statusBar1'. I want to declare the control statusBar1 was dragging this into the public static Form1 themselves to declare how the band through household property.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Manoj Bhoir
    0
    Hi,
     
    From your question it looks like you are talking about C# windows application. So first thing i want to tell you here that, you can not directly set text property for status bar or such control. You have to use StatusStrip in your form.
     
    Just follow these steps :
     
    - On your Form1 Add StatusStrip control.
    -In that StatusStrip1 control add ToolStripStatusLabel (Status Label) Control.
    -Now add below property on your Form1 Code :
     
    Code
    public string StatusBarMessage
            {
                get { return toolStripStatusLabel1.Text; }
                set { toolStripStatusLabel1.Text = value; }
            }
     
    -Now Add Form2
    -On Form2 your code will be like this :
    Code
    try
      {
         ....
      }
     catch (Exception ex)
      {
          Form1 objForm1 = new Form1();
                objForm1.StatusBarMessage = "Error Message From From2";
      }
    I hope this will help you.
    0