Retrieve a object from a list of objects in visual basic and use it to fill text/combo boxes - vb.net

I have a class as seen below:
Public Class parameters
Public Property test As String
Public Property test_type As String
Public Property user_test_name As String
Public Property meas As String
Public Property spec As String
...etc
End Class
I make a list of objects that I import from a csv somewhere. The user_test_name's from the list gets sent to a list box:
For Each parameters In param
' MsgBox(parameters.user_test_name)
ListBox1.Items.Add(parameters.user_test_name)
Next
now when the user selects something from the list i want the rest of the properties of that particular user_test_name object to populate in certain text/combo boxes in the application. Here is how I grab what is selected.
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim selected_name As String = ListBox1.SelectedItem()
' MsgBox(selected_name)
find_object_by_user_test_name(selected_name)
End Sub
Now i'm having difficulty finding the object with the selected_name from the list and using its properties to fill the text.combo boxes. I tried the following to no success:
Public Sub find_object_by_user_test_name(ByVal description)
MsgBox(description)
Dim matches = From parameters In param
Where parameters.user_test_name = description
Select parameters
' MsgBox(matches)
' MsgBox(matches.user_test_name)
TextBox1.Text = matches.test
TextBox2.Text = matches.test_name
etc,,, on and on
' populate_area(matches)
End Sub

Instead of adding the name (a string) to the ListBox, add your actual INSTANCE to it.
First, override ToString() in your class so that it displays properly in your ListBox:
Public Class parameters
Public Property test As String
Public Property test_type As String
Public Property user_test_name As String
Public Property meas As String
Public Property spec As String
Public Overrides Function ToString() As String
Return user_test_name
End Function
End Class
Next, add each instance to the ListBox:
For Each parameters In param
ListBox1.Items.Add(parameters)
Next
Now, the SelectedIndexChanged() event, you can cast the SelectedItem() item back to parameters and you already have everything at your disposal:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex <> -1 Then
Dim P As parameters = DirectCast(ListBox1.SelectedItem, parameters)
' ... do something with "P" ...
Debug.Print(P.user_test_name & " --> " & P.test)
End If
End Sub

If the user_test_names are unique, it may be easier to use a dictionary and retrieve the objects that way.
Dim params As New Dictionary(Of String, parameters)
params.add(MyParameterObject.user_test_name, MyParameterObject)
Then
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim selected_name As String = ListBox1.SelectedItem()
Dim selected_param as parameters = params(selected_name)
End Sub

Something like this should do it:
Public Sub find_object_by_user_test_name(ByVal description As String)
' assuming param is your list of parameter objects
For Each p In param
If p.user_test_name = description Then
TextBox1.Text = p.test
TestBox2.Text = p.test_name
...
Exit For
End If
Next
End Sub
A few notes about your code. First, you can't allow two objects to have the same user_test_name. Second, you can't use parameters as a variable name if you already have a class called parameters in your current namespace (which you do).

There is a simpler solution than any of these--just do the following:
Dim i as Integer = ListBox1.SelectedIndex
Then you can use i as an index to your original list of objects.

Related

VB.NET Search ListBox for string and return specific data

I have a populated listbox. Each item has a string of data with id's and values. How would i search for the id and receive the vale?
If i search for 'itemColor' i would like it to return each boot color in a new msgbox.
itemName="boots" itemCost="$39" itemColor="red"
itemName="boots" itemCost="$39" itemColor="green"
itemName="boots" itemCost="$39" itemColor="blue"
itemName="boots" itemCost="$39" itemColor="yellow"
I understand there are different and easier ways to do this but i need to do it this way.
Thanks!
Here's one way to do it involving parsing the text as XML:
' Here's Some Sample Text
Dim listboxText As String = "itemName=""boots"" itemCost=""$39"" itemColor=""red"""
' Load XML and Convert to an Object
Dim xmlDocument As New System.Xml.XmlDocument
xmlDocument.LoadXml("<item " & listboxText & "></item>")
Dim item = New With {.ItemName = xmlDocument.DocumentElement.Attributes("itemName").Value,
.ItemCost = xmlDocument.DocumentElement.Attributes("itemCost").Value,
.ItemColor = xmlDocument.DocumentElement.Attributes("itemColor").Value}
' Write It Out as a Test
Console.WriteLine(item.ItemName & " " & item.ItemCost & item.ItemColor)
Console.Read()
Create a class (or structure), the appropriate properties, a default constructor, a parametized constructor and an Override of .ToString.
Public Class Item
Public Property Name As String
Public Property Cost As String
Public Property Color As String
Public Sub New()
End Sub
Public Sub New(sName As String, sCost As String, sColor As String)
Name = sName
Cost = sCost
Color = sColor
End Sub
Public Overrides Function ToString() As String
Return $"{Name} - {Cost} - {Color}"
End Function
End Class
Item objects are added to the list box calling the parameterized constructor. The list box calls .ToString on the objects to determine what to display.
Private Sub FillList()
ListBox3.Items.Add(New Item("boots", "$39", "red"))
ListBox3.Items.Add(New Item("boots", "$39", "green"))
ListBox3.Items.Add(New Item("boots", "$39", "blue"))
ListBox3.Items.Add(New Item("boots", "$39", "yellow"))
End Sub
Since we added Item objects to the list box, we can cast each list item back to the Item type and access its properties.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For Each i In ListBox3.Items
sb.AppendLine(DirectCast(i, Item).Color)
Next
MessageBox.Show(sb.ToString)
End Sub

