ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   proper way to create a userform (https://www.excelbanter.com/excel-programming/341336-proper-way-create-userform.html)

Gixxer_J_97[_2_]

proper way to create a userform
 
Hi all,

just wondering what the 'correct' way to initialize and dispose of a
userform is.

presently i am doing something like this:

private sub UserForm_Initialize()
' clear contents of combo boxes, label captions, etc
' set up initial values of the above
end sub

public sub init(optional arg as variant)
UserForm_Initialize
'any other code to run here / sub calls
MyForm.show
end sub

private sub myButton_clicked()
MyForm.hide
end sub

and externally (from another module)

MyForm.init()


is there a better (more correct) way to do this?

tia

J

Jim Cone

proper way to create a userform
 
j,

A userform goes thru two steps...
1. It is loaded into memory - the Initialize event takes place during loading.
2. It is shown to the user - the Activate event takes place here.

Step 1 (loading) takes place only once and occurs whenever the userform is
referenced in code. So a statement like UserForm1.Width = 500 automatically
loads the form.
Step 2 (showing the form) can take place as many times as you require.
The Activate event will occur each time the form is shown. (UserForm1.Show)
Obviously, the form must be hidden (UserForm1.Hide) each time
before it can be shown again.

It is not necessary to write code to specifically load a userform as
UserForm1.Show will load the form and show it.

The usual procedure (at the programming desk here) is to place any
one time code in the Initialize event in the userform code module.
Any code you want to run each time the form is shown is
placed in the Activate event in the userform code module.
The code for the above would be entered in the form module during
design of the userform and would not have to be referenced again.

The general code module code that initiates the process could be as simple as...
'--------------------------------
Sub GetThingsGoing
Dim strUserInput as String
'Do normal checks for protected workbooks etc.

UserForm1.Show

'(The "close" button on the form should hide the form)

'Get information entered into userform...
strUserInput = UserForm1.TextBox1.Value

'After getting info, release memory required for the form.
Unload UserForm1
Set UserForm1 = Nothing

'Other code here to do something with the string variable.
End Sub
'--------------------------------
Jim Cone
San Francisco, USA


"Gixxer_J_97"
wrote in message
...
Hi all,
just wondering what the 'correct' way to initialize and dispose of a
userform is.
presently i am doing something like this:
private sub UserForm_Initialize()
' clear contents of combo boxes, label captions, etc
' set up initial values of the above
end sub

public sub init(optional arg as variant)
UserForm_Initialize
'any other code to run here / sub calls
MyForm.show
end sub

private sub myButton_clicked()
MyForm.hide
end sub

and externally (from another module)
MyForm.init()

is there a better (more correct) way to do this?
tia
J


All times are GMT +1. The time now is 05:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com