PDA

View Full Version : Solved: event manipulation and list boxes



figment
09-12-2007, 02:53 PM
ok i have too quetions.

first how can i hide and then show a user forum without triggering the UserForm_Activate event?

second. is there any way to tell how many items are selected in a list box, when the multi select property is set?

lucas
09-12-2007, 03:55 PM
I am not sure why you are wanting this and since you don't say....I guess you must be trying to put a userform out of sight and then recall it without it having lost the data you added when it was open the first time.? Is that about right?

figment
09-12-2007, 06:43 PM
got it in one

the forum requires you to give an input to auto cad, and as such it is easier if it gets out of the way. but afterwards i need it to come back in its new state, not its start state.

Bob Phillips
09-13-2007, 01:36 AM
Create an activate method in your form, and clear out the Activate event, and call that when you first activate the form. That way you dictate when the activate code is invoked.

Bob Phillips
09-13-2007, 01:40 AM
For the other bit, you need to loop through counting the selected items.

figment
09-13-2007, 06:23 AM
Create an activate method in your form, and clear out the Activate event, and call that when you first activate the form. That way you dictate when the activate code is invoked.

i am not sure what you mean by an activate method. how would the code know to run it on oppeing if its not in the event module?

Charlize
09-13-2007, 06:50 AM
You could use a boolean the first time and set it to true when you initialize the form. On activating the form you check the value of that boolean. If boolean is true (means you have initalized it once) so don't do the beginning stuff but keep values.

Bob Phillips
09-13-2007, 07:53 AM
What I mean is something like this in the form


Private Sub UserForm_Activate()
'do nothing
End Sub

Private Sub UserForm_Initialize()
'do nothing
End Sub

Public Function Initialize() As Boolean
Initialize = True
On Error GoTo proc_error
'do the things that you would normally do in the Initialize event

proc_exit:
Exit Function

proc_error:
Initialize = False
Resume proc_exit
End Function

Public Function Activate() As Boolean
Activate = True
On Error GoTo proc_error
'do the things that you would normally do in the activate event

proc_exit:
Exit Function

proc_error:
Activate = False
Resume proc_exit
End Function


and then you invoke the form like this



Public Sub Test()
Dim frm As UserForm1

On Error GoTo proc_error
Set frm = New UserForm1
If Not frm.Initialize Then Err.Raise 19999
If Not frm.Activate Then Err.Raise 19999
frm.Show
'when you come out ...
'... do some stuff

'then show form without the activate
frm.Show

'...

proc_exit:
Set frm = Nothing
Exit Sub

proc_error:
'display error details
Resume proc_exit

End Sub

Norie
09-13-2007, 09:27 AM
What's the purpose of not running the activate event?

Bob Phillips
09-13-2007, 11:09 AM
Read the thread.

Norie
09-13-2007, 11:32 AM
Why not use the Initialiaze event instead of the Activate event?

As far as I know it isn't triggered when you just show a userform.

Bob Phillips
09-13-2007, 03:35 PM
Is that asking of me or the OP?

Norie
09-13-2007, 03:41 PM
xld

It's just a suggestion.:)

Like I said as far as I'm aware the Initialize event isn't triggered when you show a userform.

But the Activate event is.

So if the problem is with the Activate event why not use the Initialize event instead?

Bob Phillips
09-14-2007, 03:15 AM
I understand the difference between Initialize and Activate, but I used Activate because, as I said earlier, ... That way you dictate when the activate code is invoked...

If you look at my code as well, you will see that I allowed for Initialize and Activate code, so you have total control over when and where each or both is used. It ceases to be Initialize and Activate really, just two other methods. I stopped using the events and using custom methods some years ago when I was building an app that kept firing the Activate event and trying to stop it got far too complex, this method is simple and foolproof (AFAICS)

figment
09-19-2007, 01:15 PM
thanks for all the help. its greatly appreciated.