Formatting Data Bound ComboBox

Posted on 2013-06-06
Medium Priority
Last Modified: 2013-06-11
I have a ComboBox which displays a list of COM ports to chose from:

The underlying data source it's bound to just stores an integer {1,2,3,4,5} instead of a string.

So how do I get the ComboBox to display "COM" + i ?

And in reverse, when the user changes the ComboBox, I just want to store the integer (possibly the (SelectedIndex + 1).

And it's all done with Data Binding. I have a:


which Visual Studio kindly added for me. (All I did was drag from my Data Source to my form.)

I was looking for a Parse event but didn't find it, or I'm not looking in the right place. Or I'm looking for a Format event, or I need an IFormatProvider. What I really need is a working example, since one working example is worth a book full of wordy explanations.

Thought I saw an article on CodeProject explaining how to do this but I've given up trying to find it again.
Question by:deleyd
LVL 62

Expert Comment

by:Julian Hansen
ID: 39227337
Can you post code of where you are so far?
LVL 10

Assisted Solution

adriankohws earned 1600 total points
ID: 39229178
If you need to use binding, then play with:
ValueMember and DisplayMember

ValueMember for you to keep Index and DisplayMember is the text going to be displayed.

Usually I don't use bindings. I will always keep relating list in Arraylists.


Create a combobox named "cbTest" on a new form and paste these codes to see.

    Dim a As New ArrayList
    Dim b As New ArrayList

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Private Sub LoadArrayList()
        Dim x As Integer
        For x = 1 To 5
        For x = 1 To 5
            b.Add("COM " + CStr(x))
    End Sub

    Sub LoadCB()
        Dim x As Integer
        For x = 1 To a.Count
            cbTest.Items.Add(b(x - 1))
    End Sub

    Private Sub cbTest_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTest.SelectedIndexChanged
    End Sub

Author Comment

ID: 39230364
Hmm still not quite what I was looking for. There's some way to format data bound stuff, an event or something to intercept where you can then fiddle with what actually gets displayed. Some doc I have says look into a Parse event and a Format event.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

LVL 10

Accepted Solution

adriankohws earned 1600 total points
ID: 39231024
LVL 83

Assisted Solution

CodeCruiser earned 400 total points
ID: 39239029

Author Closing Comment

ID: 39239748
I ended up doing it just manually. The FORMAT and PARSE events I was looking for is for the older "Binding" whereas nowadays we use "BindingSource" which apparently doesn't have those options.''

I do see the FORMAT event for ListControl, which ComboBox does have, though I'm not quite clear what one can do during the FORMAT event.

Thank you everyone.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question