Make the input of two combo boxes search for, read and display data from csv file - vb.net

Look this question is probably going to be unanswered or really confusing. This is all in Visual Studio (VB Code is preferred).
I have this program, and basically when the program loads, I want the combo boxes to be populated with data from a CSV file(I have done this.)
Then once I choose a value from the section box, I want the competitor number combo box to only show the numbers that have the selected value from the section box (i.e dependent combo boxes). Then based of the two values chosen, I want a name to be displayed.
So if Section 1, competitor 1 is chosen, I want their name to be displayed. But if I change it to section 1 competitor 2, the name should change.

Build a class for your competitor object.
Public Class Person
Public Property ComboBox1Value As String
Public Property ComboBox2Value As String
Public Property Name As String
End Class
Then declare a collection to hold the list of objects somewhere. I added it to the top of Form1.vb. Now when you hit your Search_For_Name sub, just loop through the collection until the combobox values match those in one of the objects.
Public Class Form1
Dim MyCollection As New Collection
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Populate your collection here from your csv by declaring a new
'person for each row of your file, and then adding it to the
'collection.
End Sub
Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
Search_For_Name()
End Sub
Private Sub ComboBox2_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedValueChanged
Search_For_Name()
End Sub
Private Sub Search_For_Name()
For Each P As Person In MyCollection
If P.ComboBox1Value = ComboBox1.Text And P.ComboBox2Value = ComboBox2.Text Then
TextBox1.Text = P.Name
Exit For
End If
Next
End Sub
End Class

Related

parse value from datagrid to button name

I'm building a form that has many buttons, all buttons do the same thing: add 1 every time they are clicked. Every pressed button is sent to a datagridview along with the time they are pressed. Datagrid values look like this:
a_1_serv (button name), 18:05:00(time).
Sometimes I want to delete the last row. Everything works fine so far.
When I delete the last row, I want to change the text of the button (a_1_serv).
I can parse the dgv value (a_1_serv) to a variable but I can't bind it to the appropriate button name so I can control it.
Is there a way to do it?
Don't store your program state in your UI
Create a data structure to hold the information, and let the DataGridView be a "view", not treating it as a variable. You will save yourself headaches vs using the UI as a variable.
That said, create a class to represent your information
Public Class Data
Public Sub New(button As Button, time As DateTime)
Me.Button = button
Me.Time = time
End Sub
<System.ComponentModel.Browsable(False)>
Public Property Button As Button
Public ReadOnly Property Text As String
Get
Return Button.Name
End Get
End Property
Public Property Time As DateTime
End Class
And your code can manipulate the data in a variable off the UI. Bind the data to the DataGridView for display.
Private datas As New List(Of Data)()
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
addButton(DirectCast(sender, Button))
End Sub
Private Sub RemoveLastButton_Click(sender As Object, e As EventArgs) Handles RemoveLastButton.Click
removeLast()
End Sub
Private Sub addButton(b As Button)
datas.Add(New Data(b, DateTime.Now))
bindData()
End Sub
Private Sub removeLast()
Dim b = datas.Last.Button
b.Text = "new text" ' change to whatever
datas.RemoveAt(datas.Count - 1)
bindData()
End Sub
Private Sub bindData()
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = datas
End Sub
This does exactly what you stated but there may be inconsistency in these two bits of information you provided: a_1_serv (button name) and I want to change the text of the button .... This changes the button text but not the name. The name is displayed in the grid. You can change the data class to display the text or whatever. But the point is this approach will keep your data off the UI and you won't need to look up the control by name anymore.

Get Object from a Collection to another form

