Tuesday, March 6, 2007

Today's Code 14:46 2007-3-6

Public Class Form1
Private m_sAllRecords As String Private m_sCorrectDefinition As String Private m_sSelectCorrectDefinition As String Private m_aAllRecordsArray Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load m_sAllRecords = GetAllRecords() m_aAllRecordsArray = Split(m_sAllRecords, vbNewLine) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Static lIndex As Long Dim aFields
If m_sSelectCorrectDefinition <> m_sCorrectDefinition Then MsgBox("wrong") Exit Sub End If

If lIndex > UBound(m_aAllRecordsArray) Then lIndex = 0 End If

aFields = Split(GetRecord(lIndex), "") lIndex = lIndex + 1
If UBound(aFields) > 0 Then lblDef1.Text = aFields(0) m_sCorrectDefinition = aFields(1)
RadioButton1.Text = GetNewRandomDefinition() RadioButton2.Text = GetNewRandomDefinition() RadioButton3.Text = GetNewRandomDefinition() RadioButton4.Text = GetNewRandomDefinition() RadioButton5.Text = GetNewRandomDefinition()
WriteCorrectDefToRandomRadioButton()
RadioButton1.Checked = False RadioButton2.Checked = False RadioButton3.Checked = False RadioButton4.Checked = False RadioButton5.Checked = False

End If End Sub Private Sub WriteCorrectDefToRandomRadioButton() Dim i As Integer
i = CInt(GetRandomNumber(1, 5)) If Controls(i).Name Like "RadioButton*" Then Controls(i).Text = m_sCorrectDefinition Else WriteCorrectDefToRandomRadioButton() End If 'For i = 0 To Me.Controls.Count - 1 ' If Controls(i).Name Like "RadioButton*" Then ' Controls(i).Text = m_sCorrectDefinition ' End If
'Next End Sub Private Function GetNewRandomDefinition() As String Dim i As Integer Dim sRandomDefinition As String sRandomDefinition = "" sRandomDefinition = GetRandomDefinition() For i = 0 To Me.Controls.Count - 1 'Application.DoEvents() 'debug for duplicates If Controls(i).Name Like "RadioButton*" Then If sRandomDefinition = Me.Controls(i).Text Then GetNewRandomDefinition End If End If Next Return sRandomDefinition
End Function Private Function GetRandomDefinition() As String Dim lIndexDefinition As Long Dim aFields
lIndexDefinition = GetRandomNumber(LBound(m_aAllRecordsArray), UBound(m_aAllRecordsArray)) aFields = Split(GetRecord(lIndexDefinition), "") Return aFields(1) End Function Private Function GetRecord(ByVal lIndex As Long) As String If lIndex <= UBound(m_aAllRecordsArray) Then Return m_aAllRecordsArray(lIndex) Else Return "" End If End Function Private Function GetAllRecords() As String Return (System.IO.File.OpenText(Application.StartupPath & "\data1.txt").ReadToEnd) End Function
Private Function GetRandomNumber(ByVal lLowerBound As Long, ByVal lUpperBound As Long) As Long Dim RandomValue As Long
RandomValue = CInt(Int((lUpperBound - lLowerBound + 1) * Rnd() + lLowerBound))
Return RandomValue End Function
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged m_sSelectCorrectDefinition = RadioButton1.Text End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged m_sSelectCorrectDefinition = RadioButton2.Text End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged m_sSelectCorrectDefinition = RadioButton3.Text End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged m_sSelectCorrectDefinition = RadioButton4.Text End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged m_sSelectCorrectDefinition = RadioButton5.Text End SubEnd Class

No comments: