Weird Form Event Occurances

C

ChrisM

Hi,

Don't know if anyone can cast any light on this...
I have a fairly complex C# WinForm with (amongst other) a text box and a
button.
The TextBox has events declared for KeyUp and KeyDown, the button has a
simple Click event.

Under certain circumstances (haven't worked out exactly what they are yet,
but it involves opening and closing a sub form). Pressing ENTER with the
cursor in the textbox causes the Button Click event to fire...!!
Can anyone make ANY suggestions as to where I could start troubleshooting
this?

Thanks,

ChrisM
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

I'd have to see a sample to believe this. I think that maybe you might
be doing something you might not be aware of which results in the code being
executed.

Can you post a working sample?
 
C

ChrisM

Nicholas Paldino said:
Chris,

I'd have to see a sample to believe this. I think that maybe you might
be doing something you might not be aware of which results in the code
being executed.

Can you post a working sample?

I'm sure you're right, but I've checked all the things I can think of, and
there's nothing else that calls the myButton_Click...
code. I can't really post an example as it is an integral part of a fairly
sizable application, and I don't think I can extract the code with the
problem without including most of the rest of the application...

A little more experimentation has shown that the 'KeyDown' event of the
TextBox stops receiving the Enter key when it becomes 'crosswired' with the
Buttons Click event.

I will see if I can create a small working demo of the problem, but I'm not
too hopeful.

Thanks for your reply anyway.

Chris.
 
C

ChrisM

ChrisM said:
OK,

It wasn't actually as hard to reproduce the problem as I thought...
I have attached a Zip file containing a sample project that shows exactly
the problem I'm having.

There are Three forms:
Form1 - An MDI Parent Form.
FormMain - A child form which represents a Menu Form.
FormDemo - Another child form that represents the form with the problem.

To reproduce the problem:
1) Run the application and you should get the Parent form containing the
Menu Form.
2) Click the button - should pull up the other child form.
3) Make sure focus is in the text box and press ENTER - should get
appropriate message.
4) Click the button - should appropriate message, and Menu Form comes to
front.
5) CLOSE Menu Form (Click on X in Top RH Corner)
6) Make sure focus is in the Textbox and press ENTER.
7) Explain that...!

If you have any problems with the attachment, and are prepared to help,
email me on this address and I can email you the code.
( Remove Blue and Suede from my email address to reply).

Thanks,

Chris.



btw, this is all running in VS2003 / Framework 1.1

Chris.
 
C

ChrisM

FALSE, but changing it to TRUE doesn't seem to help...

Thanks anyway. If you want to have a play, see my other post in this thread
with a sample project attached...

Cheers,

Chris.
 
C

Chris Jobson

ChrisM said:
btw, this is all running in VS2003 / Framework 1.1

Very weird! I've run your code and get exactly the same (inexplicable)
results.

If it's any help, I've also run it in VS2005 and Framework 2.0, and there it
behaves properly, so it looks like it's some sort of bug in the Framework
1.1. Sorry, no more time to investigate now. Good luck!

Chris Jobson
 
G

Guest

Chris,
Just for informational purposes, I don't think this group allows for
attachments. Even if it does, many people use the online Web - based version
of the reader, which definitely does not allow for same. So, you'll need to
post code samples "inline" in your message.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
C

ChrisM

Peter,
Thanks for your message. I wasn't sure how many people would be able to get
at the attachment.
In order to show what I'm trying to explain however, requires a complete
mini-project containing 3 forms, too much code to sensibly include in-line
IMHO.
The best I can say is if anyone is interested in trying to help, or just
wants to see what is happening, I can email them the zip file for them to
look at. Hence my offer to email it to any interested parties.

Thanks,

Chris.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What you are seeing is the expected behavior I think.

First if you want to accept the Enter you need to set the textbox to
multiline. if you do not want to show it you can cancel it in the keydown
handler.

Another possible solution is to put the button as TabIndex = 0 and the
textbox as tabindex =1. that very simple change will prevent the "enter" to
be processed by the next control
 
C

ChrisM

Hi,
Thanks for your reply.

I can't really change the Tab Order on my real application, as the tab order
that is set up is the required one. That is, the user needs to go to the
TextBox before they press the button (If they choose to press the button).
I don't want the ENTER to appear in the textbox, the idea is someone enters
some text in the box, and presses ENTER to perform an operation on that text
(in the actual application, it is a Search box). The button performs a
totally different task.

The strange thing is that at first, it all works as expected, pressing ENTER
in the TextBox fires that event, and clicking on the button fires the Click
event. It is only after changing to the second form and back that things
work differently. That is what is confusing me.

If you haven't tried the example project that I wrote to illustrate the
problem, and are still interested, I suggest that you have a look, as it
really does make the problem much clearer than my explanation.

Cheers,

Chris.
 

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