Userform remove caption disabled textbox setfocus

Rolly_Sefu

Board Regular
Joined
Oct 25, 2013
Messages
149
Hello.

I have a userform that when initialized textbox1.setfocus is run => all is ok

I needed to remove the caption and x button from the userform => textbox1 will not get selected

this is the code I used for the caption removal:

in a module:
Code:
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Long) As Long
Sub RemoveCaption(objForm As Object)
Dim lStyle          As Long
Dim hMenu           As Long
Dim mhWndForm       As Long
    mhWndForm = FindWindow("ThunderDFrame", objForm.Caption) 'XL2000+
    lStyle = GetWindowLong(mhWndForm, -16)
    lStyle = lStyle And Not &HC00000
    SetWindowLong mhWndForm, -16, lStyle
    DrawMenuBar mhWndForm
End Sub
Sub button1()
    UserForm1.Show False
End Sub

in userform:
Code:
Private Sub userform_initialize()
TextBox1.SetFocus
Call RemoveCaption(Me)
End Sub

Does anyone know how to setfocus to the userform after the caption is removed ?
Thanks
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Switch the lines around. Call the remove caption first, then set the focus.
 
Last edited:
Upvote 0
I wonder if you put the Call line into Userform_Activate event, and left the Focus line in the Initialize routine.....
 
Upvote 0
I figured out the solution.

When the userform is call from the button:
Sub button1()
UserForm1.Show False
End Sub
=> textbox1.setfocus => will not work

but if you remove the "False"
Sub button1()
UserForm1.Show
End Sub
=> textbox1.setfocus => WILL WORK.

Thanks for your support.
And I hope this post might also help others.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top