Event Programming In VB6

Level:
Level1

Introduction

Perhaps you're a DOS programmer making the transition to Windows, or a new programmer just getting started with Visual Basic. Regardless of your background, one of the most fundamental concepts you'll need to understand to write programs in Visual Basic is the idea of event driven programming.

In traditional procedural programming, the application starts (typically in a procedure named "Main") and from that point onward has complete responsibility for everything that happens. It must handle keyboad input, mouse input, output to the screen, etc. With Visual Basic (and Windows in general), you no longer need to worry about low level input handling. Windows and Visual Basic take care of the details for you, allowing you to concentrate on what the application is supposed to accomplish.

Lets say for example that you want a program to display a message when the user clicks on a button. With a DOS based program, you would need to track the mouse position, display a cursor, detect the mouse click, redraw the button when its clicked, and display the message. Visual Basic and Windows greatly simplify this process. To accomplish the same task, you simply draw a command button on a form, and write a procedure to handle the click event. VB and Windows will take care of the mouse and fire the event when the user clicks on the button. All you need to do is attach the code to the button's click event and write the code that you wish to run when it occurs.

To add an event procedure and attach it a form or control, just pick the form or control from the "Object" combo box in the code window and choose the event from the "Proc" combo box.
New Event Procedure
VB will create the event procedure for you in the code window. All that's left for you to do is to add the code that you wish to run when the event is fired by VB. Here's the result of our attempt to display a message.

Private Sub cmd_Click()

  MsgBox "Hello World!"

End Sub
When the user clicks on the button "cmd", the message box will be displayed. That's all there is to it. Once you understand this simple - but not entirely obvious - concept, you're ready to start writing code for your VB applications.

Form Events

Visual Basic forms and controls can trigger dozens of events in your applications. Forms, controls, and classes all have events. Let's look at some of the events for forms, and examine how and when they occur.

To demonstrate form events, I created a new form and placed code like this in the event procedure for each of the events:

Private Sub Form_Load()
  Debug.Print "Load"
End Sub
For each event, the name of the event was sent to the debug window. Here was the output:
Initialize
Load
Resize
Activate
Paint
' I closed the form here
QueryUnload
Unload
Terminate
There are other form events also, such as the click event, etc., but those must be triggered by user actions or other code. These are the events that are always triggered for forms and the generic order in which they occur. Let's take a look at each.
  • Initialize
    This event occurs as the form is being loaded, but before the Load event. It gives you the opportunity to initialize data that must be available when the form is loading.
  • Load
    Load is the event most often used to initialize any dynamic components of the form, such as private data, control arrays, or any other element of the design that needs to be setup at run time rather than in design view.
  • Resize
    This event occurs whenever the window state (windowed, minimized, or maximized) changes or whenever the form window is resized by the user. Here is where you place code that is dependent on the form size or window state. If, for example, you are building a text editor, you might resize a text box to the internal size of the form in the Resize event.
  • Activate
    This event occurs whenever the form window gets the input focus. If you have code that tracks the active form in the application, you might use this event for that code.
  • Paint
    This is where VB actually draw the form on the screen. If you are drawing directly on the form with graphics methods, that code might go in this event.
  • QueryUnload
    This event is fired when the form is closed. You can use the event to determine the way the form is being closed (through your own code, by the user, or by Windows) and can also cancel the event and prevent the form from being closed.
  • Unload
    This event occurs when the form unloads.
  • Terminate
    This event occurs when the form is destroyed.
There is often some confusion over the three events that occur when a form is being destroyed - the QueryUnload, Unload, and Terminate events. Of the three, the QueryUnload event is the only one which can be cancelled. The key to remember is that a form isn't completely removed from memory until after the Terminate event has finished. Compare the following two statements:
Unload Form1
Set Form1 = Nothing
The first statement will cause the QueryUnload and the Unload events to fire, but only by using the second statement can you force the Terminate event to fire. If you have form level variables, the values will not be reset after the Unload event has fired. They are only reset after the Terminate event fires and the form is completely removed from memory.

Control Events

Controls also have events, but the events which occur vary by the type of control. Most controls will have a full set of keyboard and mouse events, including KeyUp, KeyDown, KeyPress, MouseUp, MouseDown, MouseMove, Click, and DblClick. The nature of the events is exactly what you would expect given the name of the event. There are also events to support drag and drop operations, including DragDrop and DragOver.

Some other common control events are Change, which occurs when the data in a control is changed, GotFocus and LostFocus, which occur when the user enters or leaves the control using the mouse or keyboard, and the Link... events, which are used for DDE operations.

Among all the control events, the one which is used most often in applications is the click event - normally attached to command button controls and menu controls.


Special Note for Access Programmers

If you have experience programming in Microsoft Access using Access Basic or macros, one of the things you'll find missing are the various BeforeUpdate and AfterUpdate events for forms and controls. If you're used to using these events, you're going to have to find some other means of doing whatever you were doing there. While there is a Change event for controls, the event is fired after every change - meaning after every keystroke while the user is typing.

On the other hand, if you've ever had to write a form level keyboard handler in Access, you'll find the Visual Basic KeyPreview property for forms will greatly simplify your efforts.

