Strange Event Behaviour

C

ChrisM

I posted this last week, so apologies for re-posting but I'm still looking
for a sensible answer, and I'm hoping somone new might be able to cast some
light...


Basically, I have a fairly complicated application which seems to me to be
misbehaving. The following is an attempt to reproduce the error. There are
reasons in the real application why I'm trying to do things the way they are
here, so please no comments on techniques here, UNLESS I'm trying to do
somthing fundamentally wrong (or stupid!)

Anyway, if someone could humour me and try the following:
1) Create a new dotNET project.
2) Add 3 Forms: frmMainForm, frmForm1 and frmForm2. (MainForm is the form
that should load on application startup).
3) Make frmMainForm an MDI Container Form.
4) Add the following code for MainForm:

private void MainForm_Load(object sender, System.EventArgs e)
{
frmForm1 form1 = new frmForm1();
form1.MdiParent = this;
form1.Show();
}

internal void ShowOtherForm()
{
frmForm2 form2 = new frmForm2();
form2.MdiParent = this;
form2.Show();
}

5) Place a button on form1 with the following code:
private void button1_Click(object sender, System.EventArgs e)
{
((frmMainForm)this.MdiParent).ShowOtherForm();
}

6) Place a button and a textBox on form2 with the following code:
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Button Pressed");
this.SendToBack();
}

private void textBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
MessageBox.Show("Enter Pressed");
}

7) Run the program, you should get as a child form inside MainForm
8) Click the button on form1 and you should get an instance of form2.
9) Click the button on form2 and you should get the 'Button Pressed' message
and Form1 comes to the front.
10) CLOSE Form1
11) Make sure focus is in the textBox on Form2 and press ENTER.
12) Please can someone explain what happens next...?

I would really appreciate it if someone could try this out and give me their
thoughts, because it's driving me crazy. I have this project available as a
zip-file if you can't be bothered with all the typing.

Many, many thanks.

ChrisM
 
C

Claes Bergefall

It displays a message box that says "Enter Pressed", just as expected. What
result are you getting?

/claes
 
C

ChrisM

That's interesting!!
When I do it, I get 'Button Pressed' which is what was confusing me.
What environment are you using? I have VS2003 and Framework 1.1.

Cheers,

Chris.

PS. You did have exactly the setup I described. Just copied the code and
didn't make any assumptions of corrections? The only way to make it go wrong
is exactly the steps I described. Not accusing you of being unable to follow
simple instructions, but confused as to why it works correctly for you and
not for me. Unless you are using Framework 2.0 which might explain a lot...
:)

C.
 
C

Claes Bergefall

I used 2.0 for my initial test. Testing it in 1.1 gives the result you're
seeing though. Strange indeed

/claes
 
C

ChrisM

Glad to hear that you can now see the same problem. I thought for a while I
was going mad...
Any idea what might be going wrong? More importantly, any idea how to stop
it from happening???

;-)

Thanks,

Chris.
 
C

Claes Bergefall

It behaves as if the AccepButton property of the form is set to the button,
but it clearly isn't so not sure why that happens. I noticed that it's only
the enter key that triggers the button click event. All other keys gets
routed to the keypress event handler as expected. Looking at the call stack
I noticed that the Form's PropDefaultButton is set and this is what causes
the button click event handler to be invoked (use Reflector and take a look
in the Form.ProcessDialogKey method). Can't tell you why or where it gets
set though, but that might be worth tracking down.

If you don't already have Reflector you should download it. It's a great
tool to see the code in the framework. You can find it at
http://www.aisto.com/roeder/dotnet/

/claes
 
C

ChrisM

Thanks for your input, Claes, it is much appreciated.

What IS (Prop)DefaultButton?? I know what AcceptButton, and CancelButton are
for, but from what I can tell, DefaultButton is an ASP.Net thing, not a
Windows Forms thing?? What could be setting it?

Cheers,

Chris.

PS I didn't have 'Reflector' before, but I do now, and it looks like it
might be useful...
 
C

ChrisM

Hi Claes,

With your invaluable help, I've managed to bodge my way round this problem,
(I hope).
Since, as you pointed out, the problem is stemming from the
'ProcessDialogKey' code, I've stuck the following code into the Form that is
causing the problem:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter && this.ActiveControl == textBox1)
return true;
else
return base.ProcessDialogKey (keyData);
}

In other-words, if you've pressed Enter, and you are in the textBox then
don't carry out the usual ProcessDialogKey code. If you are in any other
control, or pressed any other key then carry on as normal, and pass the call
on up the stack.
Certainly works in my little test App, I'm going to try it in my proper one
now, and see what happens...

Will report back (probably tomorrow)

Thanks once again,

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