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.