|
-
Aug 12th, 2007, 04:17 PM
#1
Thread Starter
Member
How to SET the value of a Listview / Combobox item?
I can create a Listview with
Me.ListBox1.Items.Add("Name1")
Me.ListBox1.Items.Add("Name2")
Me.ListBox1.Items.Add("Name3")
But how can I set the values of each item?
When I select a Listview item, I want to read the value of the select item with ListBox1.SelectedValue. But I haven´t found a way to set the value of a item.
Last edited by belinea2003; Nov 8th, 2007 at 04:42 PM.
-
Aug 12th, 2007, 05:19 PM
#2
Re: [2005] How to SET the value of a Listview / Combobox item?
If it's a Listview;
ListView1.Items(0).Text = "NewString"
If it's a ListBox;
ListBox1.Items(0) = "NewString"
If it's a ComboBox;
ComboBox1.Items(0) = "NewString"
Last edited by Bulldog; Aug 12th, 2007 at 05:31 PM.
-
Aug 13th, 2007, 03:04 AM
#3
Thread Starter
Member
Re: [2005] How to SET the value of a Listview / Combobox item?
 Originally Posted by Bulldog
If it's a Listview;
ListView1.Items(0).Text = "NewString"
If it's a ListBox;
ListBox1.Items(0) = "NewString"
If it's a ComboBox;
ComboBox1.Items(0) = "NewString"
That changes only the displayed text of an item. But I need to change it´s value.
Or is it not possible to assign a value to a item ?
edit: It´s really impossible. But I think it could be done be a array. But how?
I have a listbox with Names and I must assign a telephonenumber to this entrys. So if I selected the name "Herbert Herbertson" I will get the telephon number of this selected person.
But I don´t know how to assign a telephone number to this ListBox, ComboBox entry.
Last edited by belinea2003; Aug 13th, 2007 at 03:30 AM.
-
Aug 13th, 2007, 05:09 AM
#4
Re: [2005] How to SET the value of a Listview / Combobox item?
The code you have shown in your first post simply adds strings to a ListBox. Strings don't have telephone numbers so in that sense your request doesn't make sense. If you bind a list of objects to the ListBox then you can do the sort of thing you are asking for. For instance, let's say that you have a Person class that has Name and PhoneNumber properties. You could create an array or collection of Person objects and bind them to the ListBox like this:
vb.net Code:
Dim people As New List(Of Person)
Dim person As New Person
person.Name = "John Smith"
person.PhoneNumber = "5551234"
people.Add(person)
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.DataSource = people
The list of Person objects is now bound to the ListBox and the Name of each item will be displayed. You can then change the PhoneNumber of the selected Person like so:
vb.net Code:
DirectCast(Me.ListBox1.SelectedItem, Person).PhoneNumber = "5559876"
-
Aug 13th, 2007, 11:55 AM
#5
Thread Starter
Member
Re: [2005] How to SET the value of a Listview / Combobox item?
Does not work for me. I got the error
Code:
Type 'Person' ist not defined.
Type 'Person' ist not defined.
-
Aug 13th, 2007, 12:04 PM
#6
Hyperactive Member
Re: [2005] How to SET the value of a Listview / Combobox item?
 Originally Posted by jmcilhinney
