Search code examples
c#winformsclassprogram-entry-point

Issue with referencing Winform form


I have bean having a issue referencing a winform form I initialize in main, the code for the referencing is as follows,

Form1 f1 = new Form1();
Application.Run(f1);
f1.changeText("Hello World");

the form itself is defined in another file. f1.changetext is a simple function which takes a string input and sets a text block element to the string, I call the function in the form class as the result of clicking a button and that works fine but when I call it in main I get no result. As a side note I have a console.writeline function in main as well that doesn't do anything but I don't know if that's related. For context I'm using Visual studio and if it wasn't obvious I don't have too much experience with coding with classes and frameworks like this. Thanks for any assistance!


Solution

  • I'm not familiar with Application.Run(f1); also shouldnt you change the text prior to "Running it?"

    It Being:

    Form1 f1 = new Form1();
    f1.changeText("Hello World");
    Application.Run(f1);
    

    A way I'm sure will work (the one I use) with showing / initializing another form would be

    Form1 f1 = new Form1();
    f1.changeText("Hello World");
    f1.ShowDialog();
    

    Have you tried Debug to check if the code works / was read when running?