0

I have a user control where the Load event is firing too early. The user control has a couple of objects passed into the constructor, and has a load event.

public int num;
public String value;

public myControl(int num, String value)
{
    InitializeComponent();
    this.num = num;
    this.value = value;
}

private void MyControl_Load(object sender, EventArgs e)
{
    sometextbox.Text = value;
    someothertextbox.Text = num.ToString();
}

My issue is that whenever the control is called, the initialize calls the Load event too early. Towards the end of InitializeComponent() in the Designer.cs, it adds the Load event to the control using this code

this.Load += new System.EventHandler(this.MyControl_Load);

and right after it adds the event handler, it goes to the event. So if I had something like this in the Designer...

Some code 1
Some code 2
this.Load += new System.EventHandler(this.MyControl_Load);
Some code 4
Some code 5

it will actually jump OUT of the InitializeComponent() and go run MyControl_Load, before coming back and finishing out with somecode4 and somecode5.

This wouldn't be an issue normally since the Load is just setting some text boxes, but they're using values that are passed in through the constructor, and since the values are set AFTER initializeComponent(), everything just ends up being NULL.

The short answer is to put this.num = num and this.value = value ABOVE the call to initializeComponent(), but this exact same method works on multiple other user controls, and I'm just curious as to why this method doesn't work on this user control.

Chris Hobbs
  • 745
  • 2
  • 9
  • 27
  • What you're describing sounds impossible. Registering an event handler doesn't result in that event handler immediately being called. Have you put a breakpoint in the debugger at the line `this.Load += new System.EventHandler(this.MyControl_Load);` and traced through it? – adv12 May 20 '15 at 16:19
  • Are these textboxes children of your custom control? – Powerlord May 20 '15 at 16:52
  • There is no hint in the question, you'll have to post repro code. Or just not bother because it is wrong to use the Load event in this case, move the code into the constructor. – Hans Passant May 20 '15 at 16:52
  • Sorry for the delay, but yes I've debugged all the way through it - anytime that event is registered it gets called immediately. It's freaking strange. – Chris Hobbs May 20 '15 at 16:55

1 Answers1

2

I agree with adv12, this probably isn't what's actually happening; all this.Load += new System.EventHandler(this.MyControl_Load); does is register the event, not actually fire it.

Having said that, remember that the designer is just a tool that generates code for you. You can do anything that the designer does (and so much more!) yourself if you want more control.

So, if you're having a problem with it, instead of adding the Load event in the designer, change your code like this:

public int num;
public String value;

public myControl(int num, String value)
{
    InitializeComponent();
    this.num = num;
    this.value = value;
    InitializeTextboxes();
}

private void InitializeTextboxes()
{
    sometextbox.Text = value;
    someothertextbox.Text = num.ToString();
}

You may want to read Hans Passant's answer here as well on what you should/should not do in a constructor.

Community
  • 1
  • 1
DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • Yeah, that was another thing I considered doing, but I'm still curious as to how the eventhandler register ends up calling the load function – Chris Hobbs May 20 '15 at 17:04