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
Related
I'm using the VB.net forms application for this project.
so I have a text file like this
7,John,Kimberlake,john#mail.com,27,Bachelor
8,Tiny,Down,tiny#mail.com,34,Master
9,Jeniffer,Kime,Jen#mail.com,22,None
I have 1 textbox and 1 button.
The purpose is that you need to fill an id number to find the data about the person.
Dim Findstring = IO.File.ReadAllText("data.txt")
Dim Data As String = TextBox1.Text
Dim aryTextFile() As String
aryTextFile = Findstring.Split(",")
If aryTextFile.Contains(Data) Then
End If
I tried this and something like finding the index number in the array of the requested id but it didn't work.
Instead of ReadAllText use ReadLines and loop each line to get the data.
Walk through below code and comments.
There are much better ways to do this, But the below is very basic way of doing for easy understanding.
'READ EACH LINE OF THE TEXT FILE.
For Each item As String In IO.File.ReadLines("C:\\Desktop\\test.txt") 'ENSURE VALID PATH HERE
'THIS IS EACH LINE.
Dim Findstring = item
'ASSUME THIS IS TEXT ID FROM TEXT BOX.
Dim ID As String = "8"
'SPLIT THE LINE BASED ON ","
Dim aryTextLine() As String
aryTextLine = Findstring.Split(",")
'NOW YOU HAVE ARRAY TO READ EACH ITEM.
If aryTextLine(0) = ID Then
Dim name = aryTextLine(1)
End If
Next
Im making a program where I want to be able to paste in my email list, then search through the list and compare them to a list of strings in another textbox, and if any of it matches, then the email address gets moved to a third textbox.
For example, if I wanted to filter all the gmails and hotmails, I would enter them in the box, paste the emails in the other box and click go.
But it doesn't seem to work properly, with only a few entries it seems to work fine, but if i paste more than a few emails it only seems to detect gmails(or whatever the first entry i have in the compare textbox).
I hope this makes sense I can't figure out why it wont work.
Here is my code
Dim compare As String
Dim comparear() As String
Dim list As String
Dim listar() As String
compare = txtcompare.Text
comparear = compare.Split(vbNewLine)
list = txtlist.Text
listar = list.Split(vbNewLine)
For i = 0 To comparear.Length - 1
For p = 0 To listar.Length - 1
If listar(p).Contains(comparear(i)) Then
txtresult.Text = txtresult.Text & listar(p)
Else
End If
Next
Next
Replace this line:
comparear = compare.Split(vbNewLine)
...
listar = list.Split(vbNewLine)
With this:
comparear = compare.Split(",")
...
listar = list.Split(",")
If you split the search criterias into commas(,)
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
I'm creating a program to calculate the average. There are 12 TextBox and I want to create the possibility to leave some fields blank. Now there are only errors and the crash of the program. Is possible to create that?
This is part of code:
ItalianoScritto = (TextBox1.Text)
MatematicaScritto = (TextBox2.Text)
IngleseScritto = (TextBox3.Text)
InformaticaScritto = (TextBox4.Text)
ScienzeScritto = (TextBox5.Text)
FisicaScritto = (TextBox6.Text)
MediaScritto = (ItalianoScritto + MatematicaScritto + IngleseScritto + InformaticaScritto + ScienzeScritto + FisicaScritto) / 6
Label10.Text = Str(MediaScritto)
If i leave blank the textbox1 when I click on the button to calculate the average Vb says Cast not valid from the string "" to type 'Single' and the bar of te textbox1 become yellow
I would do the following:
Iterate over the textboxes and check if you can parse the value into an iteger. If yes, add it to a value list.
Then add all values from that list and divide it by the number of cases.
It is faster than big if-statements and resilient against error
dim TBList as new list(of Textbox)
'add your textboxes to the list here
TbList.add(Textbox1)
...
dim ValList as new List(Of Integer)
for each elem in Tblist
dim value as integer
If integer.tryparse(elem.text,value)=True
ValList.add(Value)
else
'report error or do nothing
end if
next
dim Result as Integer
Dim MaxVal as Integer =0
for each elem in ValList
Maxval +=elem
next
Result = MaxVal / ValList.count
If you need support for point values, just choose double or single instead of Integer.
Also: regardless what you do -CHECK if the values in the textboxes are numbers or not. If you omit the tryparse, somebody will enter "A" and your app will crash and burn
Also: You OPTION STRICT ON!
You just have to check if the TextBox is blank on each one before using the value:
If TextBox7.TextLength <> 0 Then
'Use the value inside
End If
The way to do it depends a lot of your code. You should consider editing your question giving more information (and code) in order to us to help you better.
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