Add Text to start of ListBox - vb.net

I want to know how I can add TextBox text to the start of some of items in my Listbox. For example when I type 'Game 1' in the TextBox it shows that at each timing like Game 1: 00:00:00 and if I type 'Game 2' It shows it like Game 2: 00:00:00 .
I am using VB.Net, any help would be appreicated

If you are using VB.net then let's just say you want to change the last Item of your Listbox then you could have the code below:
Dim itm As String = ListBox1.Items(ListBox1.Items.Count - 1)
ListBox1.Items(ListBox1.Items.Count - 1) = TextBox1.Text + itm
If you want the selected item from the Listbox is where the value of the Textbox will be added then you could use SelectedIndex property instead like:
Dim itm As String = ListBox1.Items(ListBox1.SelectedIndex)
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text + itm

Related

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next

How to assign a word(number) from a listbox to a textbox

I have an application in VB.Net that displays the results of a math operation to a listbox. For example I have the 1 + 1 = 2.
What I'm trying to do is to have the first 2 numbers (1 and 1) copied to 2 different textboxes when listbox item is selected.
Any help would be greatly appreciated.
Thank you.
My VB.Net is a bit rusty, but something like this should do it:
In the SelectedIndexChanged event put this:
'Make sure that we have a selected item before continuing
If listBox1.SelectedIndex = -1 Then
Return
End If
'Get the text of the selected item
Dim selectedtext As String = listBox1.Items(listBox1.SelectedIndex).ToString()
'Split the item by the + and the = into an array of strings
Dim parts As String() = selectedtext.Split("+"C, "="C)
'If we have more than 2 parts
If parts.Length > 2 Then
'Define a variable for each part
Dim part1 As String = parts(0).Trim()
Dim part2 As String = parts(1).Trim()
'Make text boxes set to part1 and part2. part1 = 1, part2 = 1
End If

Select Line from listbox and then convert to string

I want to search for a small bit of text in my listbox, if it's in the listbox I want to select it, and then covert it to a string.
How do I do this?
Because I cannot find the good command to select something on a specific line!
Thanks
To select a ListBox item, set the ListBox's SelectedIndex property. So, for example:
Dim stringToFind As String = "someString"
For i As Integer = 0 To Me.MyListBox.Items.Count - 1
Dim itemAsString As String = Me.MyListBox.Items(i).ToString()
If itemAsString.Contains(stringToFind) Then
Me.MyLabel.Text = itemAsString
Me.MyListBox.SelectedIndex = i
Exit For 'If you're using a MultiSelect ListBox, you can add to Me.MyListBox.SelectedIndices and remove this line.
End If
Next

display selectedItems in a label

I need to display the selecteditems in a label
I am using VB 2005
I set the selection mode to multi-extended
It did work selecting only one item with the following code:
me.xresultslabel.text= me.xlisttextbox.text.selectedItem.tostring
But when I tried to display more than one item using the following code:
me.resultlabel.text= me.xlisttextbox.text.selectedItems, I get the following string on the label:
system.windows.forms.listbox+selectedobjetcollections.
any help will be highly appreciate
Dim collection = Me.xlisttextbox.Text.SelectedItems
Dim builder As New StringBuilder()
For i As Integer = 0 To collection.Count - 1
If i > 0 Then builder.Append(", ")
builder.Append(collection(i))
Next
Me.resultlabel.Text = builder.ToString

VB.NET..... Loop help

I am using My.Settings in visual studio 2008 to store information, for when the user runs the program again.
I have that working fine... but as I am using 12 textboxes I don't want to write...
my.settings.grade1 = textbox1.text
for each one, and I am also making calculations using the stored information, so I dont want to be writing my.settings.grade1 + my.settings.grade2 etc..
Any help welcome
Thanks =)
On your form that has the textboxes, add them to a collection or an array of textboxes when the form is initialised.
Then, iterate through the collection or array of textboxes to assign to the setting value.
If you don't want to manually code the assignment of the textboxes into the array, then in your form initialisation code, iterate through all controls on the form and check for the control type or a specfici 'Tag' you assign to each textbox, then add each textbox to the array that way.
For Each c as Control in Me.Controls
If c.Tag.ToString() = "Grade" Then
' Add Items to collection here '
End If
Next c
Have you considered using ApplicationSettings Binding to automatically bind your values to your Textboxes.Text properties. This will support 2-way binding and then all you have to do is Call Save when you close.
or you could do something like this:
given the your textboxes are named along the lines of: Grade1, Grade2, Grade3, etc.
you could store the Grades in an Array and then loop through the array:
((TextBox)form.findControl("Grade" + i.ToString())).Text = Grade(i)
Depending on your calculation, then you could also execute the calculation inside the loop.
Populate a List of grade textboxes:
'at the class level'
Public GradeBoxes(11) As TextBox
Const grade As String = "GRADE"
'when the form is created'
Dim i As Integer = 0
For Each ctr As Control In Controls
If TypeOf (ctr) Is TextBox AndAlso ctr.Name.ToUpper.StartsWith(grade) Then
i = CInt(ctr.Name.SubString(grade.Length))
If i >= 0 AndAlso i < GradeBoxes.Length Then GradeBoxes(i) = ctrl
End If
Next ctr
For Each box As TextBox in GradeBoxes
If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
box.Text = My.Settings(box.Name)
End If
Next box
Save grades:
For Each box As TextBox in GradeBoxes
If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
My.Settings(box.Name) = box.Text
End If
Next box
My.Settings.Save()
Do you mean something like?
Dim sum As Long
Dim grades(11) As Long
Dim i As Integer = 0
For Each ctr In Controls
If TypeOf (ctr) Is TextBox Then
grades(i) = CLng(ctr.Text)
sum = sum + grades(i)
i = i + 1
End If
Next