|
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form frmBin
Caption = "Binary Operations"
ClientHeight = 3180
ClientLeft = 60
ClientTop = 345
ClientWidth = 7305
LinkTopic = "Form1"
ScaleHeight = 3180
ScaleWidth = 7305
StartUpPosition = 3 'Windows Default
Begin MSComctlLib.StatusBar stbStatus
Align = 2 'Align Bottom
Height = 375
Left = 0
TabIndex = 13
Top = 2805
Width = 7305
_ExtentX = 12885
_ExtentY = 661
Style = 1
_Version = 393216
BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628}
NumPanels = 1
BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628}
EndProperty
EndProperty
End
Begin VB.CommandButton cmdClose
Caption = "&Close"
Height = 495
Left = 6000
TabIndex = 5
Top = 2160
Width = 1215
End
Begin VB.CommandButton cmdProcess
Caption = "&Process"
Default = -1 'True
Height = 495
Left = 6000
TabIndex = 4
Top = 1440
Width = 1215
End
Begin VB.TextBox txtResult
Height = 315
Left = 1920
Locked = -1 'True
TabIndex = 3
Top = 2280
Width = 1095
End
Begin VB.TextBox txtNum2
Height = 315
Left = 1920
MaxLength = 7
TabIndex = 1
Top = 920
Width = 1095
End
Begin VB.ComboBox cboOp
Height = 315
Left = 1920
Style = 2 'Dropdown List
TabIndex = 2
Top = 1605
Width = 1095
End
Begin VB.TextBox txtNum1
Height = 315
Left = 1920
MaxLength = 7
TabIndex = 0
Top = 240
Width = 1095
End
Begin VB.Label lblBinResult
Height = 315
Left = 3360
TabIndex = 12
Top = 2280
Width = 2415
End
Begin VB.Label lblBinNum2
Height = 315
Left = 3360
TabIndex = 11
Top = 960
Width = 2415
End
Begin VB.Label lblBinNum1
Height = 315
Left = 3360
TabIndex = 10
Top = 240
Width = 2415
End
Begin VB.Label lblResult
Caption = "Result:"
Height = 315
Left = 240
TabIndex = 9
Top = 2280
Width = 1215
End
Begin VB.Label lblOp
Caption = "Operation:"
Height = 315
Left = 240
TabIndex = 8
Top = 1600
Width = 1215
End
Begin VB.Label lblNum2
Caption = "Number 2:"
Height = 315
Left = 240
TabIndex = 7
Top = 920
Width = 1215
End
Begin VB.Label lblNum1
Caption = "Number 1:"
Height = 315
Left = 240
TabIndex = 6
Top = 240
Width = 1215
End
End
Attribute VB_Name = "frmBin"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Const DEPTH% = 24
Private Const LIMIT& = 8388608
' Store reference to Bin class
Private mBin As Bin
' Store value of numbers
Private mNum1&, mNum2&
' Store valid status of a number
Private mNum1Valid As Boolean
Private mNum2Valid As Boolean
Private Sub Form_Load()
' Populate drop-down
With cboOp
.AddItem "AND"
.AddItem "OR"
.AddItem "XOR"
' Select the first value
.ListIndex = 0
End With
' Get a reference to Bin library
Set mBin = New Bin
' Set boolean valid number status
mNum1Valid = True
mNum2Valid = True
' Set status bar
stbStatus.SimpleText = "Ready!"
End Sub
' PROCESS OPERATION
Private Sub cmdProcess_Click()
Dim decResult&
Dim binResult$
' When both numbers are valid ...
If CheckNumbers() Then
' Perform selected operation
Select Case cboOp.ListIndex
Case 0
decResult = mNum1 And mNum2
Case 1
decResult = mNum1 Or mNum2
Case 2
decResult = mNum1 Xor mNum2
End Select
' Get Bit pattern
binResult = mBin.DecToBin(decResult, DEPTH)
' Set decimal and binary result
txtResult.Text = CStr(decResult)
lblBinResult.Caption = binResult
stbStatus.SimpleText = "Success!"
Else
Call Reset
End If
End Sub
Private Sub txtNum1_Change()
Call ProcessNumber( _
txtNum1, _
lblBinNum1, _
mNum1, _
mNum1Valid _
)
End Sub
Private Sub txtNum2_Change()
Call ProcessNumber( _
txtNum2, _
lblBinNum2, _
mNum2, _
mNum2Valid _
)
End Sub
' MAKE SURE BOTH NUMBERS ARE VALID
Private Function CheckNumbers() As Boolean
CheckNumbers = True
' One invalid number makes the operation invalid
If mNum1Valid = False Or mNum2Valid = False Then
CheckNumbers = False
End If
End Function
' RESET FIELDS FOR NEXT OPERATION
Private Sub Reset()
stbStatus.SimpleText = "Ready!"
txtResult.Text = ""
lblBinResult.Caption = ""
mNum1Valid = True
mNum2Valid = True
End Sub
' GENERIC HANDLER FOR PROCESSING A NUMBER
Private Sub ProcessNumber( _
txtNum As TextBox, _
lblBin As Label, _
num&, _
valid As Boolean _
)
On Error GoTo error1
' Reset prior results
Call Reset
' Get Number
num = CLng(txtNum.Text)
If num > LIMIT Then GoTo error1
' Set bit pattern for number
lblBin.Caption = mBin.DecToBin(CLng(txtNum.Text), DEPTH)
Exit Sub
error1:
' Invalidate number
If txtNum.Text = "" Then
lblBin.Caption = ""
Else
lblBin.Caption = "INVALID"
End If
valid = False
End Sub
|