How to print certain indexes based off of certain check boxes being checked - vb.net

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

Related

Use ComboBox to Display Specific ImageList

I have a program I created in VisualBasic that is very similar to a slideshow image viewer. On the left side the user is able to select an image from a location and display it. On the right side I would like them to be able to use the combo box to select a category which would only display images in that category.
I am currently able to get an image list loaded in the right side and the user can cycle through that list, but I am having trouble figuring out how to connect the combobox and imagelist. Maybe there is another route I should try?
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub Btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
btnPrev.Enabled = True
Static i As Integer
Dim incp As String
incp = +1
i += 1
PictureBox2.Image = ImageList1.Images(i)
If i = ImageList1.Images.Count - 1 Then
i = -1
End If
End Sub
Private Sub Btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Static i As Integer
Dim incp As String
incp = +1
i += 1
PictureBox2.Image = ImageList1.Images(i)
If i = ImageList1.Images.Count - 1 Then
i = -incp
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
End Class
So you're saying that you have multiple ImageList objects and you want the user to select one using the ComboBox? If so then you do it like you would for selecting from any other list of objects using a ComboBox, e.g.
category1ImageList.Tag = "Category 1"
category2ImageList.Tag = "Category 2"
categoriesComboBox.DisplayMember = "Tag"
categoriesComboBox.DataSource = {category1ImageList, category2ImageList}
The user selects from the Tag values and then the SelectedItem of the ComboBox is the selected ImageList.

ListBoxes in Visual Basic

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.

Need hints for completing code:

This question has been answered.
Do not want to leave it exposed.
Private Sub btnTTL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles findzipButton.Click
Dim zipCode As String
'forgot
If (ListBox1.FindString(findzipButton.Text) >= 0) Then
ttlTextBox.Text = "$15"
ElseIf (ListBox2.FindString(findzipButton.Text) >= 0) Then
ttlTextBox.Text = "$20"
Else
MessageBox.Show("The zipcode was not found!")
End If
End Sub
End Class
So what I think your trying to do is match the input a user puts into a text box with a selection in the either ListBoxA or ListBoxB. I just tried this in VS 2012, and it seems to work in the way the problem above describes, but I'm only trying to find and display the shipping cost:
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub ListBox1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListBox1.Items.Add("60611")
ListBox1.Items.Add("60234")
ListBox1.Items.Add("56789")
ListBox1.Items.Add("23467")
ListBox1.Items.Add("60543")
ListBox1.Items.Add("60561")
ListBox1.Items.Add("55905")
ListBox1.Items.Add("89567")
ListBox2.Items.Add("50978")
ListBox2.Items.Add("78432")
ListBox2.Items.Add("98432")
ListBox2.Items.Add("97654")
ListBox2.Items.Add("20245")
End Sub
Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim zipCode As String = txtZipCode.Text
If (ListBox1.FindString(zipCode) >= 0) Then
txtShipping.Text = "$15"
ElseIf (ListBox2.FindString(zipCode) >= 0) Then
txtShipping.Text = "$20"
Else
MessageBox.Show("The zipcode was not found!")
End If
End Sub
You were on the right track. What you needed to do was compare what the user actually input to the textbox to what was available in the Listbox. The FindItem() method will then result a Long. If it did find your search string it will build the shipping text box.

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)

NullReferenceException was unhandled VB.NET

I am trying to assign to an array a record that has to be split into more elements for search criteria reasons. For example, if criteria is set to First Name, then search for matching keyword in array element 0 because that's where first name will be placed after the currently selected record is Split().
Source for the search form:
Public Class frmSearch
Dim arrayCurrentRecord(6) As String
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = DialogResult.Cancel
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
'fill array with list box items
Dim arraySearch(frmMain.numberOfRecords) As String
For i = 0 To frmMain.numberOfRecords - 1
arraySearch(i) = frmMain.lstListBox.Items(i)
Next i
If rbtnFirstName.Checked = True Then
For i = 0 To UBound(arraySearch)
arrayCurrentRecord = arraySearch(i).Split(" ")
If InStr(arrayCurrentRecord(0), txtSearch.Text) = True Then
lstSearch.Items.Add(arraySearch(i))
End If
Next i
MsgBox("search complete")
End If
End Sub
End Class
Changing For i = 0 To UBound(arraySearch) to
For i = 0 To UBound(arraySearch) - 1 fixed the issue, but it won't find anything just says that search was complete.
Changing line
If InStr(arrayCurrentRecord(0), txtSearch.Text) = True
to
If InStr(arrayCurrentRecord(0), txtSearch.Text)
resolved the problem.