Userform remove caption disabled textbox setfocus

Rolly_Sefu

Board Regular
Joined
Oct 25, 2013
Messages
137
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:

Some videos you may like

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,643
Switch the lines around. Call the remove caption first, then set the focus.
 
Last edited:

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,643
I wonder if you put the Call line into Userform_Activate event, and left the Focus line in the Initialize routine.....
 

Rolly_Sefu

Board Regular
Joined
Oct 25, 2013
Messages
137
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,313
Messages
5,527,952
Members
409,794
Latest member
ajithppajith

This Week's Hot Topics

Top