Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,041 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,599 people online right now. Registration is fast and FREE... Join Now!




Edit values displayed from a combo box.

 
Reply to this topicStart new topic

Edit values displayed from a combo box.

jpool
1 Feb, 2008 - 12:12 PM
Post #1

New D.I.C Head
*

Joined: 22 Jan, 2008
Posts: 14

Hi there,

I have a simple ComboBox and TextBox on my form. When you select a value from the ComboBox it displays whatever I have specified in the TextBox. Shown below:

CODE

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If ComboBox1.SelectedItem = "Some text" Then
            TextBox1.Text = "Some other text"
        End If

End Sub


What i want to be able to do (in runtime) is to change the value of what is displayed in the TextBox when the value is selected from the ComboBox. The ComboBox value will never change, but the TextBox value it relates to will. Is it possible to make the update in runtime, or will the whole thing have be done via a SQL Table or .mdb?

Thanks.

This post has been edited by Martyr2: 1 Feb, 2008 - 12:26 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Edit Values Displayed From A Combo Box.
1 Feb, 2008 - 12:32 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
So what you are asking is that can you make it so that when you click a value in the combo box have it change the textbox to some other value?

Like if I select the value "Item 1" in the combo it will print "Ketchup" and if I select "Item 2" it will print "Mustard"? So you can create a sort of topic to detail scenario. Select "Report 1" from the combo and it show details about Report 1 in the text boxes.

Am I understanding this correctly? What has me really confused is this mention of SQL table. Are you hooking this up to a database or flat file?

More info will help us out greatly. smile.gif
User is online!Profile CardPM
+Quote Post

jpool
RE: Edit Values Displayed From A Combo Box.
1 Feb, 2008 - 12:52 PM
Post #3

New D.I.C Head
*

Joined: 22 Jan, 2008
Posts: 14

Sorry, i may have confused the issue. Currently i have no SQL or database link up. It is purely the .net application.

So what i have is say two values defined in the ComboBox Properties 'Items' section.

- Tel Number
- Email address

The code would therefore be:

CODE

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If ComboBox1.SelectedItem = "Tel Number" Then
            TextBox1.Text = "1234 567 890"
        End If
        If ComboBox1.SelectedItem = "Email address" Then
            TextBox1.Text = "someone@somewhere.co.uk"
        End If

End Sub


So i can select either value and it will display only what i have specified. But what i want to able to do is to amend the data displayed - in runtime, so the end user can make an update to replace 1234 567 890 with say 0789 123 456, and do the same for the email address.

Hope that helps.

Thanks for your help in advance.

This post has been edited by Martyr2: 1 Feb, 2008 - 01:01 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Edit Values Displayed From A Combo Box.
1 Feb, 2008 - 01:24 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Gotcha! I understand now and yes there is a beautiful solution for this. First off I want to mention that your closing code tag needs the forward slash in order to work.

code.gif

Ok, now the beautiful solution. In .NET everything is treated as an object. You can create an object say "contact" which has properties like "telephone" and "name". Great, with me so far? One of the methods you can also give it is ToString() which represents a text representation of the object. It would be used like...

CODE

Dim jimmy as new Contact

jimmy.telephone = "1234 567 890"
jimmy.email = "someone@somewhere.co.uk"

' Print out the contact info
' It would messagebox something like "Jimmy - Telephone: 1234 567 890 Email: someone@somewhere.co.uk"
MessageBox.Show(jimmy.ToString())


Of course you override ToString and give it this functionality. Ok, got this bit alright? Good.

You can then add objects to your combo. The combo box will then use the info returned by the ToString method as the text for the item. So if we were to stick Jimmy into the combo box, the value "Jimmy - Telephone: 1234 567 890 Email: someone@somewhere.co.uk" would be printed in the combobox list.

The idea though is that it remains an object. So you can actually reference the combobox items as you normally would, but if you were fetch the item out you could use it as the original object... a Contact. You would just have to cast it out.

Here is an example of the contact class....

CODE

Public Class Contact
    Private Thename As String
    Private Thetelephone As String
    Private Theemail As String

    ' Constructor
    Public Sub New(ByVal name As String, ByVal telephone As String, ByVal email As String)
        Thename = name
        Thetelephone = telephone
        Theemail = email
    End Sub

    ' Property we can use to set the Telephone number
    Public Property Telephone()
        Get
            Return Thetelephone
        End Get
        Set(ByVal value)
            Thetelephone = value
        End Set
    End Property

    ' Notice this "overrides" the ToString to create our own ToString
    Public Overrides Function ToString() As String
        Return Thename & " - " & Thetelephone & " - " & Theemail
    End Function
End Class


Notice we implement a constructor for building the contact, a property for reading and setting the telelphone and another that overrides the toString method to show us the contacts name, telephone and email.

So lets add this to a combo....

CODE

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim jimmy As New Contact("Jimmy", "1234 567 890", "someone@somewhere.co.uk")

        ComboBox1.Items.Add(jimmy)
End Sub


Notice how I make a copy of the contact object and set it to Jimmy's content. I then simply add this object to the combo. It now shows one item in the combo with the contact info in it. Great! But how do we get at it again? Well we simply cast it back to a Contact object and we can get access. Here is an example...

CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Create a contact instance variable
        Dim blankContact As Contact

        ' Fetch the item from the combobox like any other item, but cast it to type "Contact" using Ctype
        blankContact = CType(ComboBox1.Items.Item(0), Contact)

        ' Print the contacts phone number... which will be Jimmy's since he is the first item in our combo.
        MessageBox.Show(blankContact.Telephone)
End Sub


With this ability you can essentially take Jimmy out of the combo, modify his settings, put him back in. You can loop through the combo, ask each item to spit out its Telephone and stick it in the textbox. This allows you to modify elements at runtime, add to them, everything you can do with an object at runtime and show the latest changes.

Hope that helps you out! Enjoy!

"At DIC we be object manipulating son of a gun code ninjas!" decap.gif

This post has been edited by Martyr2: 1 Feb, 2008 - 01:29 PM
User is online!Profile CardPM
+Quote Post

jpool
RE: Edit Values Displayed From A Combo Box.
1 Feb, 2008 - 01:45 PM
Post #5

New D.I.C Head
*

Joined: 22 Jan, 2008
Posts: 14

Brilliant!!. Thanks for that. Seems straight forward enough. Will try it out smile.gif

Oh, and sorry about the 'code' posting errors. I'll get it right next time.

Thanks for all your help Martyr2 biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month