If you enjoyed this post, subscribe for updates (it's free)

kl

kl

Event Programming in vb6. I

Event Programming in vb6. I wish you would stick to the point next time. most of the world cares less about dos

conditional driven events

ok, im programming a blackjack game and i'm really confused. im a grade 11 student and was only introduced to the VB6 IDE like 1.5 mths ago. i really need help. in the 'hitMe' procedure, i want to show the computer's cards if im bust, but i dont want to type it all again (dont say copy and paste.) is there anyway i can like make a separate piece of code that triggers when im bust. I would like to do that with all pieces of code concerning showing the cards on the form. itll really cut down on the length. Thanks, heres my code:

Option Explicit
'Author: anonymous
'Date: 01st, January, 1901
'Description: This program allows the user to play a game of Blackjack with the computer. It is still
' in the making. After reading this code (if you do finish), I guarantee you you'll wonder why
' programmers don't commit suicide.

'Declaring global variables which are to be used throughout the entire code
Dim uCard1 As Integer
Dim uCard2 As Integer
Dim uCard3 As Integer
Dim uCard4 As Integer
Dim cCard1 As Integer
Dim cCard2 As Integer
Dim cCard3 As Integer
Dim cCard4 As Integer
Dim uSuit1 As Integer
Dim uSuit2 As Integer
Dim uSuit3 As Integer
Dim uSuit4 As Integer
Dim cSuit1 As Integer
Dim cSuit2 As Integer
Dim cSuit3 As Integer
Dim cSuit4 As Integer
Dim uTotal As Integer
Dim cTotal As Integer

'allows the user to return to the game menu
Private Sub cmdBack_Click()

Dim frmGameMenu As New frmGameMenu
Load frmGameMenu
frmGameMenu.Visible = True
Unload Me

End Sub

Private Sub cmdDeal_Click()

uCard1 = 0
uCard2 = 0
uCard3 = 0
uCard4 = 0
cCard1 = 0
cCard2 = 0
cCard3 = 0
cCard4 = 0
uSuit1 = 0
uSuit2 = 0
uSuit3 = 0
uSuit4 = 0
cSuit1 = 0
cSuit2 = 0
cSuit3 = 0
cSuit4 = 0
uTotal = 0
cTotal = 0

imgCompCard1.Picture = LoadPicture()
imgCompCard2.Picture = LoadPicture()
imgCompCard3.Picture = LoadPicture()
imgCompCard4.Picture = LoadPicture()

imgUserCard1.Picture = LoadPicture()
imgUserCard2.Picture = LoadPicture()
imgUserCard3.Picture = LoadPicture()
imgUserCard4.Picture = LoadPicture()

'loads the card images from another form named frmAllCards. You may see this portion of code later on in
'this form but it will not be commented
Dim frmAllCards As New frmAllCards
Load frmAllCards

Randomize Timer 'randomizes the computers timer so that a random card can be generated for dealing

'If suit is 1 = spades, 2 = clubs, 3 = hearts, 4 = diamonds
uSuit1 = Int(Rnd * 4) + 1
uSuit2 = Int(Rnd * 4) + 1

cSuit1 = Int(Rnd * 4) + 1
cSuit2 = Int(Rnd * 4) + 1

'ensures that the card delt was not delt before
uCard1 = Int(Rnd * 13) + 1
uCard2 = Int(Rnd * 13) + 1
Do While (uCard2 = uCard1) And (uSuit2 = uSuit1)
uCard2 = Int(Rnd * 13) + 1
Loop

cCard1 = Int(Rnd * 13) + 1

Do While ((cCard1 = uCard1) And (cSuit1 = uSuit1)) Or ((cCard1 = uCard2) And (cSuit1 = uSuit2))
cCard1 = Int(Rnd * 13) + 1
Loop

cCard2 = Int(Rnd * 13) + 1
Do While ((cCard2 = uCard1) And (cSuit2 = uSuit1)) Or ((cCard2 = uCard2) And (cSuit2 = uSuit2)) Or ((cCard2 = cCard1) And (cSuit2 = cSuit1))
cCard2 = Int(Rnd * 13) + 1
Loop

'this bit of code allows the user to see the cards that were delt to him
If uSuit1 = 1 Then
If uCard1 = 1 Then
imgUserCard1.Picture = frmAllCards.Image1
uCard1 = 11 '**************************************
ElseIf uCard1 = 2 Then '*
imgUserCard1.Picture = frmAllCards.Image2 '* these bits of code which is seen alot in the code,
uCard1 = 2 '*****************************************->sets the value of the card which is usually used
ElseIf uCard1 = 3 Then '* in the game, eg. the king -> Card13 = 10 pts.
imgUserCard1.Picture = frmAllCards.Image3 '*
uCard1 = 3 '***************************************
ElseIf uCard1 = 4 Then
imgUserCard1.Picture = frmAllCards.Image4
uCard1 = 4
ElseIf uCard1 = 5 Then
imgUserCard1.Picture = frmAllCards.Image5
uCard1 = 5
ElseIf uCard1 = 6 Then
imgUserCard1.Picture = frmAllCards.Image6
uCard1 = 6
ElseIf uCard1 = 7 Then
imgUserCard1.Picture = frmAllCards.Image7
uCard1 = 7
ElseIf uCard1 = 8 Then
imgUserCard1.Picture = frmAllCards.Image8
uCard1 = 8
ElseIf uCard1 = 9 Then
imgUserCard1.Picture = frmAllCards.Image9
uCard1 = 9
ElseIf uCard1 = 10 Then
imgUserCard1.Picture = frmAllCards.Image10
uCard1 = 10
ElseIf uCard1 = 11 Then
imgUserCard1.Picture = frmAllCards.Image11
uCard1 = 10
ElseIf uCard1 = 12 Then
imgUserCard1.Picture = frmAllCards.Image12
uCard1 = 10
ElseIf uCard1 = 13 Then
imgUserCard1.Picture = frmAllCards.Image13
uCard1 = 10
End If
ElseIf uSuit1 = 2 Then
If uCard1 = 1 Then
imgUserCard1.Picture = frmAllCards.Image14
uCard1 = 11
ElseIf uCard1 = 2 Then
imgUserCard1.Picture = frmAllCards.Image15
uCard1 = 2
ElseIf uCard1 = 3 Then
imgUserCard1.Picture = frmAllCards.Image16
uCard1 = 3
ElseIf uCard1 = 4 Then
imgUserCard1.Picture = frmAllCards.Image17
uCard1 = 4
ElseIf uCard1 = 5 Then
imgUserCard1.Picture = frmAllCards.Image18
uCard1 = 5
ElseIf uCard1 = 6 Then
imgUserCard1.Picture = frmAllCards.Image19
uCard1 = 6
ElseIf uCard1 = 7 Then
imgUserCard1.Picture = frmAllCards.Image20
uCard1 = 7
ElseIf uCard1 = 8 Then
imgUserCard1.Picture = frmAllCards.Image21
uCard1 = 8
ElseIf uCard1 = 9 Then
imgUserCard1.Picture = frmAllCards.Image22
uCard1 = 9
ElseIf uCard1 = 10 Then
imgUserCard1.Picture = frmAllCards.Image23
uCard1 = 10
ElseIf uCard1 = 11 Then
imgUserCard1.Picture = frmAllCards.Image24
uCard1 = 10
ElseIf uCard1 = 12 Then
imgUserCard1.Picture = frmAllCards.Image25
uCard1 = 10
ElseIf uCard1 = 13 Then
imgUserCard1.Picture = frmAllCards.Image26
uCard1 = 10
End If
ElseIf uSuit1 = 3 Then
If uCard1 = 1 Then
imgUserCard1.Picture = frmAllCards.Image27
uCard1 = 11
ElseIf uCard1 = 2 Then
imgUserCard1.Picture = frmAllCards.Image28
uCard1 = 2
ElseIf uCard1 = 3 Then
imgUserCard1.Picture = frmAllCards.Image29
uCard1 = 3
ElseIf uCard1 = 4 Then
imgUserCard1.Picture = frmAllCards.Image30
uCard1 = 4
ElseIf uCard1 = 5 Then
imgUserCard1.Picture = frmAllCards.Image31
uCard1 = 5
ElseIf uCard1 = 6 Then
imgUserCard1.Picture = frmAllCards.Image32
uCard1 = 6
ElseIf uCard1 = 7 Then
imgUserCard1.Picture = frmAllCards.Image33
uCard1 = 7
ElseIf uCard1 = 8 Then
imgUserCard1.Picture = frmAllCards.Image34
uCard1 = 8
ElseIf uCard1 = 9 Then
imgUserCard1.Picture = frmAllCards.Image35
uCard1 = 9
ElseIf uCard1 = 10 Then
imgUserCard1.Picture = frmAllCards.Image36
uCard1 = 10
ElseIf uCard1 = 11 Then
imgUserCard1.Picture = frmAllCards.Image37
uCard1 = 10
ElseIf uCard1 = 12 Then
imgUserCard1.Picture = frmAllCards.Image38
uCard1 = 10
ElseIf uCard1 = 13 Then
imgUserCard1.Picture = frmAllCards.Image39
uCard1 = 10
End If
ElseIf uSuit1 = 4 Then
If uCard1 = 1 Then
imgUserCard1.Picture = frmAllCards.Image40
uCard1 = 11
ElseIf uCard1 = 2 Then
imgUserCard1.Picture = frmAllCards.Image41
uCard1 = 2
ElseIf uCard1 = 3 Then
imgUserCard1.Picture = frmAllCards.Image42
uCard1 = 3
ElseIf uCard1 = 4 Then
imgUserCard1.Picture = frmAllCards.Image43
uCard1 = 4
ElseIf uCard1 = 5 Then
imgUserCard1.Picture = frmAllCards.Image44
uCard1 = 5
ElseIf uCard1 = 6 Then
imgUserCard1.Picture = frmAllCards.Image45
uCard1 = 6
ElseIf uCard1 = 7 Then
imgUserCard1.Picture = frmAllCards.Image46
uCard1 = 7
ElseIf uCard1 = 8 Then
imgUserCard1.Picture = frmAllCards.Image47
uCard1 = 8
ElseIf uCard1 = 9 Then
imgUserCard1.Picture = frmAllCards.Image48
uCard1 = 9
ElseIf uCard1 = 10 Then
imgUserCard1.Picture = frmAllCards.Image49
uCard1 = 10
ElseIf uCard1 = 11 Then
imgUserCard1.Picture = frmAllCards.Image50
uCard1 = 10
ElseIf uCard1 = 12 Then
imgUserCard1.Picture = frmAllCards.Image51
uCard1 = 10
ElseIf uCard1 = 13 Then
imgUserCard1.Picture = frmAllCards.Image52
uCard1 = 10
End If
End If

If uSuit2 = 1 Then
If uCard2 = 1 Then
imgUserCard2.Picture = frmAllCards.Image1
uCard2 = 11
ElseIf uCard2 = 2 Then
imgUserCard2.Picture = frmAllCards.Image2
uCard2 = 2
ElseIf uCard2 = 3 Then
imgUserCard2.Picture = frmAllCards.Image3
uCard2 = 3
ElseIf uCard2 = 4 Then
imgUserCard2.Picture = frmAllCards.Image4
uCard2 = 4
ElseIf uCard2 = 5 Then
imgUserCard2.Picture = frmAllCards.Image5
uCard2 = 5
ElseIf uCard2 = 6 Then
imgUserCard2.Picture = frmAllCards.Image6
uCard2 = 6
ElseIf uCard2 = 7 Then
imgUserCard2.Picture = frmAllCards.Image7
uCard2 = 7
ElseIf uCard2 = 8 Then
imgUserCard2.Picture = frmAllCards.Image8
uCard2 = 8
ElseIf uCard2 = 9 Then
imgUserCard2.Picture = frmAllCards.Image9
uCard2 = 9
ElseIf uCard2 = 10 Then
imgUserCard2.Picture = frmAllCards.Image10
uCard2 = 10
ElseIf uCard2 = 11 Then
imgUserCard2.Picture = frmAllCards.Image11
uCard2 = 10
ElseIf uCard2 = 12 Then
imgUserCard2.Picture = frmAllCards.Image12
uCard2 = 10
ElseIf uCard2 = 13 Then
imgUserCard2.Picture = frmAllCards.Image13
uCard2 = 10
End If
ElseIf uSuit2 = 2 Then
If uCard2 = 1 Then
imgUserCard2.Picture = frmAllCards.Image14
uCard2 = 11
ElseIf uCard2 = 2 Then
imgUserCard2.Picture = frmAllCards.Image15
uCard2 = 2
ElseIf uCard2 = 3 Then
imgUserCard2.Picture = frmAllCards.Image16
uCard2 = 3
ElseIf uCard2 = 4 Then
imgUserCard2.Picture = frmAllCards.Image17
uCard2 = 4
ElseIf uCard2 = 5 Then
imgUserCard2.Picture = frmAllCards.Image18
uCard2 = 5
ElseIf uCard2 = 6 Then
imgUserCard2.Picture = frmAllCards.Image19
uCard2 = 6
ElseIf uCard2 = 7 Then
imgUserCard2.Picture = frmAllCards.Image20
uCard2 = 7
ElseIf uCard2 = 8 Then
imgUserCard2.Picture = frmAllCards.Image21
uCard2 = 8
ElseIf uCard2 = 9 Then
imgUserCard2.Picture = frmAllCards.Image22
uCard2 = 9
ElseIf uCard2 = 10 Then
imgUserCard2.Picture = frmAllCards.Image23
uCard2 = 10
ElseIf uCard2 = 11 Then
imgUserCard2.Picture = frmAllCards.Image24
uCard2 = 10
ElseIf uCard2 = 12 Then
imgUserCard2.Picture = frmAllCards.Image25
uCard2 = 10
ElseIf uCard2 = 13 Then
imgUserCard2.Picture = frmAllCards.Image26
uCard2 = 10
End If
ElseIf uSuit2 = 3 Then
If uCard2 = 1 Then
imgUserCard2.Picture = frmAllCards.Image27
uCard2 = 11
ElseIf uCard2 = 2 Then
imgUserCard2.Picture = frmAllCards.Image28
uCard2 = 2
ElseIf uCard2 = 3 Then
imgUserCard2.Picture = frmAllCards.Image29
uCard2 = 3
ElseIf uCard2 = 4 Then
imgUserCard2.Picture = frmAllCards.Image30
uCard2 = 4
ElseIf uCard2 = 5 Then
imgUserCard2.Picture = frmAllCards.Image31
uCard2 = 5
ElseIf uCard2 = 6 Then
imgUserCard2.Picture = frmAllCards.Image32
uCard2 = 6
ElseIf uCard2 = 7 Then
imgUserCard2.Picture = frmAllCards.Image33
uCard2 = 7
ElseIf uCard2 = 8 Then
imgUserCard2.Picture = frmAllCards.Image34
uCard2 = 8
ElseIf uCard2 = 9 Then
imgUserCard2.Picture = frmAllCards.Image35
uCard2 = 9
ElseIf uCard2 = 10 Then
imgUserCard2.Picture = frmAllCards.Image36
uCard2 = 10
ElseIf uCard2 = 11 Then
imgUserCard2.Picture = frmAllCards.Image37
uCard2 = 10
ElseIf uCard2 = 12 Then
imgUserCard2.Picture = frmAllCards.Image38
uCard2 = 10
ElseIf uCard2 = 13 Then
imgUserCard2.Picture = frmAllCards.Image39
uCard2 = 10
End If
ElseIf uSuit2 = 4 Then
If uCard2 = 1 Then
imgUserCard2.Picture = frmAllCards.Image40
uCard2 = 11
ElseIf uCard2 = 2 Then
imgUserCard2.Picture = frmAllCards.Image41
uCard2 = 2
ElseIf uCard2 = 3 Then
imgUserCard2.Picture = frmAllCards.Image42
uCard2 = 3
ElseIf uCard2 = 4 Then
imgUserCard2.Picture = frmAllCards.Image43
uCard2 = 4
ElseIf uCard2 = 5 Then
imgUserCard2.Picture = frmAllCards.Image44
uCard2 = 5
ElseIf uCard2 = 6 Then
imgUserCard2.Picture = frmAllCards.Image45
uCard2 = 6
ElseIf uCard2 = 7 Then
imgUserCard2.Picture = frmAllCards.Image46
uCard2 = 7
ElseIf uCard2 = 8 Then
imgUserCard2.Picture = frmAllCards.Image47
uCard2 = 8
ElseIf uCard2 = 9 Then
imgUserCard2.Picture = frmAllCards.Image48
uCard2 = 9
ElseIf uCard2 = 10 Then
imgUserCard2.Picture = frmAllCards.Image49
uCard2 = 10
ElseIf uCard2 = 11 Then
imgUserCard2.Picture = frmAllCards.Image50
uCard2 = 10
ElseIf uCard2 = 12 Then
imgUserCard2.Picture = frmAllCards.Image51
uCard2 = 10
ElseIf uCard2 = 13 Then
imgUserCard2.Picture = frmAllCards.Image52
uCard2 = 10
End If
End If

imgCompCard1.Picture = Image1
imgCompCard2.Picture = Image1

'If cSuit1 = 1 Then
' If cCard1 = 1 Then
' imgCompCard1.Picture = frmAllCards.Image1
' cCard1 = 11 '**************************************
' ElseIf cCard1 = 2 Then '*
' imgCompCard1.Picture = frmAllCards.Image2 '* these bits of code which is seen alot in the code,
' cCard1 = 2 '*****************************************->sets the value of the card which is usually used
' ElseIf cCard1 = 3 Then '* in the game, eg. the king -> Card13 = 10 pts.
' imgCompCard1.Picture = frmAllCards.Image3 '*
' cCard1 = 3 '***************************************
' ElseIf cCard1 = 4 Then
' imgCompCard1.Picture = frmAllCards.Image4
' cCard1 = 4
' ElseIf cCard1 = 5 Then
' imgCompCard1.Picture = frmAllCards.Image5
' cCard1 = 5
' ElseIf cCard1 = 6 Then
' imgCompCard1.Picture = frmAllCards.Image6
' cCard1 = 6
' ElseIf cCard1 = 7 Then
' imgCompCard1.Picture = frmAllCards.Image7
' cCard1 = 7
' ElseIf cCard1 = 8 Then
' imgCompCard1.Picture = frmAllCards.Image8
' cCard1 = 8
' ElseIf cCard1 = 9 Then
' imgCompCard1.Picture = frmAllCards.Image9
' cCard1 = 9
' ElseIf cCard1 = 10 Then
' imgCompCard1.Picture = frmAllCards.Image10
' cCard1 = 10
' ElseIf cCard1 = 11 Then
' imgCompCard1.Picture = frmAllCards.Image11
' cCard1 = 10
' ElseIf cCard1 = 12 Then
' imgCompCard1.Picture = frmAllCards.Image12
' cCard1 = 10
' ElseIf cCard1 = 13 Then
' imgCompCard1.Picture = frmAllCards.Image13
' cCard1 = 10
' End If
'ElseIf cSuit1 = 2 Then
' If cCard1 = 1 Then
' imgCompCard1.Picture = frmAllCards.Image14
' cCard1 = 11
' ElseIf cCard1 = 2 Then
' imgCompCard1.Picture = frmAllCards.Image15
' cCard1 = 2
' ElseIf cCard1 = 3 Then
' imgCompCard1.Picture = frmAllCards.Image16
' cCard1 = 3
' ElseIf cCard1 = 4 Then
' imgCompCard1.Picture = frmAllCards.Image17
' cCard1 = 4
' ElseIf cCard1 = 5 Then
' imgCompCard1.Picture = frmAllCards.Image18
' cCard1 = 5
' ElseIf cCard1 = 6 Then
' imgCompCard1.Picture = frmAllCards.Image19
' cCard1 = 6
' ElseIf cCard1 = 7 Then
' imgCompCard1.Picture = frmAllCards.Image20
' cCard1 = 7
' ElseIf cCard1 = 8 Then
' imgCompCard1.Picture = frmAllCards.Image21
' cCard1 = 8
' ElseIf cCard1 = 9 Then
' imgCompCard1.Picture = frmAllCards.Image22
' cCard1 = 9
' ElseIf cCard1 = 10 Then
' imgCompCard1.Picture = frmAllCards.Image23
' cCard1 = 10
' ElseIf cCard1 = 11 Then
' imgCompCard1.Picture = frmAllCards.Image24
' cCard1 = 10
' ElseIf cCard1 = 12 Then
' imgCompCard1.Picture = frmAllCards.Image25
' cCard1 = 10
' ElseIf cCard1 = 13 Then
' imgCompCard1.Picture = frmAllCards.Image26
' cCard1 = 10
' End If
'ElseIf cSuit1 = 3 Then
' If cCard1 = 1 Then
' imgCompCard1.Picture = frmAllCards.Image27
' cCard1 = 11
' ElseIf cCard1 = 2 Then
' imgCompCard1.Picture = frmAllCards.Image28
' cCard1 = 2
' ElseIf cCard1 = 3 Then
' imgCompCard1.Picture = frmAllCards.Image29
' cCard1 = 3
' ElseIf cCard1 = 4 Then
' imgCompCard1.Picture = frmAllCards.Image30
' cCard1 = 4
' ElseIf cCard1 = 5 Then
' imgCompCard1.Picture = frmAllCards.Image31
' cCard1 = 5
' ElseIf cCard1 = 6 Then
' imgCompCard1.Picture = frmAllCards.Image32
' cCard1 = 6
' ElseIf cCard1 = 7 Then
' imgCompCard1.Picture = frmAllCards.Image33
' cCard1 = 7
' ElseIf cCard1 = 8 Then
' imgCompCard1.Picture = frmAllCards.Image34
' cCard1 = 8
' ElseIf cCard1 = 9 Then
' imgCompCard1.Picture = frmAllCards.Image35
' cCard1 = 9
' ElseIf cCard1 = 10 Then
' imgCompCard1.Picture = frmAllCards.Image36
' cCard1 = 10
' ElseIf cCard1 = 11 Then
' imgCompCard1.Picture = frmAllCards.Image37
' cCard1 = 10
' ElseIf cCard1 = 12 Then
' imgCompCard1.Picture = frmAllCards.Image38
' cCard1 = 10
' ElseIf cCard1 = 13 Then
' imgCompCard1.Picture = frmAllCards.Image39
' cCard1 = 10
' End If
'ElseIf cSuit1 = 4 Then
' If cCard1 = 1 Then
' imgCompCard1.Picture = frmAllCards.Image40
' cCard1 = 11
' ElseIf cCard1 = 2 Then
' imgCompCard1.Picture = frmAllCards.Image41
' cCard1 = 2
' ElseIf cCard1 = 3 Then
' imgCompCard1.Picture = frmAllCards.Image42
' cCard1 = 3
' ElseIf cCard1 = 4 Then
' imgCompCard1.Picture = frmAllCards.Image43
' cCard1 = 4
' ElseIf cCard1 = 5 Then
' imgCompCard1.Picture = frmAllCards.Image44
' cCard1 = 5
' ElseIf cCard1 = 6 Then
' imgCompCard1.Picture = frmAllCards.Image45
' cCard1 = 6
' ElseIf cCard1 = 7 Then
' imgCompCard1.Picture = frmAllCards.Image46
' cCard1 = 7
' ElseIf cCard1 = 8 Then
' imgCompCard1.Picture = frmAllCards.Image47
' cCard1 = 8
' ElseIf cCard1 = 9 Then
' imgCompCard1.Picture = frmAllCards.Image48
' cCard1 = 9
' ElseIf cCard1 = 10 Then
' imgCompCard1.Picture = frmAllCards.Image49
' cCard1 = 10
' ElseIf cCard1 = 11 Then
' imgCompCard1.Picture = frmAllCards.Image50
' cCard1 = 10
' ElseIf cCard1 = 12 Then
' imgCompCard1.Picture = frmAllCards.Image51
' cCard1 = 10
' ElseIf cCard1 = 13 Then
' imgCompCard1.Picture = frmAllCards.Image52
' cCard1 = 10
' End If
'End If
'
'If cSuit2 = 1 Then
' If cCard2 = 1 Then
' imgCompCard2.Picture = frmAllCards.Image1
' cCard2 = 11
' ElseIf cCard2 = 2 Then
' imgCompCard2.Picture = frmAllCards.Image2
' cCard2 = 2
' ElseIf cCard2 = 3 Then
' imgCompCard2.Picture = frmAllCards.Image3
' cCard2 = 3
' ElseIf cCard2 = 4 Then
' imgCompCard2.Picture = frmAllCards.Image4
' cCard2 = 4
' ElseIf cCard2 = 5 Then
' imgCompCard2.Picture = frmAllCards.Image5
' cCard2 = 5
' ElseIf cCard2 = 6 Then
' imgCompCard2.Picture = frmAllCards.Image6
' cCard2 = 6
' ElseIf cCard2 = 7 Then
' imgCompCard2.Picture = frmAllCards.Image7
' cCard2 = 7
' ElseIf cCard2 = 8 Then
' imgCompCard2.Picture = frmAllCards.Image8
' cCard2 = 8
' ElseIf cCard2 = 9 Then
' imgCompCard2.Picture = frmAllCards.Image9
' cCard2 = 9
' ElseIf cCard2 = 10 Then
' imgCompCard2.Picture = frmAllCards.Image10
' cCard2 = 10
' ElseIf cCard2 = 11 Then
' imgCompCard2.Picture = frmAllCards.Image11
' cCard2 = 10
' ElseIf cCard2 = 12 Then
' imgCompCard2.Picture = frmAllCards.Image12
' cCard2 = 10
' ElseIf cCard2 = 13 Then
' imgCompCard2.Picture = frmAllCards.Image13
' cCard2 = 10
' End If
'ElseIf cSuit2 = 2 Then
' If cCard2 = 1 Then
' imgCompCard2.Picture = frmAllCards.Image14
' cCard2 = 11
' ElseIf cCard2 = 2 Then
' imgCompCard2.Picture = frmAllCards.Image15
' cCard2 = 2
' ElseIf cCard2 = 3 Then
' imgCompCard2.Picture = frmAllCards.Image16
' cCard2 = 3
' ElseIf cCard2 = 4 Then
' imgCompCard2.Picture = frmAllCards.Image17
' cCard2 = 4
' ElseIf cCard2 = 5 Then
' imgCompCard2.Picture = frmAllCards.Image18
' cCard2 = 5
' ElseIf cCard2 = 6 Then
' imgCompCard2.Picture = frmAllCards.Image19
' cCard2 = 6
' ElseIf cCard2 = 7 Then
' imgCompCard2.Picture = frmAllCards.Image20
' cCard2 = 7
' ElseIf cCard2 = 8 Then
' imgCompCard2.Picture = frmAllCards.Image21
' cCard2 = 8
' ElseIf cCard2 = 9 Then
' imgCompCard2.Picture = frmAllCards.Image22
' cCard2 = 9
' ElseIf cCard2 = 10 Then
' imgCompCard2.Picture = frmAllCards.Image23
' cCard2 = 10
' ElseIf cCard2 = 11 Then
' imgCompCard2.Picture = frmAllCards.Image24
' cCard2 = 10
' ElseIf cCard2 = 12 Then
' imgCompCard2.Picture = frmAllCards.Image25
' cCard2 = 10
' ElseIf cCard2 = 13 Then
' imgCompCard2.Picture = frmAllCards.Image26
' cCard2 = 10
' End If
'ElseIf cSuit2 = 3 Then
' If cCard2 = 1 Then
' imgCompCard2.Picture = frmAllCards.Image27
' cCard2 = 11
' ElseIf cCard2 = 2 Then
' imgCompCard2.Picture = frmAllCards.Image28
' cCard2 = 2
' ElseIf cCard2 = 3 Then
' imgCompCard2.Picture = frmAllCards.Image29
' cCard2 = 3
' ElseIf cCard2 = 4 Then
' imgCompCard2.Picture = frmAllCards.Image30
' cCard2 = 4
' ElseIf cCard2 = 5 Then
' imgCompCard2.Picture = frmAllCards.Image31
' cCard2 = 5
' ElseIf cCard2 = 6 Then
' imgCompCard2.Picture = frmAllCards.Image32
' cCard2 = 6
' ElseIf cCard2 = 7 Then
' imgCompCard2.Picture = frmAllCards.Image33
' cCard2 = 7
' ElseIf cCard2 = 8 Then
' imgCompCard2.Picture = frmAllCards.Image34
' cCard2 = 8
' ElseIf cCard2 = 9 Then
' imgCompCard2.Picture = frmAllCards.Image35
' cCard2 = 9
' ElseIf cCard2 = 10 Then
' imgCompCard2.Picture = frmAllCards.Image36
' cCard2 = 10
' ElseIf cCard2 = 11 Then
' imgCompCard2.Picture = frmAllCards.Image37
' cCard2 = 10
' ElseIf cCard2 = 12 Then
' imgCompCard2.Picture = frmAllCards.Image38
' cCard2 = 10
' ElseIf cCard2 = 13 Then
' imgCompCard2.Picture = frmAllCards.Image39
' cCard2 = 10
' End If
'ElseIf cSuit2 = 4 Then
' If cCard2 = 1 Then
' imgCompCard2.Picture = frmAllCards.Image40
' cCard2 = 11
' ElseIf cCard2 = 2 Then
' imgCompCard2.Picture = frmAllCards.Image41
' cCard2 = 2
' ElseIf cCard2 = 3 Then
' imgCompCard2.Picture = frmAllCards.Image42
' cCard2 = 3
' ElseIf cCard2 = 4 Then
' imgCompCard2.Picture = frmAllCards.Image43
' cCard2 = 4
' ElseIf cCard2 = 5 Then
' imgCompCard2.Picture = frmAllCards.Image44
' cCard2 = 5
' ElseIf cCard2 = 6 Then
' imgCompCard2.Picture = frmAllCards.Image45
' cCard2 = 6
' ElseIf cCard2 = 7 Then
' imgCompCard2.Picture = frmAllCards.Image46
' cCard2 = 7
' ElseIf cCard2 = 8 Then
' imgCompCard2.Picture = frmAllCards.Image47
' cCard2 = 8
' ElseIf cCard2 = 9 Then
' imgCompCard2.Picture = frmAllCards.Image48
' cCard2 = 9
' ElseIf cCard2 = 10 Then
' imgCompCard2.Picture = frmAllCards.Image49
' cCard2 = 10
' ElseIf cCard2 = 11 Then
' imgCompCard2.Picture = frmAllCards.Image50
' cCard2 = 10
' ElseIf cCard2 = 12 Then
' imgCompCard2.Picture = frmAllCards.Image51
' cCard2 = 10
' ElseIf cCard2 = 13 Then
' imgCompCard2.Picture = frmAllCards.Image52
' cCard2 = 10
' End If
'End If

cTotal = cCard1 + cCard2 + cCard3 + cCard4
uTotal = uCard1 + uCard2 + uCard3 + uCard4

Cls
Print ("cTotal " & cTotal)
Print ("cCard1 " & cCard1)
Print ("cCard2 " & cCard2)
Print ("cCard3 " & cCard3)
Print ("cCard4 " & cCard4)
Print ""
Print ("uTotal " & uTotal)
Print ("uCard1 " & uCard1)
Print ("uCard2 " & uCard2)
Print ("uCard3 " & uCard3)
Print ("uCard4 " & uCard4)

End Sub

Private Sub cmdHitMe_Click()

Dim frmAllCards As New frmAllCards
Load frmAllCards

If (uCard1 <> 0) Or (uCard2 <> 0) Then

Randomize Timer 'randomizes the computers timer so that a random card can be generated for dealing

'If suit is 1 = spades, 2 = clubs, 3 = hearts, 4 = diamonds
uSuit3 = Int(Rnd * 4) + 1

'ensures that the card delt was not delt before
uCard3 = Int(Rnd * 13) + 1
Do While ((uCard3 = uCard1) And (uSuit3 = uSuit1)) Or ((uCard3 = uCard2) And (uSuit3 = uSuit2)) Or ((uCard3 = cCard1) And (uSuit3 = cSuit1)) Or ((uCard3 = cCard2) And (uSuit3 = cSuit2))
uCard3 = Int(Rnd * 13) + 1
Loop

uTotal = uCard1 + uCard2 + uCard3 + uCard4

If uSuit3 = 1 Then
If uCard3 = 1 Then
imgUserCard3.Picture = frmAllCards.Image1
uCard3 = 11
ElseIf uCard3 = 2 Then
imgUserCard3.Picture = frmAllCards.Image2
uCard3 = 2
ElseIf uCard3 = 3 Then
imgUserCard3.Picture = frmAllCards.Image3
uCard3 = 3
ElseIf uCard3 = 4 Then
imgUserCard3.Picture = frmAllCards.Image4
uCard3 = 4
ElseIf uCard3 = 5 Then
imgUserCard3.Picture = frmAllCards.Image5
uCard3 = 5
ElseIf uCard3 = 6 Then
imgUserCard3.Picture = frmAllCards.Image6
uCard3 = 6
ElseIf uCard3 = 7 Then
imgUserCard3.Picture = frmAllCards.Image7
uCard3 = 7
ElseIf uCard3 = 8 Then
imgUserCard3.Picture = frmAllCards.Image8
uCard3 = 8
ElseIf uCard3 = 9 Then
imgUserCard3.Picture = frmAllCards.Image9
uCard3 = 9
ElseIf uCard3 = 10 Then
imgUserCard3.Picture = frmAllCards.Image10
uCard3 = 10
ElseIf uCard3 = 11 Then
imgUserCard3.Picture = frmAllCards.Image11
uCard3 = 10
ElseIf uCard3 = 12 Then
imgUserCard3.Picture = frmAllCards.Image12
uCard3 = 10
ElseIf uCard3 = 13 Then
imgUserCard3.Picture = frmAllCards.Image13
uCard3 = 10
End If
ElseIf uSuit3 = 2 Then
If uCard3 = 1 Then
imgUserCard3.Picture = frmAllCards.Image14
uCard3 = 11
ElseIf uCard3 = 2 Then
imgUserCard3.Picture = frmAllCards.Image15
uCard3 = 2
ElseIf uCard3 = 3 Then
imgUserCard3.Picture = frmAllCards.Image16
uCard3 = 3
ElseIf uCard3 = 4 Then
imgUserCard3.Picture = frmAllCards.Image17
uCard3 = 4
ElseIf uCard3 = 5 Then
imgUserCard3.Picture = frmAllCards.Image18
uCard3 = 5
ElseIf uCard3 = 6 Then
imgUserCard3.Picture = frmAllCards.Image19
uCard3 = 6
ElseIf uCard3 = 7 Then
imgUserCard3.Picture = frmAllCards.Image20
uCard3 = 7
ElseIf uCard3 = 8 Then
imgUserCard3.Picture = frmAllCards.Image21
uCard3 = 8
ElseIf uCard3 = 9 Then
imgUserCard3.Picture = frmAllCards.Image22
uCard3 = 9
ElseIf uCard3 = 10 Then
imgUserCard3.Picture = frmAllCards.Image23
uCard3 = 10
ElseIf uCard3 = 11 Then
imgUserCard3.Picture = frmAllCards.Image24
uCard3 = 10
ElseIf uCard3 = 12 Then
imgUserCard3.Picture = frmAllCards.Image25
uCard3 = 10
ElseIf uCard3 = 13 Then
imgUserCard3.Picture = frmAllCards.Image26
uCard3 = 10
End If
ElseIf uSuit3 = 3 Then
If uCard3 = 1 Then
imgUserCard3.Picture = frmAllCards.Image27
uCard3 = 11
ElseIf uCard3 = 2 Then
imgUserCard3.Picture = frmAllCards.Image28
uCard3 = 2
ElseIf uCard3 = 3 Then
imgUserCard3.Picture = frmAllCards.Image29
uCard3 = 3
ElseIf uCard3 = 4 Then
imgUserCard3.Picture = frmAllCards.Image30
uCard3 = 4
ElseIf uCard3 = 5 Then
imgUserCard3.Picture = frmAllCards.Image31
uCard3 = 5
ElseIf uCard3 = 6 Then
imgUserCard3.Picture = frmAllCards.Image32
uCard3 = 6
ElseIf uCard3 = 7 Then
imgUserCard3.Picture = frmAllCards.Image33
uCard3 = 7
ElseIf uCard3 = 8 Then
imgUserCard3.Picture = frmAllCards.Image34
uCard3 = 8
ElseIf uCard3 = 9 Then
imgUserCard3.Picture = frmAllCards.Image35
uCard3 = 9
ElseIf uCard3 = 10 Then
imgUserCard3.Picture = frmAllCards.Image36
uCard3 = 10
ElseIf uCard3 = 11 Then
imgUserCard3.Picture = frmAllCards.Image37
uCard3 = 10
ElseIf uCard3 = 12 Then
imgUserCard3.Picture = frmAllCards.Image38
uCard3 = 10
ElseIf uCard3 = 13 Then
imgUserCard3.Picture = frmAllCards.Image39
uCard3 = 10
End If
ElseIf uSuit3 = 4 Then
If uCard3 = 1 Then
imgUserCard3.Picture = frmAllCards.Image40
uCard3 = 11
ElseIf uCard3 = 2 Then
imgUserCard3.Picture = frmAllCards.Image41
uCard3 = 2
ElseIf uCard3 = 3 Then
imgUserCard3.Picture = frmAllCards.Image42
uCard3 = 3
ElseIf uCard3 = 4 Then
imgUserCard3.Picture = frmAllCards.Image43
uCard3 = 4
ElseIf uCard3 = 5 Then
imgUserCard3.Picture = frmAllCards.Image44
uCard3 = 5
ElseIf uCard3 = 6 Then
imgUserCard3.Picture = frmAllCards.Image45
uCard3 = 6
ElseIf uCard3 = 7 Then
imgUserCard3.Picture = frmAllCards.Image46
uCard3 = 7
ElseIf uCard3 = 8 Then
imgUserCard3.Picture = frmAllCards.Image47
uCard3 = 8
ElseIf uCard3 = 9 Then
imgUserCard3.Picture = frmAllCards.Image48
uCard3 = 9
ElseIf uCard3 = 10 Then
imgUserCard3.Picture = frmAllCards.Image49
uCard3 = 10
ElseIf uCard3 = 11 Then
imgUserCard3.Picture = frmAllCards.Image50
uCard3 = 10
ElseIf uCard3 = 12 Then
imgUserCard3.Picture = frmAllCards.Image51
uCard3 = 10
ElseIf uCard3 = 13 Then
imgUserCard3.Picture = frmAllCards.Image52
uCard3 = 10
End If
End If

Else
MsgBox ("You must start a new game first. Click deal to begin.")
End If

End Sub

Private Sub cmdReset_Click()

uCard1 = 0
uCard2 = 0
uCard3 = 0
uCard4 = 0
cCard1 = 0
cCard2 = 0
cCard3 = 0
cCard4 = 0
uSuit1 = 0
uSuit2 = 0
uSuit3 = 0
uSuit4 = 0
cSuit1 = 0
cSuit2 = 0
cSuit3 = 0
cSuit4 = 0
uTotal = 0
cTotal = 0

imgCompCard1.Picture = LoadPicture()
imgCompCard2.Picture = LoadPicture()
imgCompCard3.Picture = LoadPicture()
imgCompCard4.Picture = LoadPicture()

imgUserCard1.Picture = LoadPicture()
imgUserCard2.Picture = LoadPicture()
imgUserCard3.Picture = LoadPicture()
imgUserCard4.Picture = LoadPicture()

cTotal = cCard1 + cCard2 + cCard3 + cCard4
uTotal = uCard1 + uCard2 + uCard3 + uCard4

Cls
Print ("cTotal " & cTotal)
Print ("cCard1 " & cCard1)
Print ("cCard2 " & cCard2)
Print ("cCard3 " & cCard3)
Print ("cCard4 " & cCard4)
Print ""
Print ("uTotal " & uTotal)
Print ("uCard1 " & uCard1)
Print ("uCard2 " & uCard2)
Print ("uCard3 " & uCard3)
Print ("uCard4 " & uCard4)

End Sub

Private Sub cmdStand_Click()

Dim frmAllCards As New frmAllCards
Load frmAllCards

End Sub

lol

lol

The information provided is

The information provided is more apt and good. Really useful.

Loopy

Familiarize yourself with loops and that will cut down your code dramatically.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.