It has to do with setting focus and selections in the Load event, before the
Form is visible. To get around this, explicitly show the Form.
private void Form1_Load(object sender, System.EventArgs e)
{
// Do preparation work here.
for (int x = 0; x < 100; x++)
{
this.listBox1.Items.Add("Item" + x.ToString());
}
this.listBox1.SelectionMode = SelectionMode.MultiExtended;
this.Show();
// Do focused work here.
if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}
}
Alternatively, you could use the Activated event instead of Load. Just note
that the Activated event may fire multile times during the life span of the
Form, depending on what the user does. For example, if the Form loses focus
to another Form, the Activated event will fire again when the Form is
brought back to the foreground. So you would need to handle this by using a
flag that indicates the work you did in the Activated event at startup has
already been done and does not need to be done again.