How can I count the number of times a button is pressed while an item is selected from a ListBox - vb.net

The List Box has three candidates and a record Button. Every time the record button is hit I need it to add those button clicks for each candidate that is selected in the List Box. My code keeps counting all the clicks no matter which candidate I am selecting in the List Box. How can I differentiate between each selected item in the List Box.
Here is an image of how the application should look: http://i.imgur.com/N8zM2.jpg
Public Class Form1
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Dim candidatevotes(2) As Integer
Dim vote
Dim total
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
candListBox.Items.Add("Mark Stone")
candListBox.Items.Add("Sheima Patel")
candListBox.Items.Add("Sam Perez")
candListBox.SelectedIndex = 0
End Sub
Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
Dim outfile As IO.StreamWriter
outfile = IO.File.AppendText("voteinfo.txt")
outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
outfile.Close()
End Sub
Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
Dim infile As IO.StreamReader
If IO.File.Exists("voteinfo.txt") = True Then
infile = IO.File.OpenText("voteinfo.txt")
infile.Close()
End If
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
End Sub
End Class

candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
should be
candidatevotes(candListBox.SelectedIndex) += 1
and
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
should be
markLabel.Text = candidatevotes(0)
sheimaLabel.Text = candidatevotes(1)
samLabel.Text = candidatevotes(2)

Related

Moving through pictures with picturebox and next previous buttons

I'm beginner level learner in using VB and currently I am trying create two buttons that will allow me to slide through pictures in an online folder. I need some kind of a way to change the image number in the URL so that the buttons will allow me to move through the pictures.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=1&key=xxxxx")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=2&key=xxxxxxx")
Try this, notice the private variable.
Private _CurrentImage as Int32 = 1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load(string.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage ))
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.loadstring.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage )))
End Sub
I am assuming Button4 is PREVIOUS and Button5 is NEXT. You will want to do some work to prevent numbers out of range but you can see how easy it is to Increment/Decrement a number and then stuff it in a string.

Call function on button click event

How do I call this vb.net function on the button click event?
Private Sub GridView_UDGReport_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
For rowIndex As Integer = GridView_UDGReport.Rows.Count - 2 To 0 Step -1
Dim gviewRow As GridViewRow = GridView_UDGReport.Rows(rowIndex)
Dim gviewPreviousRow As GridViewRow = GridView_UDGReport.Rows(rowIndex + 1)
For cellCount As Integer = 0 To gviewRow.Cells.Count - 1
If gviewRow.Cells(cellCount).Text = gviewPreviousRow.Cells(cellCount).Text Then
If gviewPreviousRow.Cells(cellCount).RowSpan < 2 Then
gviewRow.Cells(cellCount).RowSpan = 2
Else
gviewRow.Cells(cellCount).RowSpan = gviewPreviousRow.Cells(cellCount).RowSpan + 1
End If
gviewPreviousRow.Cells(cellCount).Visible = False
End If
Next
Next
End Sub
Since you are not using the parameters anyways, you can simply call the method with Nothing as parameter.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GridView_UDGReport_DataBound1(Nothing, Nothing)
End Sub
Append the first line so that the sub handles more than one event, as follows:
Private Sub GridView_UDGReport_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound, Button1.Click
Alternatively, if you need your Click event to run some other code in addition to calling this sub, do this:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'do something
GridView_UDGReport_DataBound1(sender, e)
'do something else
End Sub

Timer and updating label text

im trying to make an app to show when there is somthing "new". So i made Label1 and Timer. I want every secound to check if in status.txt text is 0 to show nothing, if its 1 to show text from text.txt. This is what try so far:
Public Class Form1
Dim client As WebClient = New WebClient()
Dim status As String = client.DownloadString("http://force-play.com/launcher/status.txt")
Dim information As String = client.DownloadString("http://force-play.com/launcher/text.txt")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If status = 0 Then
Label1.Text = "No New Info"
End If
If status = 1 Then
Label1.Text = information
End If
End Sub
End Class
Try setting the status string at the beginning of your Timer1_Tick method.

How to print certain indexes based off of certain check boxes being checked

Below I have an array and in my design I have a check list box with 10 options. For example, if boxes 1 and 2 were checked, I would only want to print Indexes 0 and 1 ONLY. I have a button that prints all of the array members (included below) and that is what I want to make print only selected items. I have tried using a switch but that file had gotten corrupted and I am lost. Thank you. (Language is VB)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn1.Click
Dim strDecimal(9) As String
strDecimal(0) = FormatPercent(0.0146175)
strDecimal(1) = FormatPercent(0.0345324585)
strDecimal(2) = FormatPercent(0.09324543575)
strDecimal(3) = FormatPercent(0.07346475)
strDecimal(4) = FormatPercent(0.0772346615)
strDecimal(5) = FormatPercent(0.42234234654)
strDecimal(6) = FormatPercent(0.6246264664)
strDecimal(7) = FormatPercent(0.4524642234)
strDecimal(8) = FormatPercent(0.6876543534)
strDecimal(9) = FormatPercent(0.6876543534)
For num As Integer = 0 To strDecimal.Length - 1
listArrays.Items.Add(strDecimal(num))
Next
End Sub
Private Sub clearList()
listArrays.Items.Clear()
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
clearList()
End Sub
Assuming you're using a CheckedListBox and want to know which items are Checked:
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
For Each itm As String In listArrays.CheckedItems
Debug.Print(itm)
Next
End Sub

