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
1 Attachment(s)
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.
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'. "
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.
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'?
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:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'My index has changed
Me.TextBox1.Text = GiveMeMyText(DirectCast(ListBox1.SelectedItem, String))
End Sub
Private Function GiveMeMyText(ByVal UserInfo As String) As String
Select Case UserInfo
Case "Hello"
Return "World"
Case "3"
Return "Text 3)"
Case Else
Return "Unknown!!"
End Select
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!!)
Re: Change TextBox Text On ListBox Item Change
Is it possible to do this with user specified strings that are in the ListBox? Thanks!
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?
Re: Change TextBox Text On ListBox Item Change
Er, I'm new to this. Can you please post a snippet?
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 :).
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.
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.
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?
Re: Change TextBox Text On ListBox Item Change
I hate double posts but I really need someone to respond.
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:
Public Class Form1
Private names As ArrayList
Private birthDates As ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.names = New ArrayList
Me.birthDates = New ArrayList
'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.
Me.names.AddRange(New Object() {"Peter", "Paul", "Mary"})
Me.birthDates.AddRange(New Object() {#5/23/1980#, #2/15/1974#, #8/2/1979#})
'Display the names in the ListBox.
Me.ListBox1.DataSource = Me.names
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'Display the birth date that corresponds to the selected name.
MessageBox.Show(String.Format("{0} was born on {1:d}", _
Me.ListBox1.SelectedItem, _
Me.birthDates(Me.ListBox1.SelectedIndex)))
End Sub
End Class
A more professional way would be to use an explicit relationship, e.g.
VB Code:
Public Class Form1
Private namesAndBirthDates As ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.namesAndBirthDates = New ArrayList
'Add the values with an explicit relationship between each name and the corresponding birth date.
Me.namesAndBirthDates.AddRange(New Object() {New NameBirthDatePair("Peter", #5/23/1980#), _
New NameBirthDatePair("Paul", #2/15/1974#), _
New NameBirthDatePair("Mary", #8/2/1979#)})
'Display the names in the ListBox.
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.ValueMember = "BirthDate"
Me.ListBox1.DataSource = Me.namesAndBirthDates
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'Display the birth date that corresponds to the selected name.
MessageBox.Show(String.Format("{0} was born on {1:d}", _
Me.ListBox1.GetItemText(Me.ListBox1.SelectedItem), _
Me.ListBox1.SelectedValue))
End Sub
End Class
Public Structure NameBirthDatePair
Private _name As String
Private _birthDate As Date
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
Public Property BirthDate() As Date
Get
Return Me._birthDate
End Get
Set(ByVal value As Date)
Me._birthDate = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal birthDate As Date)
Me._name = name
Me._birthDate = birthDate
End Sub
End Structure
Re: Change TextBox Text On ListBox Item Change
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.