First thing, I did was to see how others have done it. An excellent example can be found ind DaniWeb. Which provided me as the basis for the interface to perform the top 5 prizes.
Since the annual dinner will be held off site, we will not have access to the database that contains the employee info. This is resolved by using xml to hold the information and then using DataTable for data manipulation. With this in mind, I created a class to perform the lucky draw using the random number generator as shown below:
The above class can then be used to select the lucky record and can be used either for the table draw or the individual employee draw.
' Common Library to be used by the Project
' Date Feb 29, 2008
' Revised
Imports System.Data.OleDb
Public Class clsDrawLib
Dim _dTab As New DataTable
Dim _dDraw As New DataTable
Dim conn As OleDbConnection
Dim _lastDraw As DataRow
Public Property dataTab()
Get
Return _dTab
End Get
Set(ByVal Value)
_dTab = Value
End Set
End Property
Public Property dataDraw()
Get
Return _dDraw
End Get
Set(ByVal Value)
_dDraw = Value
End Set
End Property
Public ReadOnly Property lastDraw()
Get
Return _lastDraw
End Get
End Property
Public Sub qryData(ByRef lbMsg As ListBox)
conn = New OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings("devdb"))
popListMsg(lbMsg, Now & " - In dbclick")
Try
conn.Open()
popListMsg(lbMsg, Now & " - Connection successful")
Dim dbCmd As OleDbCommand = conn.CreateCommand
Dim sql As String = "select eno, name from empmast where eno like '16%'"
dbCmd.CommandText = sql
Dim dbAdapt As New OleDbDataAdapter
dbAdapt.SelectCommand = dbCmd
dbAdapt.Fill(_dTab)
addRsltCol()
popListMsg(lbMsg, Now & " - Data bound")
Catch ex As Exception
popListMsg(lbMsg, Now & " - Error: " & ex.Message)
Finally
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Try
End Sub
Private Sub addRsltCol()
Dim dRow As DataRow = _dTab.NewRow
Dim dCol, dCol2 As DataColumn
_dDraw.Columns.Add("PrizeNum", System.Type.GetType("System.Int32"))
For Each dCol In _dTab.Columns
_dDraw.Columns.Add(dCol.ColumnName, dCol.DataType)
Next
End Sub
Public Sub drawRslt(ByRef lbMsg As ListBox, ByVal pGiftNum As Integer)
Dim colCnt As Integer
Dim giftRow, drawnRow As DataRow
Dim dCol As DataColumn
Dim drawFlg As Boolean
If _dTab.Rows.Count = 0 Then
popListMsg(lbMsg, "Can't proceed - no data available")
Else
'giftNum = CInt(pGiftNum)
'lbmsg.Items.Add("Gift number is " & giftNum)
drawnRow = _dTab.NewRow
drawFlg = clsDraw.clsDraw.drawRec(_dTab, drawnRow)
giftRow = _dDraw.NewRow
giftRow.Item(0) = pGiftNum
colCnt = 0
Do While colCnt < _dTab.Columns.Count
giftRow.Item(colCnt + 1) = drawnRow.Item(colCnt)
colCnt += 1
Loop
_lastDraw = giftRow
_dDraw.Rows.Add(giftRow)
'dgRslt.DataSource = dDraw
End If
End Sub
Sub popListMsg(ByRef lbMsg As ListBox, ByVal msg As String)
If (lbMsg Is Nothing) = False Then
lbMsg.Items.Add(msg)
End If
End Sub
End Class
Additional things added for the interface of the lottery is as follows
The following sample queries from a database but in the actual windows application, it will obtain the data from an XML file.
I then obtain the DataRow of the lucky number from the clsDraw class shown above and then use multiple threads to simulate the drawing and then start populating the number one at a time until all the numbers of the employee number is displayed.
There is one thread which will in turn generate six threads (one for each number assuming the employee number is six digit. I keep track of the threads in an Arraylist and join to the thread so that it won't continue until the thread is complete.
The main thread will be in charge of going through each of the numbers and ensuring that the remaining digits continue to spin until all the numbers are displayed. The other threads are the ones that actually perform the number movement.
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
Dim empName As String
If Microsoft.VisualBasic.IsNumeric(tbNum.Text) = True Then
lblName.Text = "drawn"
If CType(_drawObj.dataTab, DataTable).Rows.Count <= 0 Then
_drawObj.qryData(Nothing)
End If
_drawObj.drawRslt(Nothing, tbNum.Text)
'displayRnd()
drawnRow = _drawObj.lastDraw
eno = drawnRow.Item(1)
empName = drawnRow.Item(2)
lblName.Text += vbNewLine & eno
displayENO2(eno)
lblName.Text = empName
Else
lblName.Text = "No draw"
End If
End Sub
==================
Private Sub displayENO2(ByVal eno As String)
Dim tmpStr, tmpStr2, ctrlName As String
Dim i, loc, loc2 As Integer
Dim thrd As System.Threading.Thread
aList.Clear()
For i = 1 To 6
tmpStr = "txtN" & i
aList.Add(tmpStr)
Next
loc = 6 - eno.Length
tmpStr = eno
For i = 0 To eno.Length - 1
'rndDispList(aList)
thrd = New System.Threading.Thread(AddressOf rndDispList)
thrd.Start()
loc2 = loc - 1
tmpStr2 = Microsoft.VisualBasic.Right(tmpStr, 1)
tmpStr = Microsoft.VisualBasic.Left(tmpStr, tmpStr.Length - 1)
ctrlName = "txtN" & (6 - i)
thrd.Join()
setTxtBoxVal(ctrlName, tmpStr2)
aList.Remove(ctrlName)
Next
End Sub
Sub rndDispN1()
rndDisp("txtN1")
End Sub
Sub rndDispN2()
rndDisp("txtN2")
End Sub
Sub rndDispN3()
rndDisp("txtN3")
End Sub
Sub rndDispN4()
rndDisp("txtN4")
End Sub
Sub rndDispN5()
rndDisp("txtN5")
End Sub
Sub rndDispN6()
rndDisp("txtN6")
End Sub
Private Sub rndDispList()
Dim ctrlName As String
Dim thrd As System.Threading.Thread
Dim tList As New ArrayList
For Each ctrlName In aList
Select Case ctrlName
Case "txtN1"
thrd = New System.Threading.Thread(AddressOf rndDispN1)
thrd.Start()
tList.Add(thrd)
Case "txtN2"
thrd = New System.Threading.Thread(AddressOf rndDispN2)
thrd.Start()
tList.Add(thrd)
Case "txtN3"
thrd = New System.Threading.Thread(AddressOf rndDispN3)
thrd.Start()
tList.Add(thrd)
Case "txtN4"
thrd = New System.Threading.Thread(AddressOf rndDispN4)
thrd.Start()
tList.Add(thrd)
Case "txtN5"
thrd = New System.Threading.Thread(AddressOf rndDispN5)
thrd.Start()
tList.Add(thrd)
Case "txtN6"
thrd = New System.Threading.Thread(AddressOf rndDispN6)
thrd.Start()
tList.Add(thrd)
Case Else
End Select
Next
For Each thrd In tList
thrd.Join()
Next
End Sub
No comments:
Post a Comment