Hi, I have a program that has multiple window forms. A parent form that needs to be manipulated rarely and a child form that needs to be manipulated often. I also need the code that controls the parent form to wait until the child form does something. I do so with this loop
while(arrPlayer[n].cont == false)
{
Application.DoEvents();
Thread.Sleep(1000);
}
The child is displayed using a show() since showDialog() would hold up the execution of code under it. This allows the parent form to do things, while waiting for the child to finish it's job(at which point arrPlayer[n].cont becomes true). Here's my problem, the child form isn't taking any keyboard input. I should be able to navigate it's buttons using the arrow keys but i cannot. The mouse works however, but this is extremely annoying to use a mouse to click on buttons. Does anyone have a suggestion on how to fix this?
Thanks!
Loading
david burkePosted May 11, 2008, 6:37 PM
I tried activate, focus, and setting keypreview to true. These do not help. I am considering now to just restructure my code elsewhere.
I believe I could restructure it like this instead of having one very large method. This way would avoid the need to have some code that "pauses" the execution of code like the loop did.
ParentForm()
{
MethodA()
{
...
show()
//show child form, uses a delagate to start method B when desired
}
MethodB()
{...}
}
This way is probably more maintainable anyway, but still it seems like such strange behaviour for the mouse to be able to select a button, but the keyboard can't.
Mike GoldPosted May 7, 2008, 9:51 PM
I don't recommend doing it this way. Use a Timer class and Timer event/event handler instead. The timer class is in the Windows thread and will therefore handle keyboard messages, and mouse clicks normally. Check for your child window here.
The problem with your while loop is that you are putting the main thread (which is also the GUI thread) to sleep. You rarely want to do this, ever.
Also, make sure the child form has focus and call Activate on it, (and activate the control in the child form you want to have focus in the child form as well in the Load event handler). after you call show. Make sure the PreviewKey property is set in the child form also so it can accept keyboard input.