In my second Form, I have a Collection of an Object which I add when I need.
When I want to access to the first Object in that Collection for example, from another Form. It returns me nothing: when I do form2.list.Count, I have 0. What I understood from this, is the Collection is Null.
But in the second form (the Form where I created that Collection), if I do form2.list.Count, I have the correct count and I have access to all the data.
I made sure that Collection was Public but still, I don't know where is the problem.
How can I solve this ?
Edit:
Public Class Form2
Public mycarlist As New carlist
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
dim mycar as car
'I have other textbox but for simplicity, I only put one
mycar = new car(Me.TextBox1.Text)
mycarlist.Add(mycar)
' Here everything works, I have the correct count and brand
Form1.TextBox1.Text = mycarlist.count
Form1.TextBox2.Text = mycarlist(0).brand
Me.Close()
End Sub
End Class
And it's from this Form (Form1) that I want to access to this list:
Public Class Form1
Private Sub testbt_Click(sender As Object, e As EventArgs) Handles tesbt.Click
Me.TextBox3.Text = Form2.mycarlist.Count
'For example a Property that I want to print
Me.TextBox4.Text = Form2.mycarlist(0).Brand
End Sub
Private Sub openform2_Click(sender As Object, e As EventArgs) Handles openform2.Click
Form2.Show()
End Sub
End Class
In the Form1, the TextBox1 returns me 0
And when the program will execute this line Me.TextBox2.Text = Form2.mycarlist(0).Brand, I have this error:
System.ArgumentOutOfRangeException from carlist.
Your assertion that the collection is null is incorrect because otherwise you'd receive a NullReferenceException to the effect of:
Run-time exception (line -1): Object reference not set to an instance of an object.
VB.NET does some funky stuff when it comes to accessing forms. My guess is that you're attempting to get the wrong instance of Form2 and VB.NET is creating a new instance of the Form behind the scenes.
Could you show how it is that you're creating Form2? It may be as simple as referencing the correct instance of the Form. E.g.:
Private frm2 As Form2 = New Form2()
' user adds item(s) to list
frm2.list.Count

When a ComboBox value is selected, output particular text (Visual Basic)

