Microsoft KB Archive/289498

= PRB: Form Cannot Get Focus After You Use SetParent to Move Controls to Another Form =

Article ID: 289498

Article Last Modified on 5/15/2001

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q289498



SYMPTOMS
If you use the SetParent function to move a TextBox control from one form to another and then click the TextBox on the new parent form, you cannot click the original parent form to set the focus back to it.



RESOLUTION
To work around this problem, use the SetWindowLong function to change the TextBox control's window style to WS_CHILD, either before or after the call to SetParent, and then make the control visible. For example, if Text1 is moved from Form1 to Form2, use the following code to resolve this problem: SetParent Text1.hwnd, Form2.hwnd SetWindowLong Text1.hwnd, GWL_STYLE, WS_CHILD Text1.Visible = True For additional information, see the code sample in the &quot;More Information&quot; section.



MORE INFORMATION
This problem only occurs for certain controls. For example, the Frame, Toolbar, Tabstrip, Statusbar, Progressbar, Dbgrid, Mmcontrol, Sstab, Coolbar, Updown and Flatscrollbar controls do not require the SetWindowLong workaround. Other controls, such as the Label, Image, Timer, Data, Shape, Imagelist, Adodc, Calender, and Forms 2.0 controls do not have a window handle and, thus, cannot be moved. All of the other controls exhibit the same behavior as the TextBox control.

Important: It is much more complicated to change a control and its container relationship than to change the windows relationship. Thus, Microsoft does not recommend that you use the SetParent function to change the relationship of controls or the parent window in Visual Basic.

Steps to Reproduce Behavior
 Create a new Standard EXE project. Form1 is created by default. Add a Frame control, TextBox control, and two command buttons (Command1 and Command2) to Form1. On the Project menu, click Add Form to add another form (Form2) to the project. On the Project menu, click Add Module to add a new module (Module1) to the project.  Copy the following code into Module1: Public Declare Function SetParent Lib &quot;user32&quot; Alias &quot;SetParent&quot; _ (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Public Declare Function SetWindowLong Lib &quot;user32&quot; Alias &quot;SetWindowLongA&quot; _ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) _ As Long

Public Const GWL_STYLE = (-16) Public Const WS_CHILD = &H40000000   Copy the following code in the general declarations for Form1: Option Explicit

Private Sub Command1_Click SetParent Frame1.hwnd, Form2.hwnd End Sub

Private Sub Command2_Click SetParent Text1.hwnd, Form2.hwnd ' Uncomment these two lines to work around the problem. 'SetWindowLong Text1.hwnd, GWL_STYLE, WS_CHILD 'Text1.Visible = True End Sub

Private Sub Form_Load Form2.Show End Sub </li> Press the F5 key to run the project.</li> Click Command1 to move the Frame control from Form1 to Form2.</li> Click Command2 to move the TextBox control to Form2.</li> Click the Frame control on Form2, and then try to click Form1. Notice that Form1 gets the focus.</li> Click the TextBox control on Form2, and then try to click Form1. Notice that Form1 does not get the focus.</li> On the IDE toolbar, click Stop to stop the project.</li> To work around this problem, uncomment the two indicated lines in Command2_Click, and repeat the test. When you click the TextBox, Form1 gets the focus.</li></ol>

<div class="references_section">