Solved

Formatting Data Bound ComboBox

Posted on 2013-06-06
6
461 Views
Last Modified: 2013-06-11
I have a ComboBox which displays a list of COM ports to chose from:
COM1
COM2
COM3
COM4
COM5

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:

MyDataSet1
MyBindingSource
MyTableAdapter
TableAdapterManager

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.
0
Comment
Question by:deleyd
6 Comments
 
LVL 51

Expert Comment

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

Assisted Solution

by:adriankohws
adriankohws earned 400 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
        LoadArrayList()
        LoadCB()
    End Sub

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

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

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

Author Comment

by:deleyd
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Accepted Solution

by:
adriankohws earned 400 total points
ID: 39231024
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 100 total points
ID: 39239029
0
 

Author Closing Comment

by:deleyd
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now