Alright, so the program is set up where you have a set of radio buttons with 5 states, and another set of 5 with 5 capitals. There's a button which tells you if they match or not. However I have to do it a certain way where clicking a radio button assigns a variable to both 'strCapital' and 'strChoice' and you compare them to see if they match.
I've tried to figure everything out (since it sounds easy in theory) but I've hit a wall.
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Dim strCapital As String
Dim strChoice As String
Dim strLittleRock As String
Dim strSpringfield As String
Dim strFrankfort As String
Dim strSalem As String
Dim strMadison As String
Private Sub radArkansas_CheckedChanged(sender As Object, e As EventArgs) Handles radArkansas.CheckedChanged
strCapital = strLittleRock
End Sub
Private Sub radIllinois_CheckedChanged(sender As Object, e As EventArgs) Handles radIllinois.CheckedChanged
strCapital = strSpringfield
End Sub
Private Sub radSpringfield_CheckedChanged(sender As Object, e As EventArgs) Handles radSpringfield.CheckedChanged
strChoice = strSpringfield
End Sub
Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click
If strCapital = strChoice Then
lblMsg.Text = "Correct"
ElseIf strCapital <> strChoice Then
lblMsg.Text = "Incorrect"
End If
End Sub
Private Sub radLittleRock_CheckedChanged(sender As Object, e As EventArgs) Handles
radLittleRock.CheckedChanged
strChoice = strLittleRock
End Sub
End Class
EDIT: Also I forgot to mention the main problem which is always a bad thing. Basically whenever I run it and enter something incorrect (checking Arkansas and Springfield for example) it always says it's correct.
I did something similar... you can modify this code:
Private Sub btnAmIRight_Click(sender As Object, e As EventArgs) Handles btnAmIRight.Click
' displays if submission is correct
' declare variables
Dim strAmIRight As String
Dim dblMatch As Double
If rbtnAlabama.Checked = True And rbtnMontgomery.Checked = True Then
dblMatch = 1
ElseIf rbtnAlaska.Checked = True And rbtnJuneau.Checked = True Then
dblMatch = 1
ElseIf rbtnArizona.Checked = True And rbtnPhoenix.Checked = True Then
dblMatch = 1
ElseIf rbtnArkansas.Checked = True And rbtnLittleRock.Checked = True Then
dblMatch = 1
ElseIf rbtnCalifornia.Checked = True And rbtnSacramento.Checked = True Then
dblMatch = 1
ElseIf rbtnColorado.Checked = True And rbtnDenver.Checked = True Then
dblMatch = 1
ElseIf rbtnConnecticut.Checked = True And rbtnHartford.Checked = True Then
dblMatch = 1
ElseIf rbtnDelaware.Checked = True And rbtnDover.Checked = True Then
dblMatch = 1
ElseIf rbtnFlorida.Checked = True And rbtnTallahassee.Checked = True Then
dblMatch = 1
ElseIf rbtnGeorgia.Checked = True And rbtnAtlanta.Checked = True Then
dblMatch = 1
Else
dblMatch = 0
End If
' assign code to variable
If dblMatch = 1 Then
strAmIRight = "Correct"
ElseIf dblMatch = 0 Then
strAmIRight = "Try Again"
End If
' display result
lblResult.Text = strAmIRight.ToString
End Sub
Related
I want to use DGV as a table to take User’s inputs. And in few columns, I want User to select values from a collection hence I decided to use Comboboxcontrol. I selected a standard DGV ("DGV2") and a standard Combobox ("CBTest") as tools. And I programmed this Combo to appear on Columnindex 2 (when user enters the particular cell, Combo box pops-up). User can select from the combo items (Drop-Down-List) and after selection clicked done, curser moves to next column.
Now there is problems with UP/DOWN Keys when control is in cell having COMBO-BOX-
I am not able to control behavior of UP /DOWN Keys when user enters the Cell having COMBO Control.
Sometime with up-down keys cursor moves between Combobox items and sometimes it jumps in other rows.
I am a beginner so any hint and help will be useful.
Code I used -
Public Class frmSIDDGV
Dim nCol As Integer = 0
Dim nRow As Integer = 0
Private Sub frmSIDDGV_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DGV2
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.AutoResizeColumns()
.RowTemplate.Height = 40
.RowHeadersVisible = False
End With
DGV2.ColumnCount = 5
With DGV2
.Columns(0).Name = "Item"
.Columns(1).Name = "MRP"
.Columns(2).Name = "Qty"
.Columns(3).Name = "Unit"
.Columns(4).Name = "Amount"
End With
DGV2.Rows.Add()
DGV2(0, 0).Value = 1
End Sub
Private Sub DGV2_KeyUp(sender As Object, e As KeyEventArgs) Handles DGV2.KeyUp
If e.KeyCode = Keys.Enter Then
If ActiveControl.Name <> "CBTest" Then
If nCol = DGV2.ColumnCount - 1 Then
DGV2.Rows.Add()
DGV2.CurrentCell = DGV2(0, nRow + 1)
Else
DGV2.CurrentCell = DGV2(nCol + 1, nRow)
End If
End If
ElseIf e.KeyCode = Keys.Escape Then
If ActiveControl.Name = "CBTest" Then
CBTest.SelectedIndex = 0 : CBTest.Visible = False
DGV2.CurrentCell = DGV2(nCol, nRow)
DGV2.Focus()
End If
End If
End Sub
Private Sub DGV2_CurrentCellChanged(sender As Object, e As EventArgs) Handles DGV2.CurrentCellChanged
Try
nCol = DGV2.CurrentCell.ColumnIndex
nRow = DGV2.CurrentCell.RowIndex
Catch ex As Exception
nCol = 0
nRow = 0
End Try
End Sub
Private Sub DGV2_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DGV2.CellEnter
Select Case e.ColumnIndex
Case 2 ' User entered in Cell name "Item"
DGV2.Controls.Add(CBTest)
'DGV2.BeginEdit(True)
Dim oRectangle = DGV2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
CBTest.Size = New Size(oRectangle.Width, oRectangle.Height)
CBTest.Location = New Point(oRectangle.X, oRectangle.Y)
CBTest.Visible = True
SendKeys.Send("{F4}")
CBTest.SelectedIndex = 0
CBTest.Capture = True
CBTest.Focus()
End Select
End Sub
Private Sub CBTest_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles CBTest.SelectionChangeCommitted
With DGV2
.Focus()
.Item(nCol, nRow).Value = Trim(CBTest.Text)
.CurrentCell = .Rows(.CurrentRow.Index).Cells(nCol + 1)
End With
CBTest.Visible = False
End Sub
End Class
Can I do this shorter from a single timer without needing 5 timers?
I can run it in a sub Timer1.Enabled = True, Timer1.Start () and can change according to preferences, timer2, timer3. so I want to do this to go shorter. I think I should have a case function, or something like that. how could i do it
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("1") Then
BttRnd.PerformClick()
Else
RndTitaniumA2()
End If
End If
Next
Timer1.Stop()
Timer1.Enabled = False
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("2") Then
BttRnd.PerformClick()
Else
RndTitaniumA3()
End If
End If
Next
Timer2.Stop()
Timer2.Enabled = False
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("3") Then
BttRnd.PerformClick()
Else
RndTitaniumA4()
End If
End If
Next
Timer3.Stop()
Timer3.Enabled = False
End Sub
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("4") Then
BttRnd.PerformClick()
Else
RndTitaniumA5()
End If
End If
Next
Timer4.Stop()
Timer4.Enabled = False
End Sub
Private Sub Timer5_Tick(sender As Object, e As EventArgs) Handles Timer5.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("5") Then
BttRnd.PerformClick()
Else
MsgBox("")
End If
End If
Next
Timer5.Stop()
Timer5.Enabled = False
End Sub
Let's start with your answers to my questions.
Array indexes in .net start at zero. If you have 10 lines in your textbox, you will get an index out of range exception with For i As Integer = 1 To 10 because there is no index 10. The indexes run from 0 to 9. I still don't see how you are preventing the user from deleting a few lines.
It won't run because you have preceded the code with Exit For. When the code reaches that line it exits the For loop and goes immediately to TimerX.Stop().
Private Sub OpCode()
For i = 0 To 10
Exit For
MessageBox.Show(i.ToString)
Next
End Sub
The MessageBox in the above code will never be displayed. Even if you repositioned the Exit For, you have the exit in both the If block and the Else block so the code would only survive one iteration.
You still didn't answer if the code works as written or why you are using a For loop at all and leaving the loop on the first iteration. Also, the mysterious empty MsgBox and why there are 5 timers.
You can add a break point and step through your code.
Notice the Handles clause in the .Tick event. This event now handles all the Timers.
I used a Select Case as suggested by #preciousbetine in comments. Since a Timer is a component it has no Name property (what I would normally use in the Select Case) so, I used the Is operator. I could also have set the .Tag property for each Timer and used that. I left in the Exit For (repositioned) but be aware; you will only get a single iteration.
Calling an event in code can have unanticipated effects. I showed you how to move the code from the button click to a separate Sub and then call it from the .Click and your routine.
Private Sub AnyTimer_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Timer2.Tick, Timer3.Tick, Timer4.Tick, Timer5.Tick
Dim t As System.Windows.Forms.Timer = DirectCast(sender, System.Windows.Forms.Timer)
Dim strx As String
Dim stry As String
stry = TxtBoxIntDrawsY.Text
For i As Integer = 0 To 9
strx = TextBox1.Lines(i)
If stry.Contains(strx) = True Then
RndTitaniumA1()
Exit For
Else
Dim TextBox4Contents As String = ""
Select Case True
Case t Is Timer1
TextBox4Contents = "1"
Case t Is Timer2
TextBox4Contents = "2"
Case t Is Timer3
TextBox4Contents = "3"
Case t Is Timer4
TextBox4Contents = "4"
Case t Is Timer5
TextBox4Contents = "5"
End Select
If TextBox4.Text = TextBox4Contents Then
RndSub()
Else
Select Case True
Case t Is Timer1
RndTitaniumA2()
Case t Is Timer2
RndTitaniumA3()
Case t Is Timer3
RndTitaniumA4()
Case t Is Timer4
RndTitaniumA5()
Case t Is Timer5
MsgBox("") '???
Case Else
MessageBox.Show("Sender does not match any Timer")
Return
End Select
End If
Exit For
End If
Next
t.Stop()
t.Enabled = False
End Sub
Private Sub BttnRnd_Click(sender As Object, e As EventArgs) Handles BttnRnd.Click
RndSub()
End Sub
Private Sub RndSub()
'Your button code here
End Sub
Imports System.Threading.Thread
Public Class Form1
Dim delay As Integer = 200
Dim i As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
While i <= 5
PictureBox1.Visible = True
PictureBox1.Refresh()
Sleep(delay)
PictureBox1.Visible = False
PictureBox2.Visible = True
PictureBox2.Refresh()
Sleep(delay)
PictureBox2.Visible = False
PictureBox3.Visible = True
PictureBox3.Refresh()
Sleep(delay)
PictureBox3.Visible = False
PictureBox4.Visible = True
PictureBox4.Refresh()
Sleep(delay)
PictureBox4.Visible = False
PictureBox5.Visible = True
PictureBox5.Refresh()
Sleep(delay)
PictureBox5.Visible = False
i = i + 1
If i = 6 Then
i = 1
End If
End While
End Sub
i wrote the above code. it's working. but the following code is not not working. i want to minimize the code. i want to use 20 picture box. instead of the above code i want to use small code but it will do the same work. please help me.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
While i <= 5
Dim pic As PictureBox
Dim matches() As Control
matches = Me.Controls.Find("PictureBox" & i.ToString(), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
pic = DirectCast(matches(0), PictureBox)
pic.Visible = True
pic.Refresh()
Sleep(delay)
pic.Visible = False
End If
i = i + 1
If i = 6 Then
i = 1
End If
End While
End Sub
End Class
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
While i <= 5
Dim pic As PictureBox
Dim matches() As Control
matches = Me.Controls.Find("PictureBox" & i.ToString(), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
pic = DirectCast(matches(0), PictureBox)
pic.Visible = True
pic.Refresh()
Wait(delay)
pic.Visible = False
End If
i = i + 1
If i = 6 Then
i = 1
End If
End While
End Sub
this code is correct... sorry i wrote wrong code 1st time.. now it's correct.. thank u all..
My task was to create a 10 question maths quiz with 2 random numbers and a random operator (only +, - or *) for each question. Also the program must ask for the users name. Which I have done.
But as my quiz has only 10 questions, there is a very small probability that one of the operators will be in the majority or that one may not show up. So basically I need something that can limit how many times each operator shows up, for example each operator can only show up 4 times.
As you can see from my code, I'm coding in form. I would be very grateful if you could help me with this as I couldn't find anything on the internet for it and I'm also not too sure how to go about solving this problem either.
Private Sub StartTheQuiz()
number1 = randomizer.Next(1, 13)
number2 = randomizer.Next(1, number1)
leftpluslbl.Text = number1.ToString
rightpluslbl.Text = number2.ToString
userinput.Value = 0
Correct.Visible = False
Incorrect.Visible = False
End Sub
Public Sub TheOperator()
Randomize()
operation = randomizer.Next(1, 4)
addmintime = operation
If addmintime = 1 Then
answer = number1 + number2
operatorlbl.Text = "+"
Userinput.Value = useranswer
ElseIf addmintime = 2 Then
answer = number1 - number2
operatorlbl.Text = "-"
Userinput.Value = useranswer
ElseIf addmintime = 3 Then
answer = number1 * number2
operatorlbl.Text = "x"
Userinput.Value = useranswer
End If
End Sub
Public Function CheckTheAnswer()
If userinput.Value = answer Then
Return True
Else
Return False
End If
End Function
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
If NameBox.Text = "What is your name? Enter your name here" Then
NameBox.BackColor = Color.Red
NextBtn.Visible = False
Check.Visible = False
Else
StartTheQuiz()
NameBox.BackColor = Color.LimeGreen
Start.Enabled = False
Start.Visible = False
TheOperator()
question = 0
score = 0
Check.Visible = True
NameBox.Enabled = False
End If
End Sub
Private Sub Check_Click(sender As Object, e As EventArgs) Handles Check.Click
If CheckTheAnswer() = True Then
Correct.Visible = True
Incorrect.Visible = False
question = question + 1
score = score + 1
ElseIf CheckTheAnswer() = False Then
Correct.Visible = False
Incorrect.Visible = True
question = question + 1
score = score + 0
End If
NextBtn.Visible = True
Check.Visible = False
End Sub
Private Sub NextBtn_Click(sender As Object, e As EventArgs) Handles NextBtn.Click
If question < 10 Then
StartTheQuiz()
TheOperator()
Check.Visible = True
NextBtn.Visible = False
ElseIf question >= 10 Then
MessageBox.Show("You have scored " & score & "/10")
MessageBox.Show("Goodbye")
Me.Close()
End If
End Sub
End Class
Here's an example of what I mentioned in the comments. It builds a list of the desired number of operations in equal quantities, then shows how to pull a random one from that list:
Public Class Form1
Private Operations As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Operations = CreateOperations(10)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Operations.Count > 0 Then
Dim operation As String = NextRandomOperation()
Debug.Print(operation)
Else
Debug.Print("No more operations in list!")
End If
End Sub
Private Function CreateOperations(ByVal NumberOperations As Integer) As List(Of String)
Dim operators() As String = {"+", "-", "X"}
Dim operations As New List(Of String)
While Operations.Count < NumberOperations
Operations.AddRange(operators)
End While
While Operations.Count > NumberOperations
Operations.RemoveAt(Operations.Count - 1)
End While
Return operations
End Function
Private Function NextRandomOperation() As String
Static R As New Random
If Operations.Count > 0 Then
Dim index As Integer = R.Next(Operations.Count)
Dim operation As String = Operations(index)
Operations.RemoveAt(index)
Return operation
End If
Return ""
End Function
End Class
The proposed solution above will always produce four +, three - and three X. If you want a more random distribution you need to randomize the operators() array.
Dim rnd As New System.Random
Dim operators() As String = New String() {"+", "-", "X"}.OrderBy(Function() rnd.Next).ToArray
I am looking for a way, if its even possible, to re enable a label without actually needing to use its label name?
I have a game with labels I am using as click able boxes. The first box becomes disabled after the click event occurs, I want after the second box is clicked to re enable that first box. Any Ideas? Here is the code for the first two box click events.
Edit: there will be 15 labels, 2 can be chosen at a time. The first will be disabled so that it can't be chosen a second time.
Private Sub lblMemory1_Click(sender As Object, e As EventArgs) Handles lblMemory1.Click
Dim intClickBox As Integer = 1
Dim intClickAnswer As Integer
intClickAnswer = GuessClick(intClickBox)
If blnActive = False Then
blnActive = True
whatClicked1 = intClickBox
lblMemory1.BackColor = Color.Green
lblMemory1.Text = intClickAnswer.ToString
lblMemory1.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory1.BackColor = Color.Cyan
lblMemory1.Text = "X"
lblMemory1.Enabled = False
End If
If blnActive = True Then
whatClicked2 = intClickBox
lblMemory1.BackColor = Color.Green
lblMemory1.Text = intClickAnswer.ToString
lblMemory1.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory1.BackColor = Color.Cyan
lblMemory1.Text = "X"
blnActive = False
End If
End Sub
Private Sub lblMemory2_Click(sender As Object, e As EventArgs) Handles lblMemory2.Click
Dim intClickBox As Integer = 2
Dim intClickAnswer As Integer
intClickAnswer = GuessClick(intClickBox)
If blnActive = False Then
blnActive = True
whatClicked1 = intClickBox
lblMemory2.BackColor = Color.Green
lblMemory2.Text = intClickAnswer.ToString
lblMemory2.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory2.BackColor = Color.Cyan
lblMemory2.Text = "X"
lblMemory2.Enabled = False
End If
If blnActive = True Then
whatClicked2 = intClickBox
lblMemory2.BackColor = Color.Green
lblMemory2.Text = intClickAnswer.ToString
lblMemory2.Refresh()
System.Threading.Thread.Sleep(2000)
lblMemory2.BackColor = Color.Cyan
lblMemory2.Text = "X"
blnActive = False
End If
End Sub
you can do something like
Public Sub game(sender As Object)
Dim lbl As Label = sender
If lbl.Enabled Then
For i = 0 To Me.Controls.Count - 1
Dim lbl2 As Label = Me.Controls(i)
If Not (lbl2.Enabled) Then
lbl2.Enabled = True
GoTo exitLoop
End If
Next
End If
exitLoop:
lbl.Enabled = False
End Sub
then in each label just call the game sub and pass sender like so...
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click
game(sender)
End Sub
this will allow only 1 label to be diabled at any give time...
you can also (within the loops) use lbl and lbl2 to set the properties for each label.
if you use a var to save the name of the first textbox you can also specifically re-enable only that box...
let me know if you want the code for that too.