MDI Child Wrong Event Firing

G

Guest

Environment
---------------
Visual Studio.NET 2003 Version 7.1.3088
..NET Framework 1.1 Version 1.1.4322 SP1
XP Professional 5.1.2600 SP2 Build 2600

Problem Description
-----------------------

I have an mdi parent form.
When it opens it creates 2 identical mdi child forms.
The mdi child form contains a textbox and a button.

In the keydown event of the textbox I trap the enter key and display the
contents of the textbox.
In the button click event I display a message box "Click".

When the application runs the 2 child forms are created and the second child
form has focus.
Using the mouse set focus on the textbox and press enter. The message is
displayed as expected.

Now use the mouse to set focus on the first child forms textbox and press
enter.
This time the button click event fires !!

Next set focus on child 2 textbox and press enter - this again works ok.
Finally return to child1 and press enter in the textbox - this now works and
displays the correct message.


If you take the button off the child form the problem doesn't occurr. Also
when both textboxes are working correctly it breaks again if you click the
button, select the other child form and then return to the original child
form.

Looks like a bug to me but I would be interested to know if anyone else has
experienced this problem or know how to prevent it from hapenning ?


--------------------------------------------------------------------
Source Code
--------------------------------------------------------------------
MDI Parent Form
--------------------------------------------------------------------

Public Class frmMain
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(718, 350)
Me.IsMdiContainer = True
Me.Name = "frmMain"
Me.Text = "frmMain"

End Sub

#End Region

Private Sub frmMain_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Create 2 child forms at startup...
Dim frm1 As New frmChild
With frm1
.MdiParent = Me
.TextBox1.Text = "CHILD1"
.Show()
End With

Dim frm2 As New frmChild
With frm2
.MdiParent = Me
.TextBox1.Text = "CHILD2"
.Show()
.Left = frm1.Width + 50
.Top = frm1.Top
End With

End Sub


End Class



--------------------------------------------------------------------
MDI Child Form
--------------------------------------------------------------------
Public Class frmChild
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(21, 48)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(9, 12)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'frmChild
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(127, 86)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "frmChild"
Me.Text = "frmChild"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

MsgBox("Click")

End Sub

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

If e.KeyCode = Keys.Enter Then
MsgBox(TextBox1.Text)
End If

End Sub
End Class
 
T

tommaso.gastaldi

Hi Lenster ,

could you check whether the textbox taborder in "first child" is the
same as the textbox taborder in "second child" (wiew>taborder). I have
not tried your code but, from what you say, I have the suspect that
"first child" might have order 0 on the button while the other on the
textbox. Could that be?

-tom

Lenster ha scritto:
 
G

Guest

Both child forms are instances of the same underlying form so they share the
same tab order etc.
 

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