473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Unable to get events from a form

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
7 1311
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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.OnKeyDow n);

//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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Hi,

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

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7
Thanks for the tip. It worked fine
Cheers
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Al Murphy | last post by:
Hi, I've written a very basic windows application with a pushbutton and a listbox. When the user clicks on the button it should display a message in the listbox. Sounds simple right? Well mine...
0
by: developer | last post by:
I am trying to get a custom provider to receive health monitoring events in vb.net, whidbey beta 1. I have a health monitoring setup like this in my web.config: <healthMonitoring enabled="true">...
2
by: NG WEI CHUNG | last post by:
Hi, Some of my clients site is unable to view the ASP .NET page due to the proxy server and firewall or perhaps IE setting etc, I m not sure what is the actual cause of the problem, it display...
1
by: Marek Murin | last post by:
Hi all, I have created vb.net user control that has to be used by vb6 form. Everything goes well with putting the vb.net user control on the VB6 form until I want to receive any event from my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.