Populate datagrid in Form1 constructor, window handle error?

P

polarz

I have a menu item that will populate a datagrid when clicked, but
when I try to load the same data on startup I get the following error.

Cannot call Invoke or InvokeAsync on a control until the window handle
has been created.

if(Convert.ToBoolean(loadSum))
{
loadDataDelegate startLDD = new
loadDataDelegate(loadData);
Invoke(startLDD,new object[] {"collection"});
}

Above is the code I use to call my load function. This is the last
instruction in the Form1 constructor. Any suggestions are much
appreciated. Thank you.
 
N

Nicholas Paldino [.NET/C# MVP]

Polarz,

A control relies on the windows message loop to make a call through
Invoke on another thread. Because the handle hasn't been created in the
constructor yet, you can't make calls to Invoke there. Basically, your code
should be called in the Load event of the form, which is where you are
assured that the handle will be created.

Or, you could hook up to the HandleCreated event, if you want to do this
sooner.

Hope this helps.
 
P

polarz

Hello Nicholas, thanks for the reply. Your solution worked, I could
have sworn I'd tried that and it didn't work. Maybe I just needed to
go to bed. In any case thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top