The code you have shown in your first post simply adds strings to a ListBox. Strings don't have telephone numbers so in that sense your request doesn't make sense. If you bind a list of objects to the ListBox then you can do the sort of thing you are asking for. For instance, let's say that you have a Person class that has Name and PhoneNumber properties. You could create an array or collection of Person objects and bind them to the ListBox like this:
vb.net Code:
Dim people As New List(Of Person)
Dim person As New Person
person.Name = "John Smith"
person.PhoneNumber = "5551234"
people.Add(person)
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.DataSource = people
The list of Person objects is now bound to the ListBox and the Name of each item will be displayed. You can then change the PhoneNumber of the selected Person like so:
vb.net Code:
DirectCast(Me.ListBox1.SelectedItem, Person).PhoneNumber = "5559876"
jmc has hypothetically stating. You would have to create the "person" class with the two properties.
-
Aug 13th, 2007, 12:22 PM
#7
Re: [2005] How to SET the value of a Listview / Combobox item?
Also am I wrong in thinking that you can set the ValueMember = "PhoneNumber" in this case as well. Thus removing the need to use the DirectCast line to get the phone number. like so
vb.net Code:
Dim people As New List(Of Person)
Dim person As New Person
person.Name = "John Smith"
person.PhoneNumber = "5551234"
people.Add(person)
Me.ListBox1.DisplayMember = "Name"
Me.Listbox1.ValueMember = "PhoneNumber"
Me.ListBox1.DataSource = people
then you can just get the phone number by calling
vb.net Code:
Me.ListBox1.SelectedValue
To expand a little on that here is a working example
with this example the person's name will be displayed and the phone number is the value. Double-clicking the listbox will show the selected value.
vb.net Code:
Public Class Form1
Private Class Person
Private _Name As String
Private _PhoneNumber As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property PhoneNumber() As String
Get
Return _PhoneNumber
End Get
Set(ByVal value As String)
_PhoneNumber = value
End Set
End Property
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim people As New List(Of Person)
Dim person As New Person
person.Name = "John Smith"
person.PhoneNumber = "555-125-4456"
people.Add(person)
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.ValueMember = "PhoneNumber"
Me.ListBox1.DataSource = people
End Sub
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
MessageBox.Show(Me.ListBox1.SelectedValue)
End Sub
End Class
Last edited by bmahler; Aug 13th, 2007 at 12:28 PM.
-
Aug 13th, 2007, 12:34 PM
#8
Thread Starter
Member
-
Aug 13th, 2007, 01:10 PM
#9
Thread Starter
Member
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
A last question: How can I add more persons?
HTML Code:
person.Name = "John Smith"
person.PhoneNumber = "555-125-4456
people.Add(person)
person.Name = "John Sinclair"
person.PhoneNumber = "777-125-4456
people.Add(person)
person.Name = "John Johnsen"
person.PhoneNumber = "999-125-4456
people.Add(person)
... doesn´t work.
-
Aug 13th, 2007, 01:28 PM
#10
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
If you change the person class to accept the values in the new constructor it makes it quite easy. something like so
Code:
Private Class Person
Private _Name As String
Private _PhoneNumber As String
Public Sub New(ByVal Name As String, ByVal PhoneNumber As String)
_Name = Name
_PhoneNumber = PhoneNumber
End Sub
Public Sub New()
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property PhoneNumber() As String
Get
Return _PhoneNumber
End Get
Set(ByVal value As String)
_PhoneNumber = value
End Set
End Property
End Class
You can then easily add new ones like this
Code:
Dim people As New List(Of Person)
people.Add(New Person("John Smith", "555-125-4456"))
people.Add(New Person("Joe Jones", "555-125-4587"))
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.ValueMember = "PhoneNumber"
Me.ListBox1.DataSource = people
Also notice that I overloaded the New Constructor with one that accepts nothing as parameters. This is so that you can create an instance of the class without initially speciifying the name and phone number if desired.
-
Aug 13th, 2007, 01:59 PM
#11
Thread Starter
Member
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
PERFECT !!!!
Many, many, many, many thanks.
-
Aug 29th, 2007, 08:37 AM
#12
Thread Starter
Member
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
I need help again.
With
Code:
Me.ListBox1.SelectedValue
i can get the Phone number of the selected person in the ListBox. But how can I get the Name of the person of the current selected Person?
-
Aug 29th, 2007, 12:38 PM
#13
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
use
Code:
Me.ListBox1.SelectedItem.ToString()
but you will need to override the ToString method of your class like so
Code:
Private Class Person
Private _Name As String
Private _PhoneNumber As String
Public Sub New(ByVal Name As String, ByVal PhoneNumber As String)
_Name = Name
_PhoneNumber = PhoneNumber
End Sub
Public Sub New()
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property PhoneNumber() As String
Get
Return _PhoneNumber
End Get
Set(ByVal value As String)
_PhoneNumber = value
End Set
End Property
Public Overrides Function ToString() As String
Return _Name
End Function
End Class
Last edited by bmahler; Aug 29th, 2007 at 12:47 PM.
-
Aug 29th, 2007, 06:31 PM
#14
Thread Starter
Member
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
-
Nov 4th, 2007, 03:56 PM
#15
Thread Starter
Member
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
Can I sort the people array list by name?
doesn´t work. It stops with error "InvalidOperationException"
Last edited by belinea2003; Nov 4th, 2007 at 04:00 PM.
-
Nov 8th, 2007, 04:41 PM
#16
Thread Starter
Member
Re: [RESOLVED] How to SET the value of a Listview / Combobox item?
-
Feb 7th, 2008, 12:22 PM
#17
Thread Starter
Member
Re: How to SET the value of a Listview / Combobox item?
Really no way to sort the people alphabetically ?
-
Feb 17th, 2008, 11:36 AM
#18
Thread Starter
Member
Re: How to SET the value of a Listview / Combobox item?
I have another problem.
I can select a contact in the dropdown by it it´s telephone number
Code:
Me.listbox1.SelectedValue = "555-125-4587"
But I cannot select a contact in the dropdown by it´s name.
Code:
Me.listbox1.SelectedItem = "John Doe"
When I try this, the dropdown stays at the current selected entry.
Can you help me again bmahler? Thank you...
-
Feb 19th, 2008, 01:51 PM
#19
Re: How to SET the value of a Listview / Combobox item?
That's because SelectedItem is expecting an object.... If you want to select by the name, then you need to do one of two things. 1) Set the text property of the listbox to the name you want to select (ah... it might be TextDisplayed, or something along those lines).... but I'm not 100% sure that works... the other thing to do is to loop through your collection to find the item, then set the SelectedItem to THAT....
As for sorting..... try just sorting the listbox... not the collection itself. Your people.sort isn't working because your collection doesn't know how to sort the objects because they are just that, objects. In order to sort your collection, you have to provide a way to sort it yourself. If what you are after is just getting the listbox to sort, just set the .Sorted property to true.
-tg
-
Feb 19th, 2008, 02:42 PM
#20
Thread Starter
Member
Re: How to SET the value of a Listview / Combobox item?
 Originally Posted by techgnome
