?
Solved

Static Method containing a Main form control

Posted on 2009-12-22
3
Medium Priority
?
446 Views
Last Modified: 2013-12-17
I have a public static method that I am calling from a different class to make an adjustment to my main window gui.  In order for the other class to be able to use it I made it static.  But then I got an error saying that the form control that I am updating needs to be static.  How can I get around this to be able to update an existing control on my main gui?

code that is part of my window1 class that refers to my status bar in my WPF front end.
static public void status_bar_msg(string msg)
        {
            statusBar1.Items.Clear();
            TextBlock statusLabel = new TextBlock();
            statusLabel.Text = msg;

            statusBar1.Items.Add(statusLabel);
        }

code that is in a class outside of my Window1 class that attempts to make a change to the front end.

 Window1.status_bar_msg("Excel Export took " + Math.Round(diff.TotalSeconds, 2).ToString() + " secs.");


0
Comment
Question by:perrytaylor
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26107871
I don't work with WPF but the general "proper" solution is to either:

(1) Make the secondary class raise a custom EVENT with the value that the main form receives and processes.
(2) Pass a REFERENCE to the main form into the secondary class so that you can manipulate it directly.  The Modifiers() property of the control would need to be changed to public/friend so it can be seen outside the class.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 26107882
Don't make the method static, but you'll need to pass a reference to Form1 to Form2.


static class Program
{
  static void Main(void)
  {
    Application.Run(new Form1());
  }
}

public class Form1 : Form
{
  public void status_bar_msg(string msg)
  {
    statusBar1.Items.Clear();
    TextBlock statusLabel = new TextBlock();
    statusLabel.Text = msg;

    statusBar1.Items.Add(statusLabel);
  }    
  
  protected void SomeButton_Click(object sender, EventArgs e)
  {
    new Form2(this).Show();
  }
}

public class Form2 : Form
{
  private Form1 _form1;
  public Form2(Form1 theOpener)
  {
    _form1 = theOpener;
  }
  protected void SomeOtherButton_Click(object sender, EventArgs e)
  {
    _form1.status_bar_msg("Hello World");
  }
}

Open in new window

0
 

Author Closing Comment

by:perrytaylor
ID: 31669135
Thanks for the quick and easy to understand answer
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

839 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