• Log in or sign up for an account here: https://www.sythe.org/login

[Question]Handle Events in a User-Defined Control

Markizano

Active Member

Banned
Joined
Mar 2, 2006
Sythe Gold
0
Messages
100
Location
Aurora, CO
When making a User-Defined control in VB6, how do I tell VB what to do when an event is called. I am currently trying to make a number box to take the place of a TextBox. The only problem with that is I can't figure out how to tell VB to handle the Click event and others. A user-defined control comes with GotFocus, LostFocus and Validate; but I want to add Click, MouseUp, MouseDown, DblClick, KeyPress, KeyUp, KeyDown, and Change (Maybe a few others). I could use some general help to make the code handle the event when it is called and have it be called at the right time.

Any help is appreciated.

Markizano
 
Actually, that's what I'm trying to create. I've learned recently that the Sub UserControl is the SubProgram for the usercontrol, then there's an underscore, then the event name. A basic UserControl that can receive focus in the properties editor comes with the events: "GotFocus", "LostFocus", and "Validate". Yet, these events are nowhere to be found in the module. Now, what if I wanted to add an event that would occur when the mouse is clicked? I put in this code in the General Declarations:
Code:
Event Click()
and now when I make the .OCX and add the component to my toolbox and add that to my form, I can now add this to my code (I'm making a NumberBox to start simple):
Code:
Private Sub NumberBox1_Click()
   'Whatever I want the control to do when clicked goes here.
End Sub
The only problem with that is that Just putting the code "Event Click()" Doesn't mean that when the mouse is pressed and released over the control that it will automatically handle it in that fasion. What I want to do is handle the code for the event.
Something like:
Code:
'Declarations
Event Click()

Private Sub HandleEvent_Click()
    If MouseDown Then
        If MouseUp Then
            Call Click
        End If
    End If
End Sub
Something like that. Hope that cleared things up for you, Covey.

EDIT: Didn't you make your own UserControl for EliteSwitch? I saw one when that kid tried to decompile it.
 
Add this to your usercontrol code:
Code:
Public Event Click()

Private Sub UserControl_Click()
    RaiseEvent Click
End Sub

Ok, kew...Thanks for the method on how to handle events for the regular/basic/given events when a control is created at design-time; now, what if I wanted to create a new event that is not included in the given events, such as...
Code:
Event Random(Number As Integer) ' Randomizes the numbers in the NumberBox if a number is entered.

Private Sub UserControl_Random(Number As Integer)
If UserControl.Number = Number Then RaiseEvent Random
End Sub
Is this the manner that I would proceed to do so?

In the case that I use this for other User-Defined controls, I want to be able to use this for events not included in the default set of events given when the control is created at design-time. I really appreciate what has been given so far. This really helps,
THANK YOU MUCH! :D.
 
Ok Covey, Thank you very much for everything! :D If I have any more questions, of course, I'll post.
 
Back
Top