-
Feb 20th, 2018, 09:01 AM
#1
Thread Starter
New Member
ListBox items to textbox
Could You help me with this?
I have ListBox which contains:
Point1: 25.00
Point2: 28.00
Point3: 36.00
Point1: 26.00
Point2: 29.00
I am receiving this data from lan so it's refreshing every 5 seconds - adding me new items under. (old items stays on the list, like it is above) In total I'll have more then 100 items.
Is it possible to loop the check if listbox contains "Point1" and write value in textbox? Of course if new value apears under, put new value in textbox.
I tried:
Dim i As Integer = ListBox1.FindString("Point1:")
Do Until i = ListBox1.Items.Count
TextBox1.Text = ListBox1.Items(i)
Loop
but it doesn't work.
-
Feb 20th, 2018, 10:14 AM
#2
Re: ListBox items to textbox
vb.net Code:
For Each item In Me.ListBox1.Items.OfType(Of String) If item.Contains("Point1:") Then Me.TextBox1.Text = item Next
-
Feb 20th, 2018, 07:06 PM
#3
Re: ListBox items to textbox
Originally Posted by Arve K.
vb.net Code:
For Each item In Me.ListBox1.Items.OfType(Of String) If item.Contains("Point1:") Then Me.TextBox1.Text = item Next
That's not really an appropriate use of the OfType method. It is intended as a filter by type, so you'd call it on a list that can contain items of various types when you only want the items of one type, e.g. if you want only Buttons from the Controls collection of a form. In this case, it is the Cast method that is more appropriate, because it is intended to get every item but as a reference of a particular type. That said, you don't need any method if you use a loop, because you can simply declare the loop control variable as the type you want:
vb.net Code:
For Each item As String In Me.ListBox1.Items
That loop is rather inefficient though, because you're going to loop through every item and update the TextBox multiple times when what you actually want is the last matching value. I'd suggest that there should be no need to touch the ListBox at all. We don't know how the ListBox is being populated with the initial or new data but I'd suggest that you should check the data before it's added to the ListBox and if the data contains "Point 1:" then update the TextBox. What's the point in going back over old data or updating the TextBox if there's no new matching data?
-
Feb 25th, 2018, 12:03 PM
#4
Thread Starter
New Member
Re: ListBox items to textbox
Thank you Arve K. code is working!
jmcilhinney I don't mind what's going with old data, I need only updated info and then do mathematical stuff with this.
I want to make this fully auto. When it's new info going from Lan, it's going to ListBox (where all listened things from sockets are put) then write info to textbox (if updated) and then make a calculation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|