1

I'm trying to change the text of a label in Form1 (Windows Forms) from an external class. To do that I've done this inside the external class:

public static class ExternalClass
{
    static MyClass myClass = new MyClass();
    public static void myMethod()
    {
        myClass.lblMyLabel.Text = ("My Text");
    }
}

I don't get any errors or such so it looks like it should work but when using the application the text won't change, what's the easiest way to solve this?

After searching around on this page I've found that the code I wrote above should be enough seen to their solutions.

I've tried to call non static a method in the Form1 class through the myClass. operator(?) with no result.

Also I've tried to use a string method with get; set; that been pointed to the label and it's value that's told to work in lots of threads with no result, so now I'm out of ideas.

Preferably there should be a way to work with the code in the example.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Perly X
  • 45
  • 1
  • 7
  • Your question lacks in context. Is MyClass a Form? – Steve Sep 03 '16 at 15:07
  • Is which form is that label that you're referring to in `MyClass` ? How are you setting that label in that static Field ? I think that's the most relevant part to your issue and you didn't show it. Please update your answer and show that part. – Zein Makki Sep 03 '16 at 15:09
  • how do you instantiate and show Form1? – rlee Sep 03 '16 at 15:12
  • Steve: Yes, MyClass is the Form1. – Perly X Sep 03 '16 at 15:14
  • @PerlyX Where are you using `myClass.Show()` ? Since `myClass` is private I think this should be in the same `ExternalClass`. I think you're using `new MyClass()` somewhere else and you think that you're accessing the same instance. Please show that part. – Zein Makki Sep 03 '16 at 15:16
  • @user3185569 I'm not using myClass.Show() since I haven't seen that in the threads where they've used the example code. Am I supposed to use that? I got 4 external classes and new MyClass() is only used once in one of them. – Perly X Sep 03 '16 at 15:24
  • @rlee I guess I haven't done that? The rest of the code in the external classes works fine though when "speaking" with MyClass()/Form1. – Perly X Sep 03 '16 at 15:29
  • @PerlyX Then how to you make your form visible ? You said that `MyClass` is actually your form. When are how are you creating and showing your form ? And how are you making the static `myClass` refer to that form that is visible ? Do you understand the difference between `static` and `instance` members ? – Zein Makki Sep 03 '16 at 15:31
  • Now I added the myClass.Show and the label changes with the myClass, probably not the best way to change the label text though since when reading the other threads I got the image that it would update the Form1/MyClass label text rather than display a new identical form except the label – Perly X Sep 03 '16 at 15:33
  • 1
    Here is your problem: `static MyClass myClass = new MyClass();` You have `MyClass` open on your desktop and the instance you created at this line, is another instance, different than the instance which you see. – Reza Aghaei Sep 04 '16 at 14:13
  • 1
    Read about some useful options here: [How to Change a Control of a Form from Another Form?](https://stackoverflow.com/questions/38768737/how-to-change-a-control-of-a-form-from-another-form) – Reza Aghaei Sep 04 '16 at 14:15
  • 1
    Also if your `ExternalClass` should be static, then you can find the open instance of your `MyClass` this way: `var f = Application.OpenForms.OfType().FirstOrDefault();` and set `f.lblMyLabel.Text="Something"`. Don't forget to set `lblMyLabel` to `Public`. For example see this post: [C# Can't change labels and button properties from a dialogbox](http://stackoverflow.com/a/32816640/3110834). – Reza Aghaei Sep 04 '16 at 14:18
  • 1
    @RezaAghaei Oh, that's so great, thank you so much :) – Perly X Sep 04 '16 at 20:50
  • 1
    Also when you want to say a comment was useful, it's enough to vote for comments to bring it up in comments list. – Reza Aghaei Sep 04 '16 at 20:53
  • By the way, if you found those links useful, it would be great if you vote for answers and questions to make them more popular to be more useful for future readers. It's a new permission for you after gaining 15 reputations. It's not compulsory at all, but it motivates users to post high quality answers, also motivates them to link to useful answers instead of posting duplicate answers. – Reza Aghaei Sep 05 '16 at 21:32

0 Answers0