1) Set the text property of the listbox to the name you want to select (ah... it might be TextDisplayed, or something along those lines).... but I'm not 100% sure that works...
IT WORKS IT WORKS IT WORKS !!!! YEEEEEEEEEEEEEEEEEEEEAAAAAAH
The 50$ are yours!
 Originally Posted by techgnome
If what you are after is just getting the listbox to sort, just set the .Sorted property to true.
The ComboDropdown hasn´t a .Sorted Property.
Last edited by belinea2003; Feb 19th, 2008 at 02:52 PM.
-
Feb 19th, 2008, 03:21 PM
#21
Re: How to SET the value of a Listview / Combobox item?
Oh.. a combobox..... not a listbox.... I'll have to get back to you on that one.
-tg
-
Feb 19th, 2008, 03:25 PM
#22
Thread Starter
Member
Re: How to SET the value of a Listview / Combobox item?
 Originally Posted by techgnome
1) Set the text property of the listbox to the name you want to select (ah... it might be TextDisplayed, or something along those lines).... -tg
This method doesn´t work when you try to select a contact that doesn´t exist in the dropdown.
If you try to select a contact with
Code:
Me.ComboDropdown.Text = "John Doe"
Visual Basic enters this name in the current selected position in the dropdown when the name is not found in the dropdown.
I think the only way to select a name is your second soluution (switch to all contacts in a loop).
Last edited by belinea2003; Feb 19th, 2008 at 03:31 PM.
-
Feb 19th, 2008, 05:29 PM
#23
Re: How to SET the value of a Listview / Combobox item?
It should... depending on the ComboBox style.... if you change it from a dropdowncombo to a dropdownlist... it should work.... but it also depends on if you want the user to be able to type into it....
-tg
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
|