I'm creating a decision interface for clinical support with numerous amounts of combo boxes with boolean values 'Yes|No". However I want it so if the user choses either yes or no, a button can be clicked at the bottom and then another windows form appears and says whether they have cancer or not.
For example, if the user clicks 'yes' in the comboBox and then clicks the 'submit' button, another form will appear and the text box will say whether they have cancer or not. Would anyone be able to supply an example of how this would work? I have the form with the combo boxes, a button that links to another form, and a textbox inside the second form....but I can't get the information between the two forms.
The code I have at the moment is
Private Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
If (RectalBleeding.SelectedItem = "Yes") Then Outcome.OutcomeBox.Text = "you have cancer" End If Outcome.Show()
End Sub
Particular combo box im trying to link is called 'RectalBleeding'. The button on the decision interface form is called 'submit' the second form is called 'outcome'. The box inside outcome is called 'outcomeBox' I want it so that if 'Yes' was chosen in the comboBox, the user clicks 'submit' second form appears and in the text box it says "you have cancer"
Thanks!
You can use public properties and constructors to pass values between two forms. Below is some code that uses these tools for your purpose.
Here is the code for the main form:
Public Class Form1
Private frm As Outcome
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddDecision()
End Sub
Private Sub AddDecision()
RectalBleeding.Items.Add("Yes")
RectalBleeding.Items.Add("No")
End Sub
Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
If RectalBleeding.Text = "Yes" Then
frm = New Outcome("You have cancer")
Else
frm = New Outcome("You don't have cancer")
End If
frm.Show()
End Sub
End Class
and here for the outcome form
Public Class Outcome
Private _Decision As String
Public Property Decision() As String
Get
Return _Decision
End Get
Set(ByVal value As String)
_Decision = value
End Set
End Property
Private Sub Outcome_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillTextBox()
End Sub
Public Sub New(ByVal results As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Decision = results
End Sub
Private Sub FillTextBox()
outcomeBox.Text = Me.Decision
End Sub
End Class

Cannot get decimal entries from array to appear in listbox

In my VB class we've been asked to set up an array which is populated by user entries. These entries are decimal type, meant to be gas prices. There are twelve, one per month. The entries are supposed to show up one at a time, as they are entered and processed, in a list box.
I've got them showing up but they're not showing up properly. Instead of 4.55 (or whatever), the entries show up as "Decimal[] Array" (minus the quotation marks, of course).
How can I get the entries to show up properly? The code is below and it's very incomplete as I'm only about a third of the way into the project, so don't sweat that unless you see some horrible problem sticking out like a sore thumb.
Public Class GasPrices
Dim prices(11) As Decimal
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
prices(PriceList.Items.Count) = Convert.ToDecimal(PriceText.Text)
PriceText.Clear()
For i = 0 To 11
prices(i) = i
Next i
PriceList.Items.Add(prices)
End Sub
Private Sub PriceList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PriceList.SelectedIndexChanged
PriceList.Items.Clear()
PriceList.Items.Add(prices)
End Sub
End Class
You're adding the entire array as one "entry". You'd need to add each individual entry instead using syntax like you've got where you access prices(i) in the loop.
Public Class GasPrices
Private prices(11) As Decimal
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
If PriceList.Items.Count < 12 Then
Dim price As Decimal
If Decimal.TryParse(PriceText.Text, System.Globalization.NumberStyles.Currency, Nothing, price) Then
prices(PriceList.Items.Count) = price
PriceList.Items.Add(price)
PriceText.Clear()
PriceText.Focus()
Else
MessageBox.Show("Invalid Price!")
End If
Else
MessageBox.Show("12 entries have already been entered!")
End If
End Sub
Private Sub PriceList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles PriceList.SelectedIndexChanged
If PriceList.SelectedIndex <> -1 Then
Label1.Text = PriceList.SelectedItem
End If
End Sub
End Class
It's quite simple. With Add you add the array itself as single object to the listbox. The listbox's default behavior is to show object.ToString for each entry. And since the object is an array of decimals, you get the unwanted output.
If you want to add the elements of an array to a list, listbox, ... you use the AddRange method instead.

How to pass data from one form to another using a class (VB.Net)

In my main program (form) I have two list boxes, one text box, and a button.
When I pick two items in each list boxes and enter a number in the text box, it is suppsoed to be stocked in an array. I wanted to do this using a class. (I just asked a question about this, it works well now). The problem is I want to show the results in a different form.
The code in my class looks like this:
Public Class Stocking
Public sale(3, 4) As Integer
Public numberSellers(3) As Integer
Public numberProducts(4) As Integer
Public Sub addItem(ByRef my_sellerListBox As ListBox, ByRef my_productListBox As ListBox, ByRef my_saleTextBox As TextBox)
Dim sellerLineInteger As Integer
Dim productColumnInteger As Integer
sellerLineInteger = my_sellerListBox.SelectedIndex
productColumnInteger = my_productListBox.SelectedIndex
' add in two dimensional array
If sellerLineInteger >= 0 And productColumnInteger >= 0 Then
sale(sellerLineInteger, productColumnInteger) = Decimal.Parse(my_saleTextBox.Text)
End If
my_saleTextBox.Clear()
my_saleTextBox.Focus()
For sellerLineInteger = 0 To 3
For productColumnInteger = 0 To 4
numberSellers(sellerLineInteger) += sale(sellerLineInteger, productColumnInteger)
Next productColumnInteger
Next sellerLineInteger
End Sub
Public Sub showItems(ByRef my_label)
my_label.Text = numberSellers(0).ToString 'using this as a test to see if it works for now
End Sub
End Class
My main form looks like this:
Public Class showForm
Public sale(3, 4) As Integer
Public numberSellers(3) As Integer
Public numberProducts(4) As Integer
Dim StockClass As New Stocking
Public Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
StockClass.addItem(sellerListBox, producttListBox, saleTextBox)
End Sub
Public Sub SalesByMonthToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesByMonthToolStripMenuItem.Click
saleForm.Show()
And in my second form, to show the results stocked in the array is:
Public Class saleForm
Dim StockClass As New Stocking
Public Sub saleForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StockClass.showItems(Label00)
'Only using one label as a test for now.
End Sub
End Class
End Sub
I tested it and tried to see if the results shows on the main form, it does. So I'm guessing the problem is because I use a different form. Also I think it might be because I call the class again in my different form and doesn't keep the data.
The problem is that your saleForm is instantiating a new Stocking object. You need to send the Stocking object that is created in your primary form to the new form, during the creation of saleForm, or you need to make the Stocking object in your main form publically available, perhaps through a property.
So, in your main form, you might have something like this:
Public StockClass As New Stocking
then, because it's not protected as a private variable, you could access it from your secondary form through something like
showForm.StockClass.showItems(Label00)
The danger, of course, is that this tightly binds the two forms together. It would be better, in the long run, to learn how to send the StockClass that is populated in the first form to the second form during initialization, but I don't remember enough on WinForms development to help with that, sorry.