Page 1 of 2 12 LastLast
Results 1 to 40 of 56

Thread: Confused about listbox. how to read one variable at a time

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Confused about listbox. how to read one variable at a time

    The question probably sounds nuts, but I've never worked with listboxes before. This is what I need to know how to do. When you use a For Each item in listbox.Items , each "item" holds the entire string for that row.

    here's an example, if this helps
    Code:
    For Each item In lstContacts.Items
       MsgBox(item) 'entire row is output to the msgbox
    Next

    Is need a way to break down that "item" string and turn it back into fields like it was when it was entered into the listbox. It was entered using a tiny array(4).

    any help appreciated. And Please! Get me out of UI land, lol

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Confused about listbox. how to read one variable at a time

    Can you show the array you bound to the listbox and how you set the displaymember?
    Assuming you did bind it, you cast the selecteditem to the type and access the fields through that.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Hi Paul, good to hear from you. Here's my code, but you have to promise not to laugh, well, at least not "too" loud.

    Code:
      Private Sub btnListView_Click(sender As Object, e As EventArgs) Handles btnListView.Click
    
            For Each item In lstContacts.Items
                lView.lstView.View = View.Details
                Dim fixedSize As Short = 140
    
                If Not doneOnce Then
                    With lView.lstView.Columns
                        .Add("to fool First Name Column", 0, HorizontalAlignment.Left)
                        .Add("First Name", fixedSize, HorizontalAlignment.Left)
                        .Add("Last Name", fixedSize, HorizontalAlignment.Left)
                        .Add("Phone Number", fixedSize, HorizontalAlignment.Left)
                        .Add("Birthday", fixedSize, HorizontalAlignment.Left)
                    End With
                End If
                doneOnce = True 'only create columns once
    
                MsgBox(item)
                Try
                    Dim objStr(4) As String
                    objStr(1) = obj.FirstName
                    objStr(2) = obj.LastName
                    objStr(3) = obj.PhoneNumber
                    objStr(4) = obj.Birthday.ToString("d")
    
                    Dim itm As ListViewItem
                    itm = New ListViewItem(objStr)
                    lView.lstView.Items.Add(itm)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Next
            lView.ShowDialog()
        End Sub
    This is all done prior to showing the form. That part I've done before, but I don't think that I did "this" much. And no.... I don't think I'm binding to anything

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Darn... I think I might see something.... brb

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    nope, nothing

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Ok, I've found out what the problem is. I did not state what the real problem was because I was trying to find a way to fix it and I didn't know how to do that "way". What was wrong is that the same name was being entered into the Listview over and over and NOT the value you see in the "item" msgbox.

    It's the part where it says: obj.FirstName... and all the others that follow. They all repeat themselves and I know where to go to fix it. So thanks guy. Sorry to bother you.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Confused about listbox. how to read one variable at a time

    That's not a listbox!!!
    That's a listview. Each row is a listviewitem and has a subitems collection containing all of the fields.
    BTW you can't bind a listview ...

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by .paul. View Post
    That's not a listbox!!!
    That's a listview. Each row is a listviewitem and has a subitems collection containing all of the fields.
    BTW you can't bind a listview ...
    Yes, I know that's true. I'm apologize for being unclear. I'm taking items out of a ListBox and putting them into a ListView. So that's what is going on.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Confused about listbox. how to read one variable at a time

    So what exactly does the listbox item look like?
    Listboxes don't have fields, though you can bind to them

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by .paul. View Post
    So what exactly does the listbox item look like?
    Listboxes don't have fields, though you can bind to them
    Oh sure... make me show how dumb I am... but it's true. I don't know anything about listboxes. I've never used one before now. I guess I thought they could have columns or something. But now, I guess not. Maybe I should see if I can take the Listbox out all together. The Listbox wasn't exactly mine and moving the items to a listview was an afterthought.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Confused about listbox. how to read one variable at a time

    Listboxes display strings, but you can bind them to complex datasources with fields, the displaymember is the field displayed in the listbox and you can retrieve the other fields from the selecteditem property, but it looks like you are right to redesign and not use a listbox

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by .paul. View Post
    Listboxes display strings, but you can bind them to complex datasources with fields, the displaymember is the field displayed in the listbox and you can retrieve the other fields from the selecteditem property, but it looks like you are right to redesign and not use a listbox
    Ok, thanks a lot. At least I had "one" good idea <grin>. I don't think it should be too hard. Guess I won't know until I get in there. I'll let you know how things turn out.

    Appreciate the help.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    It sounds like what you have done is taken your original data and combined it in some way in order to display it in the ListBox. If that's the case then what you have is a String and you would break it up the same way you would any other String. There's no magic to it because the String is coming from a ListBox.

    If you need combined data to be displayed in the ListBox but then to be able to access individual data afterwards then you should use your original data afterwards. For instance, let's say that you have a Person class with GivenName and FamilyName properties and you need the full name displayed in the ListBox but to then access the individual names later. You might do something like this:
    vb.net Code:
    1. 'Assume that `people` is an array of Person objects.
    2.  
    3. 'Create an array of Tuple(Of Person, String) for binding.
    4. Dim items = people.Select(Function(p) Tuple.Create(p, p.GivenName & " " & p.FamilyName)).ToArray()
    5.  
    6. With Me.ListBox1
    7.     .DisplayMember = "Item2" 'The second property, i.e. the String
    8.     .ValueMember = "Item1" 'The first property, i.e. the Person
    9.     .DataSource = items
    10. End With
    The user will then see the combined names in the ListBox. When the select a name, the SelectedItem will be the corresponding Tuple(Of Person, String) that the selected String came from and the SelectedValue will be the corresponding Person object:
    vb.net Code:
    1. Dim p = DirectCast(Me.ListBox.SelectedValue, Person)
    2.  
    3. MessageBox.Show(p.GivenName)
    4. MessageBox.Show(p.FamilyName)
    Note that Tuples are objects that can hold an arbitrary number of values of arbitrary types. In this case, a Person and a String are passed to the Tuple.Create method so it returns a Tuple(Of Person, String). Basically, they save you from having to define classes of your own for one-off uses.

    EDIT: Hmmm... I must have had this thread open for a while without realising because there were a heap of new posts when I submitted mine. Anyway, maybe I helped teach you something about Tuples.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Wow jmc! I've never heard of a tuple before. And the code for it looks really good. And I like what I think you said. It's like have a class on the fly for just a little while

    Code:
    Dim items = people.Select(Function(p) Tuple.Create(p, p.GivenName & " " & 
    p.FamilyName)).ToArray()
     
    With Me.ListBox1
        .DisplayMember = "Item2" 'The second property, i.e. the String
        .ValueMember = "Item1" 'The first property, i.e. the Person
        .DataSource = items
    End With
    When I look at the code I just think it's really cool. And thanks for telling me about Tuples. That's got to be one of the wildest things I've heard about in .net. I'm gong to try and fix my problem as you suggested, but afterwards, I want to do some studying up on tuples (what a great name).

    So thanks a bunch. Heading to bed, but I'll post here in the morning and give a status report.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    You may or may not know that tuples are a mathematical concept that has been implemented in .NET programming. The name derives from the suffix for a group of items in any number greater than four, i.e. one item is a single, two is a double, three is a triple, four is a quadruple, five is a quintuple, six is a sextuple and so on.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    I have to laugh about that one. I would have never expected that's where the name came from. Sounds like a couple of software engineers had a few too many when they were trying to come up with a name

    But it "is" a good way to remember it. Anything above four. I think that should about do it.

    Well, good night. I'm really going to do it this time <grin>
    you have a great one.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    I have to laugh about that one. I would have never expected that's where the name came from. Sounds like a couple of software engineers had a few too many when they were trying to come up with a name
    Not software engineers. Mathematicians. Like I said, tuples are a mathematical concept that has simply been implemented in programming.
    Quote Originally Posted by jumper77 View Post
    But it "is" a good way to remember it. Anything above four. I think that should about do it.
    You probably already do realise this but, just to be clear, Tuples in .NET can have any number of members from one on up. Strictly speaking, a single Tuple can only have seven items but it can then refer to another Tuple which can refer to another and so on, so the number of items is theoretically limitless.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    You probably already do realise this but, just to be clear, Tuples in .NET can have any number of members from one on up. Strictly speaking, a single Tuple can only have seven items but it can then refer to another Tuple which can refer to another and so on, so the number of items is theoretically limitless.
    Morning jmc, no I didn't know that you could have any number of members. But I'm glad you told me. I'm really going to have to study up on this, because it seems like something I would really like. I need to find out all the basics, like

    • How do I declare one
    • What is it's scope


    plus a few more that I won't know about until I try to use it.
    I'm going to start searching about it now. but of course if you want to throw any scraps my way.....

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Ok jmc, (and anyone else of course) Need some help on tuples. Can't add values because all properties are read only. Here's where I'm trying to use the code. Look at the last line to see where I need the tuple to go.

    So here's the partial bit of code that should be enough for you to tell me what I'm doing wrong. Something fundamental I'm sure.

    Code:
     Dim newContact As New Contact() With {
                    .Birthday = dtpBirthday.Value,
                    .FirstName = txtFirstName.Text.Trim(),
                    .LastName = txtLastName.Text.Trim(),
                    .PhoneNumber = txtPhoneNumber.Text.Trim()
                  }
                _serial_result.Add(newContact)
                result.Add(newContact)
    
                'put things in tuple here
    Last edited by jumper77; Apr 27th, 2016 at 07:24 AM.

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    One more thing, when I try to do "tuple.create", I get an error message saying that "Create" is not a member of string, string, string, date.

    The variables are FirstName, LastName, PhoneNumber, Birthday. What am I not getting?

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    What do you want the Tuple to store? Unless you need it to store more then eight values, you would just call Tuple.Create and pass the values you want stored. The type of the Tuple created will be inferred from the values. For instance, if you passed a String, an Integer and a Date to Tuple.Create then you'd get a Tuple(Of String, Integer, Date) and the Item1, Item2 and Item3 properties would be types String, Integer and Date respectively.

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    One more thing, when I try to do "tuple.create", I get an error message saying that "Create" is not a member of string, string, string, date.

    The variables are FirstName, LastName, PhoneNumber, Birthday. What am I not getting?
    It would help if you showed the code that generates that error message. If you're getting that message then you're not calling Create on the Tuple class. It's a Shared member so the only way to call it is on the Tuple class itself, not on some object, even if that object is a Tuple itself.

  23. #23

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Sorry, I was trying hard to be specific. Let's try code. Here's what I'm doing.

    Code:
    Dim tuple As Tuple(Of String, String, String, Date)
    Then I do
    Code:
     tuple.Create(txtFirstName.Text, txtLastName.Text, txtPhoneNumber.Text, 
    dtpBirthday.Value.ToString)("d")
    Maybe this helps better.
    Last edited by jumper77; Apr 27th, 2016 at 08:16 AM.

  24. #24

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    I apologize about the number of posts, but I'm just trying to keep everyone updated. I now know how to declare a tuple. I had to search and it wasn't that easy to find. So here's the code.

    Code:
    Dim thisTuple = New Tuple(Of String)(4)
    As usual with most new things, I was trying to make it more difficult than it was.

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    I apologize about the number of posts, but I'm just trying to keep everyone updated. I now know how to declare a tuple. I had to search and it wasn't that easy to find. So here's the code.

    Code:
    Dim thisTuple = New Tuple(Of String)(4)
    As usual with most new things, I was trying to make it more difficult than it was.
    Nope. What you're doing there is creating a Tuple with just an Item1 property of type String with the value "4". If you had Option Strict On then that code wouldn't even compile because you're passing an Integer value to a String parameter.

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    Sorry, I was trying hard to be specific. Let's try code. Here's what I'm doing.

    Code:
    Dim tuple As Tuple(Of String, String, String, Date)
    Then I do
    Code:
     tuple.Create(txtFirstName.Text, txtLastName.Text, txtPhoneNumber.Text, 
    dtpBirthday.Value.ToString)("d")
    Maybe this helps better.
    Like I said, it's a Shared method of the Tuple class so you call it on the class, not an instance. If you want a Tuple(Of String, String, String, Date) then you call Tuple.Create and pass it three Strings and a Date, not four Strings.
    Code:
    Dim myTuple = Tuple.Create(txtFirstName.Text, txtLastName.Text, txtPhoneNumber.Text, dtpBirthday.Value)

  27. #27

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Add whatever cuss word you like here. This is a new project that I didn't start, so I didn't look to see of Strict was on off. But you are right. Now that I turned it on, it will not compile.

  28. #28

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Ok, I've tried some things with option strict on and I'm getting close. I have managed to get item1 through item4 not to have any red lines underneath them. But now there is a red line under Tuple. Here's the code.

    Code:
    Tuple(txtFirstName.Text, txtLastName.Text, txtPhoneNumber.Text, dtpBirthday.ToString)
    edit: thanks for all your help. don't mean to take up so much of your time...

  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    Add whatever cuss word you like here.
    Fudge? Mmmmm... fuuudge... <indistinct gurgling sound>

  30. #30

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Thank you for the chuckle. You don't know how much I needed that. Puts a smile on my face

  31. #31
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    Ok, I've tried some things with option strict on and I'm getting close. I have managed to get item1 through item4 not to have any red lines underneath them. But now there is a red line under Tuple. Here's the code.

    Code:
    Tuple(txtFirstName.Text, txtLastName.Text, txtPhoneNumber.Text, dtpBirthday.ToString)
    edit: thanks for all your help. don't mean to take up so much of your time...
    Tuple.Create, not just Tuple. Also, use the Value property of the DTP rather than the ToString method, unless you actually want a String representation of the date, in which case you should probably use the Text.

  32. #32

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Sorry it took me a minute, I killed my function so I had to back up to an earlier version. Thank God for version control. So here's a small bit of code that I've already sent to try and explain better about what I'm wanting.

    Code:
          Dim objStr(4) As String
                objStr(1) = obj.FirstName
                objStr(2) = obj.LastName
                objStr(3) = obj.PhoneNumber
                objStr(4) = obj.Birthday.ToString("d")
    Now after every one ot the objStr lines, I need to add this information in the tuple as well. Is this doable with a tuple?

  33. #33
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Confused about listbox. how to read one variable at a time

    I'm in late, and I don't think I fully understand it, but I think you've made some bad decisions that can be corrected easily.

    This looks based on what I did with the ListBox in a previous thread. Re-read it. There's four ways people tend to use the ListBox:

    1. Convert your entire object to a String, store that String in the Listbox, then when you need the object, parse the String back into the object.
    2. Maintain a parallel data structure with the same indexes as the ListBox. Store Strings in the ListBox, but objects in the data structure, and use the index to correlate.
    3. Store objects in the ListBox, using DisplayMember or .ToString() to control the String it displays.
    4. Bind a data structor to the ListBox, using DisplayMember or .ToString() to control the String it displays.


    (1) is the hardest, most convoluted, least efficient way to do things. Don't pick it.

    For (2), you already have the object in your data structure. If you want to do something for all items, do it on the data structure. Don't mess with the String unless you need to change it.

    For (3) and (4), you already have the object, so cast and use it.

    But now I have another question: the thread is about ListBox. But your code interacts with a ListView. Which one are you using? There's a pretty neat trick to associate a random object with a ListViewItem. But let's go over the ways people tend to use ListView:

    1. Convert the objects to ListViewItems, then throw away the objects. Write code that can parse all the ListViewSubItem values back to the objects when needed.
    2. Convert the objects to ListViewItems, and stash the original object inside the .Tag property.
    3. Convert the objects to ListViewItems, but also maintain the objects in a parallel data structure.
    4. ListView virtual mode.


    (1) is the hardest, most convoluted, least efficient way to do things. Don't pick it.

    (3) is just like ListBox's version of it: you keep an array or list where the indexes point to the same stuff. If you need the object, you use your data structure, not the ListView, to get it, and update the relevant items accordingly.

    (4) is super cool, but super complicated. When the ListView is in virtual mode, you keep the data structure as in (3), but handle some events that ask you to create the ListViewItems on the fly.

    (2) is the most common. There's an Object property on ListViewItem with the bizarre name .Tag. Windows Forms doesn't have a special purpose for it, it's there so you can associate any data you please with the item. 99% of the time, this is a good place to put the object that generated the ListViewItem. So, here's how I'd add one of our contacts to a ListView:

    Code:
    Dim newItem As New ListViewItem()
    newItem.Text = contact.LastName
    newItem.SubItems.Add(contact.FirstName)
    newItem.SubItems.Add(contact.Birthday.ToString("d"))
    newItem.SubItems.Add(contact.PhoneNumber)
    newItem.Tag = contact
    
    lvContacts.Items.Add(newItem)
    Then, if I just want to get at each item, say to look at the birthdays and see whose birthday is today, well, things get a little tricky because of some stupid history. The collection type that ListView has doesn't play well with a For Each loop, but a tiny bit of LINQ can help, or just use a For loop. I'm going to use a For loop to avoid discussion of imports.
    Code:
    For i As Integer = 0 To lvItems.Items.Count
        Dim theItem As ListViewItem = lvItems.Items(i)
        Dim theContact As Contact = CType(theItem.Tag, Contact)
        Dim theBirthday As DateTime = theContact.Birthday
        ...
    Next
    If you were using (3), you'd just iterate your list of contacts.

    The hard part of using a ListView is keeping the items in sync with the list. Windows Forms makes this a real booger.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  34. #34

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    lots of good stuff. let me take some time to make a good reply

  35. #35

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    I'm in late, and I don't think I fully understand it, but I think you've made some bad decisions that can be corrected easily.

    This looks based on what I did with the ListBox in a previous thread. Re-read it. There's four ways people tend to use the ListBox:

    Convert your entire object to a String, store that String in the Listbox, then when you need the object, parse the String back into the object.
    Maintain a parallel data structure with the same indexes as the ListBox. Store Strings in the ListBox, but objects in the data structure, and use the index to correlate.
    Store objects in the ListBox, using DisplayMember or .ToString() to control the String it displays.
    Bind a data structor to the ListBox, using DisplayMember or .ToString() to control the String it displays.
    Darn, lost everything and had to start over. Here's what's going on.
    • As the software comes up, DeSerailize reads the XML file
    • The contents of the file are stored in a class
    • Whe the ListBox comes up, I use the class to populate the saved information
    • When you enter a new contact and click the add button. Only the one you just created is put in the ListView
    • But it displays the one entry for as many times as there are items in the ListBox.
    • This is because of of a loop for getting all the items from the ListBox.


    Number 2 sounds like what I'm already trying to do. So I would stick with that.

    I have no knowledge of Tags whatsoever. Never used them, read about them, or knew anyone who used them. That about covers it

    Thanks for all the info. I think I'm already trying what you're suggesting. I just haven't had any luck yet

    edit: You can put all the information from the ListBox(item) in the ListView, but it will only show up in the first field.

  36. #36
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Confused about listbox. how to read one variable at a time

    "Tag" is just a name for an Object property of ListViewItem. It isn't really a concept, just a convenient way to deal with a ListView without having to manage an external collection as well. I really hate WinForms architecture.

    I can't talk about code I can't see, and so far I only have a tiny window into part of it that you've likely changed since posting!
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  37. #37

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Maybe this will give you a better idea of what's going on. I haven't cleaned it up so there are probably some test things in there that are not used right now.

    Code:
        Private Sub btnListView_Click(sender As Object, e As EventArgs) Handles btnListView.Click
    
            For Each item In lstContacts.Items
                lView.lstView.View = View.Details
                Dim fixedSize As Short = 140
    
                If Not doneOnce Then
                    With lView.lstView.Columns
                        .Add("to fool First Name Column", 0, HorizontalAlignment.Left)
                        .Add("First Name", fixedSize, HorizontalAlignment.Left)
                        .Add("Last Name", fixedSize, HorizontalAlignment.Left)
                        .Add("Phone Number", fixedSize, HorizontalAlignment.Left)
                        .Add("Birthday", fixedSize, HorizontalAlignment.Left)
                    End With
                End If
                doneOnce = True    'only create columns once
    
                'bookmark
                Dim objStr(4) As String
                objStr(1) = obj.FirstName
                objStr(2) = obj.LastName
                objStr(3) = obj.PhoneNumber
                objStr(4) = obj.Birthday.ToString("d")
    
                thisBud.Add(obj)
                Dim itm As ListViewItem
                itm = New ListViewItem(objStr)
                lView.lstView.Items.Add(itm)
            Next
    
    
            lView.ShowDialog()
        End Sub
    DeSerialize code
    Code:
     Public Function doDeserialize() As List(Of Contact)
            _serial_result = Contact.Deserialize(_xml_filename)
            Dim thisBud As New List(Of Contact)()
    
            For i = 0 To _serial_result.Count - 1
                lstContacts.Items.Add(_serial_result(i).FirstName & " " & _serial_result(i).LastName & " " &
                              _serial_result(i).PhoneNumber & " " & _serial_result(i).Birthday.ToString("d"))
            Next i
    
            Return _serial_result
        End Function

  38. #38
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Confused about listbox. how to read one variable at a time

    See, you're in case (1) for the ListBox. I'd just put the Contacts in the ListBox. Or, if I didn't want to muck with .ToString() on Contact, I'd keep a List(Of Contact) around for convenience. Anything but case (1) is fine.

    Essentially, you're deserializing Contacts so you can serialize them to a ListBox, then deserialize them back to Contacts. ("Serialization" is just a fancy word for "converting to another format".) It can be made to work, but it's very roundabout.

    I sort of want to ask why bother with both the ListBox and ListView, but I can think of a few reasons.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  39. #39

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Hey... Yep, that's what I'm doing with the listbox now. I use the class to insert contacts into the listbox. And I don't mind using ToString() I kinda like it. I used it a "tiny" bit in my last C++ job. But I've used it a ton in the VB stuff. And I just don't mind any casting stuff. If it wasn't for that, I would have made it as a C/C++ programmer

    And thanks so much for telling me about what DeSerialize and Serialize actually mean, because I didn't know. Guess I never slowed down enough to ask or search.

  40. #40

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    I have to confess, I'm loosing my mind. Let me tell you what I didn't know was happening. After a while of staring at the names in the Listbox, I started to think... that's not what's in the XML file. And sure enough, it's not. So I tried to do all kinds of things to make it read the right things. Nothing worked.

    Then I renamed the XML file to something different so the file the program was looking for wouldn't be there. But the program was not phased a bit. From somewhere it got 7 contacts and entered them into the Listbox.

    I've watched all the methods that read the file in the debugger, and NONE of them have complained about not finding the file. I absolutely have no idea what's going on.... shoot me now.

    edit: just had a thought... I might have thought of another way to go about this.
    edit2: nope, I was wrong. Still doesn't work.

    Edit3: hang on. don't write yet. I just found something out that I'll write about in a bit
    Last edited by jumper77; Apr 27th, 2016 at 05:44 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width