|
Otázky a odpovědi, pro potřebu nějakého testu, kvízu apod. Zajímavé natolik, že se pokouším záměr rozvinout a přispět vlastním nápadem. Zohledněna možnost vybrat jen jednu správnou odpověď pro danou otázku nebo více správných odpovědí a místo počtu správných otázek sčítat body za otázku a nakonec provést vyhodnocení. Nutno ještě nápad rozvinout... Součet vypočítaného sloupce datatable pomocí Compute. Do kódu form1 vložit:
Public Class Form1
Dim dt1 As DataTable
Dim dt2 As DataTable
Dim WithEvents dgv1 As DataGridView
Dim WithEvents dgv2 As DataGridView
Dim bs As BindingSource
Dim ss As StatusBar
Dim b1 As Boolean
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
Me.Text = "QuestionsAndAnswers"
dt1 = New DataTable
With dt1
.Columns.Add("Číslo", Type.GetType("System.Int32"))
.Columns.Add("Otázka", Type.GetType("System.String"))
.Rows.Add(1, "Co je to ?")
.Rows.Add(2, "Co je to ?")
.Rows.Add(3, "Co je to ?")
End With
dt2 = New DataTable
With dt2
.Columns.Add("Číslo", Type.GetType("System.Int32"))
.Columns.Add("Varianta", Type.GetType("System.String"))
.Columns.Add("Odpověď", Type.GetType("System.String"))
.Columns.Add("Vyber", Type.GetType("System.Boolean"))
.Columns.Add("Body", Type.GetType("System.Int32"))
.Columns.Add("Zisk", Type.GetType("System.Int32"), "IIf(Vyber,Body,0)")
.Rows.Add(1, "a", "áčko", 0, 1)
.Rows.Add(1, "b", "béčko", 0, 2)
.Rows.Add(2, "a", "áčko", 0, 1)
.Rows.Add(2, "b", "béčko", 0, 2)
.Rows.Add(2, "c", "céčko", 0, 3)
.Rows.Add(3, "a", "áčko", 0, 1)
.Rows.Add(3, "b", "béčko", 0, 2)
.Rows.Add(3, "c", "céčko", 0, 3)
.Rows.Add(3, "d", "déčko", 0, 4)
End With
bs = New BindingSource
bs.DataSource = dt2
dgv1 = New DataGridView
With dgv1
.DataSource = dt1
.Dock = DockStyle.Fill
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.AllowUserToAddRows = False
.ReadOnly = True
End With
dgv2 = New DataGridView
With dgv2
.Dock = DockStyle.Fill
.MultiSelect = False
.AllowUserToAddRows = False
.DataSource = bs
For i As Integer = 0 To .Columns.Count - 1
If i <> 3 Then .Columns(i).ReadOnly = True
Next
End With
Dim sc As SplitContainer = New SplitContainer
With sc
.Panel1.Controls.Add(dgv1)
.Panel2.Controls.Add(dgv2)
.Dock = DockStyle.Fill
End With
Me.Controls.Add(sc)
ss = New StatusBar
ss.Dock = DockStyle.Bottom
Me.Controls.Add(ss)
Dim ms As MenuStrip = New MenuStrip
Dim tsmi As ToolStripMenuItem = New ToolStripMenuItem
Dim tsmi1 As ToolStripMenuItem = New ToolStripMenuItem
Dim tsmi2 As ToolStripMenuItem = New ToolStripMenuItem
Dim tsmi3 As ToolStripMenuItem = New ToolStripMenuItem
With tsmi
.Text = "Menu"
.DropDownItems.AddRange(New ToolStripMenuItem() {tsmi1, tsmi2, tsmi3})
End With
With tsmi1
.Text = "One answer per question"
.CheckOnClick = True
.Tag = 1
AddHandler .Click, New EventHandler(AddressOf tsmi_Click)
End With
With tsmi2
.Text = "Total points"
.Tag = 2
AddHandler .Click, New EventHandler(AddressOf tsmi_Click)
End With
With tsmi3
.Text = "User View"
.CheckOnClick = True
.Tag = 3
AddHandler .Click, New EventHandler(AddressOf tsmi_Click)
End With
With ms
.Dock = DockStyle.Top
.Items.Add(tsmi)
End With
Me.Controls.Add(ms)
End Sub
Private Sub dgv1_SelectionChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.SelectionChanged
Dim flt As String = "Číslo=" & dgv1.Rows(dgv1.CurrentRow.Index).Cells(0).Value.ToString
If bs.Filter = flt Then Exit Sub
bs.Filter = flt
dgv1.AutoResizeColumns()
dgv2.AutoResizeColumns()
End Sub
Private Sub dgv2_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv2.CurrentCellDirtyStateChanged
If dgv2.IsCurrentCellDirty And dgv2.CurrentCell.ColumnIndex = 3 Then
If b1 Then
For i As Integer = 0 To dgv2.Rows.Count - 1
If dgv2.Rows(i).Cells(3).Value = True And i <> dgv2.CurrentRow.Index Then
dgv2.Rows(i).Cells(3).Value = False
End If
Next
End If
dgv2.CommitEdit(DataGridViewDataErrorContexts.Commit)
dt2.AcceptChanges()
ss.Text = IIf(b1, "Correct answers : ", "Total points : ") & dt2.Compute("Sum(Zisk)", "")
End If
End Sub
Private Sub tsmi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim tsmi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Select Case CInt(tsmi.Tag)
Case 1
Dim tsmi1 As ToolStripMenuItem = tsmi.OwnerItem
b1 = tsmi.Checked
tsmi1.DropDownItems(1).Text = IIf(b1, "Correct answers", "Total points")
ss.Text = vbNullString
For i As Integer = 0 To dt1.Rows.Count - 1
Dim b As Integer = 0
For j As Integer = 0 To dt2.Rows.Count - 1
If dt1.Rows(i).Item(0) = dt2.Rows(j).Item(0) Then
b = b + 1
dt2.Rows(j).Item(3) = False
If b1 Then
dt2.Rows(j).Item(4) = IIf(b = 1, 1, 0)
Else
dt2.Rows(j).Item(4) = b
End If
End If
Next
Next
dt2.AcceptChanges()
Case 2
Dim msg As String = tsmi.Text & " : " & dt2.Compute("Sum(Zisk)", "")
If b1 Then
If dt1.Rows.Count = dt2.Compute("Sum(Body)", "") Then
msg = msg & vbCrLf & "Vše správně." & vbCrLf & "Congratulations!"
End If
Else
If dt2.Compute("Sum(Zisk)", "") = dt2.Compute("Sum(Body)", "") Then
msg = msg & vbCrLf & "Plný počet bodů." & vbCrLf & "Congratulations!"
End If
End If
MsgBox(msg, MsgBoxStyle.Information, Me.Text)
Case 3
With dgv2
For k As Integer = 0 To .Columns.Count - 1
Select Case k
Case 0, 4, 5
.Columns(k).Visible = Not tsmi.Checked
End Select
Next
End With
End Select
End Sub
End Class
Mnoho zdaru!
|