0

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!

collent
  • 13
  • 2

1 Answers1

0

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?

Welcome to StackOverFlow!

AcisSys
  • 51
  • 1
  • 8
  • 1
    You know I think I was thinking in terms of unity(where all my experience comes from) where you had to initialize the object before altering it, and after testing it out you are correct! Thanks a bunch – collent Jul 06 '22 at 17:58
  • 'Form1 f1 = new Form1();' Would be the Initialization no? You need to change before showing the Form, because once you "enter" the form, the 'f1.changeText("Hello World");' would only be run after said form was closed – AcisSys Jul 07 '22 at 08:02