Vb.net combobox name issue - vb.net

I want to concat two strings and I need the name of a combo box to be the result of concatenation. For example,
parametre = hi
parametre2 = everyone
I have put name hieveryone my combobox name
parametre3 = String.Concat(parametre,parametre2)
dim parametre3 as ComboBox
How can I do solve this problem?

You can't do this. Variable naming occurs at compile time, whereas the string concatenation occurs at execution time.
Generally, if you want to dynamically map strings to values, you should use a Dictionary(Of String, Of ComboBox) or whatever. Then you can put values into the dictionary by string key, and retrieve them later.

Dim parameter1 As New String("Hi");
Dim parameter2 As New String("everyone");
Dim combo As New ComboBox();
combo.Name = String.Concat(parameter1, parameter2);

Related

Conversion to string 'System.data.dataRowView' to type integer is invalid in vb.net with 2 dropdownlist

I have a small problem in my vb.net code. The error is
conversion to string 'System.data.dataRowView' to type integer is invalid
How can I fix it? I have 2 dropdownlist, first one; when I choose an option, all the data related to that option, should appear in the second dropdownlist. Part of my code:
Dim sb As New StringBuilder
sb.Append("select date_collected, spectral_id from Spectral_information")
sb.Append("inner join Crop on crop_id = si_crop_id")
sb.Append("where si_crop_id =")
sb.Append(CInt(crop.SelectedValue.ToString()))
Dim str As String = sb.ToString()
etc..
The line
sb.Append(CInt(crop.SelectedValue.ToString()))
seems silly doesn't it? Why convert the selected value to string, then int, then back to string in StringBuilder.Append(Integer)? If you expect an Integer there, then you have an issue when there isn't. You should address that first. Changing the line to this
sb.Append(crop.SelectedValue.ToString())
should make it work. Then you need to address the logic.

How to declare a variable using a different variable

I want to be able to allow the user to input a string and then use that string to create a different variable. For example:
Dim UserInput As String
UserInput = Console.Readline()
Dim (UserInput) As String
So if the user input "Hello" a new variable called 'Hello' would be made.
Is this possible and if not, are there any alternatives?
The names of variables must be known at compile-time. What you're asking for here is to wait until run-time to know the name. That just doesn't work
What you can do is use a Dictionary(Of String, String) to hold your data.
This code works:
Dim UserInput As String
UserInput = Console.Readline()
Dim UserInputData As New Dictionary(Of String, String)
UserInputData(UserInput) = "Some Value"
Console.WriteLine(UserInputData(UserInput))
So if you enter Bar then the dictionary has this value in it:
The bottom-line is that everything is known at compile-time, but you can store data based on the user input at run-time.
It is currently not possible to dynamically create single variables in VB.net. However, you can use lists or dictionaries (source: Dynamically create variables in VB.NET)
The name of the variable does not matter to the user because they will never see it. They will only ever see the data it holds. There really is no reason for this

VB.NET ComboBox Verify if Value Exists

