Unable to get events from a form

A

a ghost1207

Hi all,

I am trying to figure this out, but I can't: I create a form, then
another form (which I call from the first), then add an event handler
for KeyDown messages in the second form and finally add another event
handler for KeyDown messages in the first form. I end up getting the
events when the second form is open and nothing with the first (basic)
form has the focus. What's wrong?

I noticed that if I reverse the sequence and add an event handler for
the first form first, and then add event handler for the second form, I
get the events in the first form and not in the second! Essentially, the
way I see it, the first event handler that is added to the project is
the one that gets the messages. Is there anyplace in the project that
the event handlers are registered?

Cheers
 
M

Morten Wennevik

Hi ghost,

I suspect you are adding the wrong event handlers or something.
Could you show us some code where you include the part with the
EventHandlers?

You can put EventHandlers anywhere in the project.
If you register several event handlers to the same event they
will be called in turn, first registered, first called.
 
A

a ghost1207

Hi Morten,

Thank you for caring.
Essentially I created a new project, added a second form, added one
keydown procedure for each form:

protected void OnKeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Alo from the first form!");
}

//added automatically to each form in the
//InitializeComponent (through the Design View/Events):
this.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.OnKeyDown);

//added to first form (to be able to open the second form):
private void button1_Click(object sender,System.EventArgs e)
{
secondForm sf=new secondForm();
sf.ShowDialog(this);
}

The whole thing seems very simple. I can't understand where's the
problem!
 
A

a ghost1207

Hi again,

I think I spotted the problem: each time I add a button the designer
adds the following line in the InitializeComponent procedure:

this.Controls.AddRange(new System.Windows.Forms.Control[]
{this.button1});

If I take out this line the button disappears, but the form gets the
keydown events. If I leave the line untouched the button appears where
it should, but the form cannot get the events any more. What's with this
AddRange thing? I'll try to figure out myself, but if you can send me a
pointer or an idea it would be much appreciated.

Cheers
 
A

a ghost1207

Hi again,

I think I got an idea about the issue at hand: the problem lies in
the focus. If the form does not have it it does not receive any events.
So when a button is added the focus goes automatically to it (and never
leaves) thus the procedure is never called unless attached to the button
it self.

To make things worse, if the control happens to be something like a
label (which does not have event handlers), then unless the focus goes
to another control the events are lost.

Cheers
 
M

Morten Wennevik

Hi,

Sorry I have been unable to respond earlier.
You need to add a control to the Form's Controls property or it won't be
added to the Form.
this.Controls.Add(mycontrol) is needed to enable the control, make it
visible etc.

AddRange is just the same as Add, but you can add a Range of Controls in
one go. In your case the Range consists of a Control array which in turn
consists of a single object, button1.

If you delete the line the button won't show (won't be created), and the
Form will get the Key events, because there is no Control to steal the
focus.

Set the Form's KeyPreview property to true to have the Form receive Key
events even if things like Button and TextBoxes have focus.
 
M

Morten Wennevik

Hi,

As explained in the reply to this parent message you need to set
KeyPreview = true for the Form.
 

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