Results 1 to 23 of 23

Thread: How to SET the value of a Listview / Combobox item?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Question 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.

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: [2005] How to SET the value of a Listview / Combobox item?

    Quote 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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim people As New List(Of Person)
    2. Dim person As New Person
    3.  
    4. person.Name = "John Smith"
    5. person.PhoneNumber = "5551234"
    6.  
    7. people.Add(person)
    8.  
    9. Me.ListBox1.DisplayMember = "Name"
    10. 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:
    1. DirectCast(Me.ListBox1.SelectedItem, Person).PhoneNumber = "5559876"
    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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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.

  6. #6
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: [2005] How to SET the value of a Listview / Combobox item?

    Quote 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:
    1. Dim people As New List(Of Person)
    2. Dim person As New Person
    3.  
    4. person.Name = "John Smith"
    5. person.PhoneNumber = "5551234"
    6.  
    7. people.Add(person)
    8.  
    9. Me.ListBox1.DisplayMember = "Name"
    10. 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:
    1. DirectCast(Me.ListBox1.SelectedItem, Person).PhoneNumber = "5559876"
    jmc has hypothetically stating. You would have to create the "person" class with the two properties.

  7. #7
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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:
    1. Dim people As New List(Of Person)
    2.       Dim person As New Person
    3.  
    4.       person.Name = "John Smith"
    5.       person.PhoneNumber = "5551234"
    6.  
    7.       people.Add(person)
    8.  
    9.       Me.ListBox1.DisplayMember = "Name"
    10.       Me.Listbox1.ValueMember = "PhoneNumber"
    11.       Me.ListBox1.DataSource = people

    then you can just get the phone number by calling
    vb.net Code:
    1. 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:
    1. Public Class Form1
    2.  
    3.     Private Class Person
    4.         Private _Name As String
    5.         Private _PhoneNumber As String
    6.  
    7.         Public Property Name() As String
    8.             Get
    9.                 Return _Name
    10.             End Get
    11.             Set(ByVal value As String)
    12.                 _Name = value
    13.             End Set
    14.         End Property
    15.  
    16.         Public Property PhoneNumber() As String
    17.             Get
    18.                 Return _PhoneNumber
    19.             End Get
    20.             Set(ByVal value As String)
    21.                 _PhoneNumber = value
    22.             End Set
    23.         End Property
    24.     End Class
    25.  
    26.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.         Dim people As New List(Of Person)
    28.         Dim person As New Person
    29.         person.Name = "John Smith"
    30.         person.PhoneNumber = "555-125-4456"
    31.  
    32.         people.Add(person)
    33.  
    34.         Me.ListBox1.DisplayMember = "Name"
    35.         Me.ListBox1.ValueMember = "PhoneNumber"
    36.         Me.ListBox1.DataSource = people
    37.  
    38.  
    39.     End Sub
    40.  
    41.     Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
    42.         MessageBox.Show(Me.ListBox1.SelectedValue)
    43.     End Sub
    44.  
    45. End Class
    Last edited by bmahler; Aug 13th, 2007 at 12:28 PM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: [2005] How to SET the value of a Listview / Combobox item?

    Thanks bmahler. It´s working

    So many code for such a easy thing.
    Last edited by belinea2003; Aug 13th, 2007 at 12:48 PM.

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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.

  10. #10
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: [RESOLVED] How to SET the value of a Listview / Combobox item?

    PERFECT !!!!

    Many, many, many, many thanks.

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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?

  13. #13
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: [RESOLVED] How to SET the value of a Listview / Combobox item?

    Thanks.

  15. #15

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: [RESOLVED] How to SET the value of a Listview / Combobox item?

    Can I sort the people array list by name?

    people.sort()
    doesn´t work. It stops with error "InvalidOperationException"
    Last edited by belinea2003; Nov 4th, 2007 at 04:00 PM.

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: [RESOLVED] How to SET the value of a Listview / Combobox item?

    No idea?

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: How to SET the value of a Listview / Combobox item?

    Really no way to sort the people alphabetically ?

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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...

  19. #19
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: How to SET the value of a Listview / Combobox item?

    Quote 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!

    Quote 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.

  21. #21
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  22. #22

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Re: How to SET the value of a Listview / Combobox item?

    Quote 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.

  23. #23
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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