ListBoxes in Visual Basic - vb.net

I have an assignment where the user will enter numbers into a list box. When the user is done entering numbers, it will compute the average and the standard deviation.(Our professor does not want us to use built in standard deviation functions) The program will have a context menu and these are the guidelines for creating the menu: You must create a subroutine called SetMenu. It is used to enable and disable context menu based on the contents of the list. When the list is empty,Clear is disabled. When the list has at least one number in it,Clear is enabled. When nothing is selected, Delete Item is disabled. When something is selected, Delete Item is enabled.
That is where I need help, creating that subroutine.
Here is my code:
Option Strict On
Public Class Form1
Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBtn.Click
ListBox.Items.Add(InputBox.Text)
InputBox.Text = ""
End Sub
Private Sub ComputeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComputeBtn.Click
Dim sum, x, number, numberSum, subtractedNum As Integer
Dim average, insideRoot, squaredRoot As Double
For x = 0 To ListBox.Items.Count - 1
ListBox.SelectedIndex = x
number = Integer.Parse(ListBox.SelectedItem.ToString)
sum = sum + number
average = sum / ListBox.Items.Count
Next
AverageLbl.Text = average.ToString
For x = 0 To ListBox.Items.Count - 1
ListBox.SelectedIndex = x
number = Integer.Parse(ListBox.SelectedItem.ToString)
subtractedNum = CInt((number - average) ^ 2)
numberSum = numberSum + subtractedNum
insideRoot = (numberSum / (ListBox.Items.Count - 1))
squaredRoot = insideRoot ^ (1 / 2)
Next
DeviationLbl.Text = squaredRoot.ToString("N2")
End Sub
Private Sub ClearMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearMenu.Click
ClearMenu.Enabled = False
SetMenu()
ListBox.Items.Clear()
End Sub
Private Sub DeleteMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteMenu.Click
ListBox.Items.Remove(ListBox.SelectedItem)
End Sub
Private Sub InputBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputBox.TextChanged
If IsNumeric(InputBox.Text) And InputBox.Text <> "" Then
AddBtn.Enabled = True
Else
AddBtn.Enabled = False
End If
End Sub
Private Sub SetMenu()
'Need help here
End Sub
End Class

How about:
Private Sub toggleContextMenuItems()
ClearToolStripMenuItem.Visible = (ListBox1.Items.Count > 0)
DeleteToolStripMenuItem.Visible = (ListBox1.SelectedIndex > -1)
End Sub
Then call this method when needed.

Related

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

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

How do I display the highest average of checkbox when multiple checkboxes checked in VB.NET?

Hi I am trying to make a program that has 6 checkboxes and when one is checked a label displays the actors take in average, but if more than one is checked the label will only show the highest average of the checked boxes.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
SC = ((Val(425488741) + Val(555909803) + Val(868659354) + Val(966435555) + Val(720388023) + Val(617520987)) / 6)
If CheckBox1.Checked = True Then
Label3.Text = "Sean Connery $" & SC
Exit Sub
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
GL = 513445231
If CheckBox2.Checked = True Then
Label3.Text = "George Lazenby $" & GL
Exit Sub
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
RM = ((Val(785677477) + (426826774) + (666367656) + (624527272) + (481005579) + (405873493) + (316186616)) / 7)
If CheckBox3.Checked = True Then
Label3.Text = "Roger Moore $" & RM
Exit Sub
End If
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
TD = ((Val(362876056) + (271586451)) / 2)
If CheckBox4.Checked = True Then
Label3.Text = "Timothy Dalton $" & TD
Exit Sub
End If
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
PB = ((Val(499954330) + (465588535) + (504705882) + (546490272)) / 4)
If CheckBox5.Checked = True Then
Label3.Text = "Pierce Brosnan $" & PB
Exit Sub
End If
End Sub
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
DC = ((Val(640803677) + (586090727)) / 2)
If CheckBox6.Checked = True Then
Label3.Text = "Daniel Craig $" & DC
Exit Sub
End If
End Sub
In the future, when asking a question, please do not include all the unnecessary details. If understand your problem correctly, this is one possible simple solution:
Dim incomes = New Integer() {100, 2000, 500}
Dim names = New String() {"John", "Tim", "Allan"}
Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim maxIncome As Integer = 0
Dim name As String = ""
For i = 0 To incomes.Length - 1
If CheckedListBox1.GetItemChecked(i) And incomes(i) > maxIncome Then
maxIncome = incomes(i)
name = names(i)
End If
Next
Label1.Text = name & " $" & maxIncome
End Sub
For this code to work you need to create a CheckedListBox with 3 items in it's collection. However, this approach might lead to a very bad code once there are many values. In that case you should use a database or at least a structure like:
Structure People
Dim name As String
Dim income As Integer
Dim checkBox As CheckBox
End Structure
Also the is no need to write Exit Sub anywhere in your code, it does nothing.
Details: Saulius is correct in that you should use a database and that you should use a checkedlistbox instead to make it much easier. However, I will just assume you want to use separate checkboxes for whatever reason.
Solution:
Create an Actor Class
Public Class Actor
Property Name As String
Property TotalValue As Double
End Class
In your main form
Public Sub DisplayHighestPaidActor(ByVal actorName As String, ByVal isChecked As Boolean)
If isChecked Then
'Add the Actor
selectedActors.Add((From actor In allActors
Where actor.Name = actorName).FirstOrDefault())
'Order the Actors
selectedActors = (From actor In selectedActors
Order By actor.TotalValue Descending).ToList()
Else
'Remove the Actor
selectedActors.Remove((From actor In allActors
Where actor.Name = actorName).FirstOrDefault())
'Order the Actors
selectedActors = (From actor In selectedActors
Order By actor.TotalValue Descending).ToList()
End If
If (selectedActors.Count > 0) Then
'Display the highest value
lblHighestPaidActor.Text = selectedActors.Item(0).Name.ToString() _
+ " $" + selectedActors.Item(0).TotalValue.ToString()
Else
lblHighestPaidActor.Text = ""
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
DisplayHighestPaidActor("Sean Connery", CheckBox1.Checked)
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
DisplayHighestPaidActor("George Lazenby", CheckBox2.Checked)
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
DisplayHighestPaidActor("Roger Moore", CheckBox3.Checked)
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

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

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)