VB.Net Passing values to another form

I would like to know how to pass a value from form1 to another form's public sub.
The problem is that it says "it is not accesible in this context because it is 'Private'."
I've tried changing Form 1 Private Sub to Public Sub but the same error remains. How should i make it work?
Public Class Form1
Dim test(), text1 As String
Const asd = "abcabc"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
text1 = Space(LOF(1))
test = Split(text1, asd)
HOST = test(1)
End Sub
And i want to pass HOST = test(1) value to another form
Public Class Form2
Public Sub Check()
'get the value to here
End Sub
You could pass it as a parameter:
Public Sub Check(valueToCheck as String)
'get the value to here
End Sub
Or create a property on form2 to receive it:
private _HostOrSomething As String = ""
Friend Property HostOrSomething As String
Get
Return _HostOrSomething
End Get
Set(ByVal value As String)
_HostOrSomething = value
End Set
In which case, Sub Check could use _HostOrSomething since it is local var. To use these:
HOST = Test(1)
frm2.Check(HOST)
or
HOST = Test(1)
frm2.HostOrSomething = HOST
frm2.Check
You can use global variables to pass data from one from to another
Dim A As New Integer= 10
Here how you declare the global The class can be define anywhere in the application.
Public Class GlobalVariables
Public Shared INTver As Integer
End Class
And how you use global variable to store the answer is here
GlobalVariables.INTver= A
put this lines in your "privet sub" and you can access the variable to any of your form that is in your WINDOWS application.

retrieve data out of a public structure and collection

I'm using a collection and structure to store some parsed data in a class, but when i try to retrieve the data it is null. On form1 i'm i can get the response string which is all the raw data. Am I adding the parsed data to the collection correctly? Did I call it on form1 correctly?
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
Dim dloader as new Downloader(blah)
'this does not work
Dim test as new _ListInfo
msgbox(test.Name) ' produces an empty message box
'this works
msgbox(dloader.Download)
End Sub
Here is my code for the class:
Public Structure _Info
Dim Name As String
End Structure
Public Class Downloader
Dim _ListCollection As New Collection(Of _ListInfo)
Public ReadOnly Property ListCollection() As Collection(Of _ListInfo)
Get
ListCollection = _ListCollection
End Get
End Property
Public Function Download() As String
'doing the download
ParseList()
Return _ResponseString
End Function
Private Sub ParseList()
_ListCollection.Clear()
Dim Name As String
Dim MyInfo As _ListInfo
MyInfo.Name = Name
_ListCollection.Add(MyInfo)
End Sub
Why do you expect it to work? You are just newing-up a structure and accessing a property. Don't you want to do something like:
Dim dloader as new Downloader(blah)
dloader.Download()
' Show first name.
MsgBox(dloader.ListCollection(0).Name)

Using the Content of Textbox as a variable in VB (2012)

In the code below b textbox will contain the string "a.text" what I want b textbox to be the evaluation of the content of the string "a.text" which is the word Test. Please don't suggest:
b.text = a.text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As String
a.Text = "Test"
t = "a.text"
b.Text = t
End Sub
End Class
Check out Controls collection of your form. You can find an item based on its name.
Also check out this answer
VB .NET Access a class property by string value
So, you could take your string, split it by the ".", find the control using the Controls Collection, and then get the property using the second half of your string using Reflection.
Of course, if you are just looking for the Text of the textbox, you just need to use the collection and forget the reflection. Like this..
For i As Integer = 1 To 25
.fields("Field" & i).value = Me.Controls("QAR" & i).Text
Next
You can do what you asked for using Reflection ... I'm not an enormous fan of it for something like this, but here's how it would look in your code:
Imports System.Reflection
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a.Text = "Hello"
Dim t As String = "a.Text"
b.Text = DirectCast(SetValue(t), String)
End Sub
Public Function SetValue(ByVal name As String) As Object
Dim ctrl As Control = Me.Controls(name.Split("."c)(0))
Return ctrl.GetType().GetProperty(name.Split("."c)(1)).GetValue(ctrl, Nothing)
End Function
End Class
This would set textbox a's value to "Hello" and then copy that to textbox b using the reflection method.
Hope this helps!

Bind a Class Property that returns a list to a DataGridViewComboBoxColumn

I have a file name that is found at multiple paths. I want to present this data to the user in the format of a DGV. I have a DGV with one text box column for the name, and another combobox column for the paths. I am having trouble getting the combobox column to bind to the property of the class that returns the paths.
Any help would be much appreciated.
Public Class fileTest
Public Property FileName As String
Public Property Paths As String()
Public Sub New(ByRef _name As String, ByVal _paths As String())
Me.FileName = _name
Me.Paths = _paths
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileList As New BindingList(Of fileTest)
fileList.Add(New fileTest("TEST", {"ABC", "123"}))
Me.DataGridView1.AutoGenerateColumns = False
Me.DataGridView1.DataSource = fileList
Me.DataGridView1.Columns("FileName").DataPropertyName = "FileName"
CType(Me.DataGridView1.Columns("Paths"), DataGridViewComboBoxColumn).DataPropertyName = "Path"
End Sub
DataGridViewComboBoxColumn is horrible. HORRIBLE.
We eventually gave up using it and now just float a standard Combobox over the grid.