Results 1 to 17 of 17

Thread: Change TextBox Text On ListBox Item Change

  1. #1

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Change TextBox Text On ListBox Item Change

    I have a listbox which the items are linked to values stored in the registry. Is there anyway I can change the textbox's text depending on which item is selected in the listbox. I'll do my best on an example:


    ...............Item 1 Selected - Textbox text is 1
    List Box - Item 2 Selected - Textbox text is 2
    ...............Item 3 Selected - Textbox text is 3

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    There are events with each control that can be used when something happens, an event. You will have an event for when the user changes the value of the list item. The simplist way to get to these events is just double-click the control and you will see that you are inside an event of that control. use the drop down lists at the top to change the type of event you are using.
    Attached Images Attached Images  
    Last edited by Grimfort; Jul 16th, 2006 at 05:54 PM.

  3. #3

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    Thank you. However, I am well aware of the events. The problem is that the items are user specified so I can't just say Listbox1.SelectedItem("TEXT") or whatever it is.

    Also, the code you supplied in the screenshot doesn't work (for me). I'm getting this error: "Value of type 'String' cannot be converted to 'System.Windows.Forms.TextBox'. "
    Last edited by TH3 K1D; Jul 16th, 2006 at 06:10 PM.

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    It doesnt matter what they are, my example picture included a tiny convert to a string object type command (its just example code). What exactly are you looking for, give some object examples.

    You are probly saying this:

    Text1 = dfdf not Text1.Text = dfdf

    Something like that anyway. No such thing as default items in .net, unlike VB6.

  5. #5

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    Ok, thanks. This is a start. Now, for the hard part. Is it possible to have a string linked to the listbox item so like if you click 'Hello' the textbox says 'World'?

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    Well, yes of course, there are numerous ways to do it.

    First off is just to have a Select case on the input, and provide an output:

    VB Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.  
    3.         'My index has changed
    4.         Me.TextBox1.Text = GiveMeMyText(DirectCast(ListBox1.SelectedItem, String))
    5.  
    6.     End Sub
    7.  
    8.     Private Function GiveMeMyText(ByVal UserInfo As String) As String
    9.  
    10.         Select Case UserInfo
    11.             Case "Hello"
    12.                 Return "World"
    13.             Case "3"
    14.                 Return "Text 3)"
    15.             Case Else
    16.                 Return "Unknown!!"
    17.         End Select
    18.  
    19.     End Function

    Thats a nice easy way if you are new to programming with objects. The other more usefull way is to use a collection. Now, there are various types of collections depending on version of .Net you are using.

    The easiest is probly a simple Collection object that allows the use of keys (http://www.vbforums.com/showthread.p...collection+key got 1!!)
    Last edited by Grimfort; Jul 16th, 2006 at 06:34 PM.

  7. #7

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    Is it possible to do this with user specified strings that are in the ListBox? Thanks!

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    Quote Originally Posted by TH3 K1D
    Is it possible to do this with user specified strings that are in the ListBox? Thanks!
    Again, its just data, as long as you can control some aspect of it, its no different from anything else. Of course you need to know what means what.
    What are you comparing the "user" data to? A known list or another user specified list?

  9. #9

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    Er, I'm new to this. Can you please post a snippet?

  10. #10
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    Quote Originally Posted by TH3 K1D
    Er, I'm new to this. Can you please post a snippet?
    Ive edited my post, but actually you need to supply the data so I can format it easier .

  11. #11

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    Ok, thanks again. The data is actually going to be stored in the registry so I don't guess I need to post it becuase I already know how to read/write to the registry and split strings.

  12. #12
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    I dont really need the actual data, I mean the format of your data, you still havent said what you are comparing. Is the text in the registry going to be tested against a known list (ie something you have hardcoded) or is it going to be some other data in the registry. If its hardcoded a simple select will probly work, but if its some other data you will need something like a collection.

  13. #13

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    No, it's not going to be hard coded. It's going to be strictly stored in the registry. I'm making a program where the user types in a product name, then the serial key and they save it to the registry so if they lose it, they can just open the application up and recover it.


    EDIT:

    I'm having trouble showing the serial after I save it to the registry. Do you have AIM or MSN so I can send you my project files and you can take a look?


    2nd EDIT:

    Is it possible to load up items in a ListBox from the registry?
    Last edited by TH3 K1D; Jul 16th, 2006 at 06:47 PM.

  14. #14

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    I hate double posts but I really need someone to respond.

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

    Re: Change TextBox Text On ListBox Item Change

    You merely need to create a relationship between the data in the ListBox and the data you want to use. An easy way would be to use an implicit relationship, e.g.
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private names As ArrayList
    4.     Private birthDates As ArrayList
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Me.names = New ArrayList
    8.         Me.birthDates = New ArrayList
    9.  
    10.         'Add the names and birth dates in the same order so that there is an implied relationship between the name and birth date at the same index.
    11.         Me.names.AddRange(New Object() {"Peter", "Paul", "Mary"})
    12.         Me.birthDates.AddRange(New Object() {#5/23/1980#, #2/15/1974#, #8/2/1979#})
    13.  
    14.         'Display the names in the ListBox.
    15.         Me.ListBox1.DataSource = Me.names
    16.     End Sub
    17.  
    18.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    19.         'Display the birth date that corresponds to the selected name.
    20.         MessageBox.Show(String.Format("{0} was born on {1:d}", _
    21.                                       Me.ListBox1.SelectedItem, _
    22.                                       Me.birthDates(Me.ListBox1.SelectedIndex)))
    23.     End Sub
    24.  
    25. End Class
    A more professional way would be to use an explicit relationship, e.g.
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private namesAndBirthDates As ArrayList
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.namesAndBirthDates = New ArrayList
    7.  
    8.         'Add the values with an explicit relationship between each name and the corresponding birth date.
    9.         Me.namesAndBirthDates.AddRange(New Object() {New NameBirthDatePair("Peter", #5/23/1980#), _
    10.                                                      New NameBirthDatePair("Paul", #2/15/1974#), _
    11.                                                      New NameBirthDatePair("Mary", #8/2/1979#)})
    12.  
    13.         'Display the names in the ListBox.
    14.         Me.ListBox1.DisplayMember = "Name"
    15.         Me.ListBox1.ValueMember = "BirthDate"
    16.         Me.ListBox1.DataSource = Me.namesAndBirthDates
    17.     End Sub
    18.  
    19.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    20.         'Display the birth date that corresponds to the selected name.
    21.         MessageBox.Show(String.Format("{0} was born on {1:d}", _
    22.                                       Me.ListBox1.GetItemText(Me.ListBox1.SelectedItem), _
    23.                                       Me.ListBox1.SelectedValue))
    24.     End Sub
    25.  
    26. End Class
    27.  
    28. Public Structure NameBirthDatePair
    29.  
    30.     Private _name As String
    31.     Private _birthDate As Date
    32.  
    33.     Public Property Name() As String
    34.         Get
    35.             Return Me._name
    36.         End Get
    37.         Set(ByVal value As String)
    38.             Me._name = value
    39.         End Set
    40.     End Property
    41.  
    42.     Public Property BirthDate() As Date
    43.         Get
    44.             Return Me._birthDate
    45.         End Get
    46.         Set(ByVal value As Date)
    47.             Me._birthDate = value
    48.         End Set
    49.     End Property
    50.  
    51.     Public Sub New(ByVal name As String, ByVal birthDate As Date)
    52.         Me._name = name
    53.         Me._birthDate = birthDate
    54.     End Sub
    55.  
    56. End Structure
    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

  16. #16

    Thread Starter
    Banned
    Join Date
    Jul 2006
    Location
    Atlanta, GA, USA
    Posts
    28

    Re: Change TextBox Text On ListBox Item Change

    Ok, thanks JM.

  17. #17
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Change TextBox Text On ListBox Item Change

    Quote Originally Posted by TH3 K1D
    I hate double posts but I really need someone to respond.

    Sorry, had to sleep . I hope you have got something of an answer now.

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