Ben
Ben

Reputation: 669

Modifying a form object created by application.run(form1)

I'm new to C# and trying to figure out how to edit a form from another class. The form is created by the default VS approach, as so:

static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

I've created a method in the Form1.cs file, just as a test for how to update a label1 field that is in Form1. Here is the method:

public void UpdateLabel(string state = "Changed Text")
{
  label1.Text = state;
}

The problem I'm having is that the Application.Run command doesn't provide for a named object of type Form1. So when I want to trigger the UpdateLabel method from Program.cs, like so:

XXX.UpdateLabel();

I don't have an object to target to access the form. If I were creating the form manually, then I believe this would work fine:

Form1 myForm = new Form1();
myForm.UpdateLabel();

With the Application.Run(Form1) that the Windows Form Application provides, how do I access the form object that is being created? Also, is this the approach I should be taking for this type of problem, or is there a better method?

Upvotes: 1

Views: 1585

Answers (4)

Steve
Steve

Reputation: 216273

Well, you could integrate your last example in this way....

static void Main() 
{ 
  Application.EnableVisualStyles(); 
  Application.SetCompatibleTextRenderingDefault(false); 
  Form1 myForm = new Form1();
  myForm.UpdateLabel();
  Application.Run(myForm); 
} 

but let me ask you: Why your logic dictates to do this outside the Form1 constructor?

public Form1()
{
    InitializeComponents();
    label1.Text = "Changed Text";
}

EDIT: Following the comments below I think you should work on something like this:

MyApplicationCode appCode;

public Form1()
{
    InitializeComponents();
    appCode = new MyApplicationCode();

    this.Text = appCode.GetFormText();
    label1.Text = appCode.GetLabelText();
    cmdSave.Enabled = appCode.UserHasSavePermission();
    ...... // and so on for other decisions on 

}

Upvotes: 2

Servy
Servy

Reputation: 203811

So you have some fairly complicated logic that is used to determine the initial value of some controls in your form. Due to the complexity of that logic, you would prefer to extract that code from the definition of Form1 and move it to another class. That is all good so far.

You can create some other class, have Form1 use that class, and have it provide a value to Form1. Rather than having some other class that has-a Form1 (which would be the effect of putting your code in Main, Form1 should have that other class.

Implementing this is fairly simple. You create another class, you give it an instance or static method that returns a string. Form1 either calls the static method, or creates an instance of the class and calls the instance method. It then sets a label based on the results of that method.

Upvotes: 1

Justin Harvey
Justin Harvey

Reputation: 14672

You could fix the immediate issue with:

Form1 form = new Form1());

form.UpdateLabel();

Application.Run(form);

As for the second question, it really depends on what you are trying to ultimately achieve.

Upvotes: 2

Andrew
Andrew

Reputation: 835

Application.Run is typically used to launch a WinForms application until such a point that that application closes. What function of your Program.cs class is supposed to call UpdateLabel? Is your application launched externally with label value parameters?

Upvotes: 0

Related Questions