0

It's my first time using winforms. I'm having some issues.

   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "FORM1";
            textBox1.AppendText("SOME TEXT");
        }
    }

I've tried this. My textbox is set to fill so it fills up the whole form. I set the multi-line propery to true and dock property to fill.

From what I can try as a first time WinForms user, the Form1_Load should run as soon as the form is created. I've tried some various ways to print text, nothing works. I noticed visual studios says "0 references" next to my function. I'm not sure what this means, maybe part of my issue? Please help.

enter image description here

enter image description here

4
  • What's the question? In my case text box fills up correctly when using your code. What are you trying to achieve with text changed event ? Commented Feb 6, 2018 at 6:08
  • @MichałTurczyn Nothing is happening. No text it ever being output to my textbox! I don't understand. If it's working for you I might try recreating project. Not sure what that'll do for me though.
    – Birdman
    Commented Feb 6, 2018 at 6:10
  • It looks like your form load event is not registered. Open designer, select the form and go to properties window (Ctrl+W, P). Here choose events, and look for Load event. Copy Form1_Load1 and paste at the box next to event name
    – saidfagan
    Commented Feb 6, 2018 at 6:16
  • It sounds like you just typed out that event handler, but did not hook subscribe it to the Form1.Load event. In the Form1() constructor, try adding the line: this.Load += Form1_Load;
    – Rufus L
    Commented Feb 6, 2018 at 7:55

2 Answers 2

3

Having 0 references to method means that it's never used in code. So your issue is that you defined method, which looks like should be executed on form load event, but it's just the definition. I suggest that you copy body of your method to clipboard, go to design page of your form, on the right side, you can browse events, that form generates, find Load event, double-click it, Visual Studio should generate code with empty method definition, paste there your code.

When you right-click your method name and click Find all references you should get something like this at the bottom:

enter image description here

2
  • THANK YOU. I don't understand though, I typed the EXACT same thing myself in the same file, I commented out my function before hand to compare. Then I used your method. They are literally identical. Yet when I did it myself I get 0 references, but when I do it your way I get the reference immediately. I actually just spent an hour trying to figure this out, looking at references & seeing my code looked fine. Is there anyway you could explain why this happened? I was under the impression you could code anything manually in the winforms files, OR use your method.
    – Birdman
    Commented Feb 6, 2018 at 6:45
  • Visual Studio automatically generates all code necessary for you. When you typed manually your method you most probably forgot to add it to your eventhandler: this.Load += new System.EventHandler... on picture I attached. Commented Feb 6, 2018 at 6:47
1

Have you checked following things are done in properties:

Go to Form1->Properties window and check for events. Now check for Load event and it should be attached to your Form1_Load method.

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.