dcsimg
Results 1 to 4 of 4
  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    7

    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.

  2. #2
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    466

    Re: ListBox items to textbox

    vb.net Code:
    1. For Each item In Me.ListBox1.Items.OfType(Of String)
    2.     If item.Contains("Point1:") Then Me.TextBox1.Text = item
    3. Next
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,939

    Re: ListBox items to textbox

    Quote Originally Posted by Arve K. View Post
    vb.net Code:
    1. For Each item In Me.ListBox1.Items.OfType(Of String)
    2.     If item.Contains("Point1:") Then Me.TextBox1.Text = item
    3. 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:
    1. 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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    7

    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
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.