-
Apr 26th, 2016, 08:38 PM
#1
Thread Starter
PowerPoster
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
-
Apr 26th, 2016, 08:43 PM
#2
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 26th, 2016, 08:47 PM
#3
Thread Starter
PowerPoster
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
-
Apr 26th, 2016, 08:51 PM
#4
Thread Starter
PowerPoster
Re: Confused about listbox. how to read one variable at a time
Darn... I think I might see something.... brb
-
Apr 26th, 2016, 08:53 PM
#5
Thread Starter
PowerPoster
Re: Confused about listbox. how to read one variable at a time
-
Apr 26th, 2016, 08:59 PM
#6
Thread Starter
PowerPoster
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.
-
Apr 26th, 2016, 09:02 PM
#7
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 ...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 26th, 2016, 09:05 PM
#8
Thread Starter
PowerPoster
Re: Confused about listbox. how to read one variable at a time
Originally Posted by .paul.
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.
-
Apr 26th, 2016, 09:11 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 26th, 2016, 09:17 PM
#10
Thread Starter
PowerPoster
Re: Confused about listbox. how to read one variable at a time
Originally Posted by .paul.
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.
-
Apr 26th, 2016, 09:22 PM
#11
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 26th, 2016, 09:25 PM
#12
Thread Starter
PowerPoster
Re: Confused about listbox. how to read one variable at a time
Originally Posted by .paul.
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.
-
Apr 26th, 2016, 09:34 PM
#13
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:
'Assume that `people` is an array of Person objects.
'Create an array of Tuple(Of Person, String) for binding.
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
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:
Dim p = DirectCast(Me.ListBox.SelectedValue, Person)
MessageBox.Show(p.GivenName)
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.
-
Apr 26th, 2016, 09:48 PM
#14
Thread Starter
PowerPoster
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.
-
Apr 26th, 2016, 10:04 PM
#15
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.
Last edited by jmcilhinney; Apr 26th, 2016 at 10:22 PM.
-
Apr 26th, 2016, 10:08 PM
#16
Thread Starter
PowerPoster
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.
-
Apr 26th, 2016, 10:27 PM
#17
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
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.
Originally Posted by jumper77
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.
-
Apr 27th, 2016, 06:21 AM
#18
Thread Starter
PowerPoster
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.....
-
Apr 27th, 2016, 07:20 AM
#19
Thread Starter
PowerPoster
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.
-
Apr 27th, 2016, 07:54 AM
#20
Thread Starter
PowerPoster
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?
-
Apr 27th, 2016, 07:55 AM
#21
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.
-
Apr 27th, 2016, 07:58 AM
#22
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
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.
-
Apr 27th, 2016, 08:04 AM
#23
Thread Starter
PowerPoster
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.
-
Apr 27th, 2016, 08:32 AM
#24
Thread Starter
PowerPoster
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.
-
Apr 27th, 2016, 08:46 AM
#25
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
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.
-
Apr 27th, 2016, 08:48 AM
#26
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
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)
-
Apr 27th, 2016, 08:55 AM
#27
Thread Starter
PowerPoster
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.
-
Apr 27th, 2016, 09:14 AM
#28
Thread Starter
PowerPoster
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...
-
Apr 27th, 2016, 09:23 AM
#29
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
Add whatever cuss word you like here.
Fudge? Mmmmm... fuuudge... <indistinct gurgling sound>
-
Apr 27th, 2016, 09:25 AM
#30
Thread Starter
PowerPoster
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
-
Apr 27th, 2016, 09:33 AM
#31
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
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.
-
Apr 27th, 2016, 10:19 AM
#32
Thread Starter
PowerPoster
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?
-
Apr 27th, 2016, 11:01 AM
#33
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.
(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:
- 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.
- Convert the objects to ListViewItems, and stash the original object inside the .Tag property.
- Convert the objects to ListViewItems, but also maintain the objects in a parallel data structure.
- 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.
-
Apr 27th, 2016, 11:24 AM
#34
Thread Starter
PowerPoster
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
-
Apr 27th, 2016, 11:53 AM
#35
Thread Starter
PowerPoster
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.
-
Apr 27th, 2016, 11:59 AM
#36
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.
-
Apr 27th, 2016, 12:03 PM
#37
Thread Starter
PowerPoster
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
-
Apr 27th, 2016, 12:20 PM
#38
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.
-
Apr 27th, 2016, 12:25 PM
#39
Thread Starter
PowerPoster
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.
-
Apr 27th, 2016, 05:35 PM
#40
Thread Starter
PowerPoster
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.
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
|