Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting Data Bound ComboBox

Posted on 2013-06-06
6
Medium Priority
?
478 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 60

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 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
        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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 10

Accepted Solution

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

Assisted Solution

by:CodeCruiser
CodeCruiser earned 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

926 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