I am currently working on an application that was coded in VB. I am making modifications and adding features to it.
The issue I have is that I want to run a verification check for the ComboBox based on if the value exists before I attempt to select it.
The combo box is populated from a sql query with a dictionary data source
Dim TimerComboSource As New Dictionary(Of String, String)()
TimerComboSource.Add(varSQLReader("ID").ToString, varSQLReader("Name").ToString)
'Binds the values to the comboboxes
cmbTimer.DataSource = New BindingSource(TimerComboSource, Nothing)
cmbTimer.DisplayMember = "Value"
cmbTimer.ValueMember = "Key"
I select a value from a different ComboBox which is populated with a different SQL Query/SQL Table.
When I select the second ComboBox value, the table it comes from contains the ID of the first ComboBox. I want to verify if the Value Exists in the first ComboBox before I select it.
The following does not work:
If cmbTechnician.Items.Contains(varSQLReader("Tech_ID").ToString) Then
cmbTechnician.SelectedValue = varSQLReader("Tech_ID").ToString
End If
Is there a specific way in VB to make this work without it being overly complicated? The other work around would to make a more complicated SQL query but I rather not do that if there's a simpler way.
Since you are using a BindingSource on a Dictionary, you should use the DataSource to determine of things exist. If you tried to add to cmbTimer.Items or delete from it directly, you'd get an error telling you to use the DataSource. So do the same for checking if something exists (dont use a dictionary with local scope):
' form or class level collection
Private cboSource As New Dictionary(Of String, String)
cboSource.Add("red", "red")
cboSource.Add("blue", "blue")
cboSource.Add("green", "green")
cbo.DataSource = New BindingSource(cboSource, Nothing)
cbo.DisplayMember = "Value"
cbo.ValueMember = "Key"
If cbo.Items.Contains("red") Then
Console.Beep() ' wont hit
End If
If cboSource.ContainsValue("red") Then
Console.Beep() ' hits!
End If
The suggestion in comments suggests casting the DataSource of the BindingSource back to dictionary:
Dim tempBS As BindingSource = CType(cbo.DataSource, BindingSource)
Dim newCol As Dictionary(Of String, String) = CType(tempBS.DataSource, Dictionary(Of String, String))
If newCol.ContainsValue("red") Then
Console.Beep() ' hits!
End If
It is easier and more direct to retain a reference to the dictionary, but recreating it will work.
Here are another way =>
If cmbTechnician.FindString(varSQLReader("Tech_ID").ToString) <= -1 Then
'Do Not Exist
End If
This will find the display member and will return an index of the row if there exist value, otherwise will return -1.
More Info are here=>ComboBox.FindString

VB.Net Dynamically Referencing a Variable or Control

It would take too long to try an explain the actual application I am building.
Lets just say, I have 3 textboxes on my form. I want to set each one of them to a value of the numerical index. This is how I would normally do it.
txt1.Text = "1"
txt2.Text = "2"
txt3.Text = "3"
Now, if I had 100 of these textboxes, I would want to do something more like this.
For i as Integer = 1 to 3
txt[i].Text = i
Next
Is this possible?
This is how I would do it: first, create a list or an array (depending on whether the number of textboxes is fixed or not) and add you textboxes to it:
Dim txtList as new list(of textbox)
txtList.items.add(txtBox1...
This can be done automatically using jmcilhinney's method (with the same caveats):
Dim txtList = New List(Of TextBox)(Me.Controls.OfType(Of TextBox)().ToArray())
Then you can reference it as such:
txtList(i).text
I hope that's what you were asking and that it helps :)

Populating a combobox with specific items from a text file

I have a text file with records of machines stored on it, a single record looks like this:
"1234567890","12/04/2013","Saw","Hilti","17/11/2012",#TRUE#,#FALSE#,#FALSE#,"Made odd noise when operating"
What I would like to achieve is to populate a combo box with only the first item of each record - the serial number - so as to be able to search for the machine in a form based on the selected serial number. I need it to read the file, pick out the serial number from each record, fill the combo box with them, then allow you to select the one that you want.
Thanks in advance for any help.
if you are using a CSV formatted text file you can store info in a datatable and store each datarow in your cb, and use the DisplayMember property of the combobox to show only the first column. You also need that first line has the names of the columns to achieve this.
Try this supposing the first column is called Serial:
For Each r as DataRow in table.Rows
combobox1.Items.Add(r)
Next
combobox1.DisplayMember("Serial")
If not, you can still use your streamreader to obtain each line and split it to obtain first field.
Try:
Do
Dim line As String = reader.readLine()
If line Is Nothing Then Exit Do
Dim fields as String() = Split(line, ",")
combobox1.Items.Add(fields(0))
Loop
Hope it helps. You may have to use SubString method to retire the quotes. I post it apart:
replace
combobox1.Items.Add(fields(0))
by
combobox1.Items.Add(fields(0).SubString(1,fields(0).length -1))
If it's a simple app and you're happy to use the reader mentioned above, you can also do the following. I've used Replace() instead of Substring().
Dim reader As StreamReader = New StreamReader(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "yourFile.txt"))
Dim input = "", fields() As String
While (Not reader.EndOfStream)
input = reader.ReadLine()
fields = input.Split(",")
Dim itemToAdd = fields(0).Replace("""", "")
ComboBox.Items.Add(itemToAdd)
End While