Windows Form won't pass a variable to another?

my problem is that is have 4 forms, 3 of these forms allow me to pass variables between them.. however one form the Booking form won't allow me to pass the txtTotal variable to the Confirmation Form.
All the other forms all me to do this, im not doing anything different i can see... im thinking that perhaps another part of the form is prohibiting me from passing that txtTotal to the Confirmatin form.
The following is for the Confirmation form, it should display the txtTotal in lblprice from the Booking form but shows nothing
Public Class Confirmation
Private Sub btnBack_Click(sender As System.Object, e As System.EventArgs) Handles btnBack.Click
Dim FrmPayment As New Payment
FrmPayment.Show()
Me.Hide()
End Sub
Private Sub btnHome_Click(sender As System.Object, e As System.EventArgs) Handles btnHome.Click
Dim FrmSelection As New Selection
FrmSelection.Show()
Me.Hide()
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles lblshow.Click, lbltime.Click, lbldate.Click, lblcust.Click, lblprice.Click
End Sub
Private Sub Confirmation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'lblprice displays nothing and the rest of the labels display the correct values
lblprice.Text = Booking.txtTotal.Text
lblshow.Text = Selection.cboShowSelect.Text
lbldate.Text = Selection.cboDateSelect.Text
lbltime.Text = Selection.cboTimeSelect.Text
End Sub
End Class
Here is all the code in the Booking form if it helps
Public Class Booking
Private Sub Booking_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'labels 1,4,6 display information from the comboboxes on the Selection Form
Label1.Text = Selection.cboShowSelect.Text
Label4.Text = Selection.cboDateSelect.Text
Label6.Text = Selection.cboTimeSelect.Text
Dim i As Integer
For i = 1 To 4
cboAdult.Items.Add(i)
cboChild.Items.Add(i)
cboSenior.Items.Add(i)
cboStudent.Items.Add(i)
Next i
End Sub
Public Sub ComboBoxes_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboAdult.SelectedIndexChanged, cboChild.SelectedIndexChanged, cboSenior.SelectedIndexChanged, cboStudent.SelectedIndexChanged
'Assigned an evet handler to all of the comboboxes then calculates the price and puts in total box
Dim Totalcombo1, Totalcombo2, Totalcombo3, Totalcombo4, Price As Decimal
Dim valuecombo1 = (cboAdult.SelectedIndex + 1) 'finds position of option selected & adds one to get number of tickets
Dim valuecombo2 = (cboChild.SelectedIndex + 1)
Dim valuecombo3 = (cboSenior.SelectedIndex + 1)
Dim valuecombo4 = (cboStudent.SelectedIndex + 1)
'if the submit button is selected without there being a value selected from any combobox then error should appear, saying at least 1 ticket should be purchased.
If (cboChild.SelectedIndex = -1) Then
Totalcombo2 = 0
Else
Price = 6.5
Totalcombo2 = valuecombo2 * Price
End If
'determines the ticketprice of combobox 1
If (cboAdult.SelectedIndex = -1) Then
Totalcombo1 = 0
Else
Price = 9
Totalcombo1 = valuecombo1 * Price
End If
'determines the ticketprice of combobox 2
If (cboSenior.SelectedIndex = -1) Then
Totalcombo3 = 0
Else
Price = 6.5
Totalcombo3 = valuecombo3 * Price
End If
'determines the ticketprice of combobox 3
If (cboStudent.SelectedIndex = -1) Then
Totalcombo4 = 0
Else
Price = 6.5
Totalcombo4 = valuecombo4 * Price
End If
'determines the ticketprice of combobox 4
If (cboAdult.SelectedIndex = -1 And cboChild.SelectedIndex = -1 And cboSenior.SelectedIndex = -1 And cboStudent.SelectedIndex = -1) Then
MessageBox.Show("Please make at least one ticket selection before continuing. ")
End If
txtTotal.Text = Totalcombo1 + Totalcombo2 + Totalcombo3 + Totalcombo4
'adds the totals of the ticketprices and then inserts into the Total label
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChild.SelectedIndexChanged
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim FrmSelection As New Selection
FrmSelection.Show()
Me.Hide()
End Sub
Sub Form_OpenBooking(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If (cboChild.SelectedIndex >= 0 And (cboSenior.SelectedIndex = -1 And cboAdult.SelectedIndex = -1)) Then
MessageBox.Show("A child must be accompanied by at least one adult", "Invalid Selection")
ElseIf (txtTotal.Text > 0) Then 'if the total label is greater than zero then this means at least one ticket selection has been made
Dim Form3 As New Payment ' if above is true open Booking Form
Form3.Show()
Me.Hide()
End If
End Sub
Private Sub Resetbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
cboAdult.SelectedIndex = -1
cboChild.SelectedIndex = -1
cboSenior.SelectedIndex = -1
cboStudent.SelectedIndex = -1
txtTotal.Text = 0
End Sub
End Class
Thanks in advance!
Try this
Public frm as new frmBooking
frm.txtTotal.Text=
It should work
You need to use the actual instance of the Form that you created, not the Class name. You should also make sure that you turn on Option Strict.
Try frmBooking.txtTotal.Text instead of Booking.txtTotal.Text