Dears,
I'm looking for assistance in visual basic with respect to multiple choices questions (MCQ).
by using visual basic for Visual Studio 2015
apply the codes without using database:
1- how to Not make any duplications in the questions?(for memorizing purpose... what is wrong and what is right?) E.g. Assuming I open the program and the first question is the word “rich” ,and I chose the correct answer, which is “IT”, I don’t want to see “rich” again until I finish with the whole list. However, if I make the wrong choice for “rich” for anything else e.g. “HR”, I want the word “rich” to appear after a while until I get the question correct. The point here to make the person memorize “rich” is “IT”.
Please write the codes down in your comment (the point you answering)
sorry for asking long question
Thank you
Public Class Form1
Private Structure questionsNanswers
Public Q As String
Public A As String
Public QT As Integer
Public QC As Integer
End Structure
Private wstart As Integer = 0
Private adad As Integer = 10
Private QA(9999) As questionsNanswers
Private word(15) As String
Private aray(15) As Integer
Private Sub RandomizeArray(a As Integer, ByRef array() As Integer)
Dim i As Integer
Dim j As Integer
Dim tmp As Integer
Randomize()
For i = 0 To a - 1
j = Int((6 - i + 1) * Rnd() + i)
tmp = array(i)
array(i) = array(j)
array(j) = tmp
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' next
CheckEntry()
wstart = wstart + 1
If wstart >= adad Then
wstart = 0
End If
WriteText()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' previous
CheckEntry()
wstart = wstart - 1
If wstart < 0 Then
wstart = adad - 1
End If
WriteText()
End Sub
Private Sub CheckEntry()
RadioButton1.Visible = True
RadioButton2.Visible = True
RadioButton3.Visible = True
RadioButton4.Visible = True
RadioButton1.ForeColor = Color.Black
RadioButton2.ForeColor = Color.Black
RadioButton3.ForeColor = Color.Black
RadioButton4.ForeColor = Color.Black
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
End Sub
Private Sub WriteText()
Dim out As Boolean = False
For kk = 0 To 6
aray(kk) = kk
Next
RandomizeArray(7, aray)
Do Until out
For j = 0 To 3
If out = False Then
If aray(j) = QA(wstart).QT Then
out = True
Exit Do
End If
End If
Next
For kkk = 0 To 6
aray(kkk) = kkk
Next
RandomizeArray(7, aray)
Loop
RadioButton1.Text = word(aray(0))
RadioButton2.Text = word(aray(1))
RadioButton3.Text = word(aray(2))
RadioButton4.Text = word(aray(3))
Label1.Text = CStr(wstart + 1) & ") " & QA(wstart).Q
' ==============================
Dim go As Boolean = False
If go Then
Dim msg As String
For ll = 0 To 6
msg = msg + CStr(aray(ll)) + "|"
Next
MsgBox(msg)
End If
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
word(0) = "TA"
word(1) = "GR"
word(2) = "HR"
word(3) = "FIN"
word(4) = "commercial"
word(5) = "Proc"
word(6) = "IT"
QA(0).Q = "rich"
QA(0).A = word(6)
QA(0).QT = 6
QA(0).QC = -1
QA(1).Q = "Tal"
QA(1).A = word(1)
QA(1).QT = 1
QA(1).QC = -1
QA(2).Q = "sau"
QA(2).A = word(2)
QA(2).QT = 2
QA(2).QC = -1
QA(3).Q = "pat"
QA(3).A = word(3)
QA(3).QT = 3
QA(3).QC = -1
QA(4).Q = "del"
QA(4).A = word(5)
QA(4).QT = 5
QA(4).QC = -1
WriteText()
End Sub
End Class
Procedures :
1•Store the questions in a database(E.g. MySql/MSSQL/EXCEL)
2•In the database, create a table of 6 columns(column 1 for questions,column 2 for mcq option1,column 3 for mcq option 2, column 4 for mcq option 3,column 5 for mcq option 4 and finally column 6 for mcq answer )
3•Add 1 label and 4 Radio buttons
Now all you have to do is follow this and use the code..
That'll do the work
Related
I have this code in VB6:
Private Sub t_Change(Index As Integer)
Iznos = 0
For i = 1 To 4
Iznos = Iznos + t(i).Text
Next
If CDbl(Iznos) > Label13.Caption Then
t(Index).Text = 0
Iznos = 0
t(Index).SelStart = 0
t(Index).SelLength = 1
For i = 1 To 4
Iznos = Iznos + t(i).Text
Next
End If
t(0).Text = Format(Label13.Caption - CDbl(Iznos), "#,##0.00")
End Sub
This code works for my textboxes (4 of them named: t(0),t(1),t(2),t(3)).
I am trying to achieve the same with VB.net but i can't seem to wrap my head around it.
This is my code in VB.Net:
Public Function izracunaj(ByVal s As Object)
Dim ukupniIznos As Decimal = Decimal.Parse(maticnavalutatbox.Text)
Dim poslaniIznos As Decimal = Decimal.Parse(s.text)
Dim iznos As Decimal
Dim allTextboxesPayment() As TextBox = {gotovinaTbox, karticeTbox, ostaloTbox, ziralnoTbox}
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
If CDbl(iznos) > ukupniIznos Then
s.Text = 0
iznos = 0
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
End If
gotovinaTbox.Text = Format(ukupniIznos - CDbl(iznos), "#,##0.00")
End Function
But if I call my function like this:
Private Sub ziralnoTbox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ziralnoTbox.TextChanged
izracunaj(sender)
End Sub
It does not work because my izracunaj function already has the input value of ziralnoTbox. How can i achieve same functionality with vb.net as in vb6
If I'm looking at this correctly, you could make use of the TextChanged event.
Private Sub TextBox1_TextChanged(sender As Object,
e As EventArgs) Handles gotovinaTbox.TextChanged,
karticeTbox.TextChanged,
ostaloTbox.TextChanged,
ziralnoTbox.TextChanged
Dim ukupniIznos As Decimal = Decimal.Parse(maticnavalutatbox.Text)
Dim poslaniIznos As Decimal = Decimal.Parse(s.text)
Dim iznos As Decimal
Dim allTextboxesPayment() As TextBox = {gotovinaTbox, karticeTbox, ostaloTbox, ziralnoTbox}
'gotovinaTbox, quita esto?
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
If CDbl(iznos) > ukupniIznos Then
s.Text = 0
iznos = 0
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
End If
gotovinaTbox.Text = Format(ukupniIznos - CDbl(iznos), "#,##0.00")
End Sub
Today i continue my work, Building a menu with a vb.net console application. I found more samples to build with Windows forms. Still i try to get Basic Knowledge with the console surface.I was not able to put the following marquee text in a scroll menu, the second Code past the marquee text.
Module Module1
Dim aTimer As New System.Timers.Timer
Const marqueeText As String = "The quick brown fox... "
Dim sb As New System.Text.StringBuilder
Dim direction As Boolean = False
Sub Main()
aTimer.AutoReset = True
aTimer.Interval = 100 '1/10 second
AddHandler aTimer.Elapsed, AddressOf tick
aTimer.Start()
Console.ReadKey()
End Sub
Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
Console.Clear()
If sb.Length = 0 Then sb.Append(marqueeText)
If direction Then
sb.Insert(0, sb(sb.Length - 1))
sb.Remove(sb.Length - 1, 1)
Else
sb.Append(sb(0))
sb.Remove(0, 1)
End If
Console.CursorLeft = 10 'no visible change
Console.CursorTop = 10 'visible change
Console.Write("{0}{1}", vbCr, sb.ToString)
End Sub
End Module
The marquee text Output from above is not easy to manage with the console.cursorleft command. I have no clue how to move it to the right or to put the marquee Output in the following Code, a scroll menu, on the third line.
Module Module1
Dim MenuList As New List(Of String)
Sub PrintMenu(highlight As Integer, left As Integer, top As Integer)
Dim Nickvektor() As Integer = {1, 2, 3, 4, 5}
For I = 0 To MenuList.Count - 1
Console.CursorLeft = left
Console.CursorTop = top + I
If I = highlight Then
Console.Write("{0}", "[" & Nickvektor(I) & "]")
Else
Console.Write(MenuList(I))
End If
Next
End Sub
Sub Main()
Console.CursorVisible = False
Dim x As Integer = 0
Dim Nickvektor() As String = {" "}
For counter As Integer = 0 To 0
Do
For Each s In Nickvektor
MenuList.Add(s)
Next
x += 1
Loop Until x = 5
Next
Console.SetCursorPosition(10, 16)
Console.Write("[ ]")
Dim CurrentItem As Integer = 0
Dim CurrentKey As ConsoleKey
While CurrentKey <> ConsoleKey.Enter
PrintMenu(CurrentItem, 10, 10)
CurrentKey = Console.ReadKey(True).Key
Select Case CurrentKey
Case ConsoleKey.DownArrow
CurrentItem += 1
Case ConsoleKey.UpArrow
CurrentItem -= 1
End Select
CurrentItem = (CurrentItem + MenuList.Count) Mod MenuList.Count
End While
End Sub
End Module
The menu Frame for the above Code can be used with the up and down arrows on the Keyboard.
Maybe it is to much work but i have no clue how to continue.
The first Solution for the marquee Output is an easy change of the original code. The wrap, vbCr, was the main Problem to move the text output toward the right edge oft he screen. The following code can be used to change the cursorTop Positon and also the cursorLeft Position of the Text.
Console.CursorVisible = False
Console.CursorLeft = 30
Console.CursorTop = 10
Console.Write("{0}", sb.ToString)
The heavy part are the Menu code Lines. To answer my own question some additional help was necessary.
I posted my question on the MS developer Network written in german language. With the following link it can be viewed.
For the case the link should be broken or other cases i post the code on this site.
Module Module1
Dim MenuList As New List(Of String)
Dim aTimer As New System.Timers.Timer
Const marqueeText As String = "The quick brown fox... "
Dim sb As New System.Text.StringBuilder
Dim direction As Boolean = False
Sub PrintMenu(highlight As Integer, left As Integer, top As Integer)
Dim Nickvektor() As Integer = {1, 2, 3, 4, 5}
For I = 0 To MenuList.Count - 1
Console.CursorLeft = left
Console.CursorTop = top + I
If I = highlight Then
Console.Write("{0}", "[" & Nickvektor(I) & "]")
Else
Console.Write(MenuList(I))
End If
Next
End Sub
Sub Main()
Console.CursorVisible = False
aTimer.AutoReset = True
aTimer.Interval = 100 '1/10 second
AddHandler aTimer.Elapsed, AddressOf tick
Dim x As Integer = 0
Dim Nickvektor() As String = {" "}
For counter As Integer = 0 To 0
Do
For Each s In Nickvektor
MenuList.Add(s)
Next
x += 1
Loop Until x = 5
Next
Console.SetCursorPosition(10, 16)
Console.Write("[ ]")
Dim CurrentItem As Integer = 0
Dim CurrentKey As ConsoleKey
While CurrentKey <> ConsoleKey.Enter
If CurrentItem = 2 Then ' Zero can be used to show the marquee output prompt
aTimer.Start() ' With a change to two or four the timer can be stoped:
'Else
'If aTimer.Enabled Then
' aTimer.Stop()
'End If
End If
PrintMenu(CurrentItem, 10, 10)
CurrentKey = Console.ReadKey(True).Key
Select Case CurrentKey
Case ConsoleKey.DownArrow
CurrentItem += 1
Case ConsoleKey.UpArrow
CurrentItem -= 1
End Select
CurrentItem = (CurrentItem + MenuList.Count) Mod MenuList.Count
End While
End Sub
Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
If sb.Length = 0 Then sb.Append(marqueeText)
If direction Then
sb.Insert(0, sb(sb.Length - 1))
sb.Remove(sb.Length - 1, 1)
Else
sb.Append(sb(0))
sb.Remove(0, 1)
End If
Console.CursorVisible = False
Console.CursorLeft = 20
Console.CursorTop = 12 ' For the first Element CursorTop=10, fort he third 12
Console.Write("{0}", sb.ToString)
End Sub
End Module
To learn an other language like English i have to search a lot. Visual Basic Code is mostly written with English key words for the commands. I think it is easier to look up the maintainable changes for your self. To search is not every day funny.
I'm developing a program that asks the user to input 5 numbers. The lowest of the 5 numbers will be dropped, and then the other 4 numbers are to be averaged.
I'm quite new to VB, but I believe I'm currently on the right path here...
I've sorted the array to help identify the lowest number, but I do not know how to exclude the lowest number and to then average the other remaining 4.
Here is my code so far:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IntArr(4) As Integer
Dim i As Integer
For i = 1 To 5
IntArr(4) = InputBox("Enter Number" & i)
Next
Array.Sort(IntArr)
'textbox1.text = ???
End Sub
End Class
Can anyone please assist or at least point me in the right direction?
In keeping with the spirit of your code, something like the following would work.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IntArr(4) As Integer
Dim OutArr(3) As Integer
For i = 0 To 4
IntArr(i) = InputBox("Enter Number " & i)
Next
Array.Sort(IntArr)
Array.Copy(IntArr, 1, OutArr, 0, 4) 'exclude the lowest number
TextBox1.Text = OutArr.Average()
End Sub
End Class
Using built in LINQ functions
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim numberOfItems = 5
Dim numberOfItemsToRemove = 1
Dim inArray(numberOfItems - 1) As Integer
Dim outArray(numberOfItems - 1 - numberOfItemsToRemove) As Integer
Dim i As Integer
For i = 0 To numberOfItems - 1
While Not Integer.TryParse(InputBox("Enter Number " & i + 1), inArray(i))
MessageBox.Show("Invalid input!")
End While
Next
outArray = inArray.OrderBy(Function(j) j).Skip(numberOfItemsToRemove).ToArray()
MessageBox.Show(
String.Format(
"Input: [{0}], Output: [{1}], average: {2:0.0}",
String.Join(", ", inArray),
String.Join(", ", outArray),
outArray.Average))
End Sub
Your code as it is above will continue to simply change the value of index 4 with each box if I'm not mistaken. I would do something like this (I will use your variable names for your convenience).
Button1_Click(procedure junk that is auto-inserted)
Dim intArr(4) As Integer
Dim OutArr(3) As Integer
Dim intCounter, intAverage, intLowest, intLowIndex As Integer
'populate all indexes of intArr()
For intCounter = 0 to 4
intArr(intCounter) = InputBox("Please enter a number.")
Next intCounter
intCounter = 1 'reset counter for check
intLowest = intArr(intLowIndex) 'start with index 0
'find lowest number and its index
For intCounter = 1 to 4
If intLowest > intArr(intCounter) Then 'defaults to previous
intLowest = intArr(intCounter)
intLowIndex = intCounter
End If
Next intCounter
intCounter = 0 'reset counter again for possible For...Next loops
Select Case intLowIndex
Case = 0
For intCounter = 0 to 3
OutArr(intCounter) = intArr(intCounter + 1)
Next intCounter
Case = 1
OutArr(0) = intArr(0)
OutArr(1) = intArr(2)
OutArr(2) = intArr(3)
OutArr(3) = intArr(4)
Case = 2
OutArr(0) = intArr(0)
OutArr(1) = intArr(1)
OutArr(2) = intArr(3)
OutArr(3) = intArr(4)
Case = 3
OutArr(0) = intArr(0)
OutArr(1) = intArr(1)
OutArr(2) = intArr(2)
OutArr(3) = intArr(4)
Case = 4
For intCounter = 0 to 3
OutArr(intCounter) = intArr(intCounter)
Next intCounter
End Select
intAverage = (OutArr(0) + OutArr(1) + OutArr(2) + OutArr(3)) / 4
'insert your preferred method to display OutArr() and intAverage
16*16 matrix is coming to my screen when I start the program. But when I click diagonal button, diagonal line isn't red. That does not change.
my code :
Public Class Form1
Dim etk As New Label 'i define the matrix as etk
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To 15
For j = 0 To 15
Dim etk As New Label
Me.Panel.Controls.Add(etk)
etk.Name = i
etk.Tag = j
etk.Size = New Size(26, 26)
etk.BackColor = Color.Black
etk.Location = New Point(30 * i + 10, 30 * j + 10)
Next
Next
End Sub
Private Sub diagonal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = True
For i = 0 To 15
For j = 0 To 15
etk.Name = i
etk.Tag = j
If i = j Then
etk.BackColor = Color.Red
End If
Next
Next
End Sub
End Class
thanks for your interests..
First, the names of your dynamically loaded labels are not unique. You need to concatenate the x and the y to make it unique:
For i = 0 To 15
For j = 0 To 15
Dim etk As New Label
Me.Panel.Controls.Add(etk)
etk.Name = i & "," & j
etk.Tag = j
etk.Size = New Size(26, 26)
etk.BackColor = Color.Black
etk.Location = New Point(30 * i + 10, 30 * j + 10)
Next
Next
Then, when you are looping through in the click event, you need to find the existing label by name. Simply setting the name property on an existing label doesn't do anything to find a control. All that does is alter the one that's already there. To find a control by name, you can look it up using the name as the key value of the Controls property:
For i = 0 To 15
For j = 0 To 15
If i = j Then
Dim etk As Control = Me.Panel.Controls(i & "," & j)
etk.BackColor = Color.Red
End If
Next
Next
Or, more simply:
For i = 0 To 15
Dim etk As Control = Me.Panel.Controls(i & "," & i)
etk.BackColor = Color.Red
Next
My teacher has instructed our class to create a basic word sorting program the 'old fashioned way' in visual basic. So comparing two array values, a and b, then if one is considered higher in the order than the other, swap them if not do nothing, continue until there are no more swaps. Here is the code I have so far:
Imports System.IO
Imports System
Public Class Form1
Public arrText As New ArrayList()
Private Sub btnImprt_Click(sender As Object, e As EventArgs) Handles btnImprt.Click
'Dim OpenAnswerFile As New OpenFileDialog
Dim objReader As New StreamReader("c:\Users\Adam\Desktop\unSortList.txt")
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
Dim i As Integer = 0
txtImport.Text = arrText(i)
End Sub
Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
Dim i As Integer = 0
Dim a As Integer = i + 1
txtImport.Text = i
txtImport.Text = a
Dim Temp As String
Dim Change As Boolean = True
While Change = True
Change = False
For Each i In arrText(i) - 1
If String.Compare(arrText(i), arrText(i + 1)) = 1 Then
Change = True
Temp = arrText(i)
arrText(i) = arrText(i + 1)
arrText(i + 1) = Temp
End If
Next
i = 0
End While
txtSort.Text = arrText(39)
End Sub
My problem is that I am getting an Index error and I'm not sure where the error is located as the logic seems fine.
And yes I am aware of the sorting function built into Visual Basic. but as the teacher said. No cheating.
Your code has several flaws, which I'm ignoring and just concentrating on the sorting part, as your query is related to that. Replace your sort loop with the following and check again. The basic problem was that your loop should only iterate up to List.Count - 2 and not List.Count - 1 because you're comparing List(i) and List(i + 1) inside the loop:
Dim Temp As String
Dim Change As Boolean = True
While Change
Change = False
For i = 0 To arrText.Count() - 2
If String.Compare(arrText(i), arrText(i + 1)) = 1 Then
Change = True
Temp = arrText(i)
arrText(i) = arrText(i + 1)
arrText(i + 1) = Temp
End If
Next
End While