Click here to Skip to main content
14,365,158 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everyone.

I am having a little hard time finding anywhere the answer to workaround this question by myself, while advancing further in my code, but at this time im nowhere else to go further if I don't solve it, and here you got me, aksing for help.

I think a simple yes/no answer can make me happy if there is nothing better. But for that, probably I should explain every bit of what I want:

I have a combobox which I want it to be filled and display with the names of all the files in a selected directory or many different ones, but when selecting an item in the combobox, it returns the whole path to the file. I feel like i should explain more of the functionality of the combobox and/or the program so it is justified the why i don't think using a DataBase its a good idea, but it probably will be a lot of worthless words, so I will limit myself to say that I will <b>not</b> use a database because that would be too much slow for the purpose.

Then, finally, my question is: Is there any simplier way to have a display member AND a value member within the combobox without a database, connection string, etc?

To make it faster, i had this idea of code, so if the answer is no, AndAlso the answer is simplier And/Or faster/code-optimal than this, I would appreciate the hint (and code snippet) very much.

Private ColPaths() As String
Private ColSelPath As String

Private Sub FillCboBox()

    For Each Str As String In Directory.GetFiles(InteractiveMap.GalleriesPath, _
                                                "*.lst", SearchOption.AllDirectories)

        ReDim Preserve ColPaths(cboCollection.Items.Count - 1)
        ColPaths(cboCollection.Items.Count - 1) = Str
End Sub

Private Sub cboCollection_SelectedIndexChanged(sender As Object, e As EventArgs) _
                                                Handles cboCollection.SelectedIndexChanged

    ColSelPath = ColPaths(cboCollection.SelectedIndex)
End Sub

I should add that this is only one of 3 different comboboxes that i want to fill and use with a very similar methood, unless one is with the content from a text file(easy for user to edit manually also, if wanted/needed) and the other one is with the combination of files/paths and content of the same kind of editable text files, that is one of the many reasons of why a database would not work for me.

PS. I am new at this site, so I don't know yet how do things work around here, i assume similar to other forums, but i have seen some different and very interesting things (like the workspace). Whatever, its also because of that, that i want to ask you about the "Preview" shown below here, the text editor, i cant see anything but an orange-line rectangle with "..." in it.

Is my browser (chrome) not well for working here? is the site? or what could it be? (sorry if this question is not in the correct place)

PS2. This code is in but I welcome C# answers too.
Im working in VS2013 and the framework of this project (because of its simplicity and that i want it to be very compatible) its 2.0

Finally, sorry for my horrible english, and thank you in advance.
Updated 17-Oct-19 23:59pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

To use DataMember and ValueMember you don't have to have a DataBase, but you do have to have a data something. Meaning you could use a DataTable or other DataBinding class. If you loaded your files into a datatable, there are likely examples online, you could do it that way.

The other way and probably easier is to change your code a little bit. From[^], if this is a Winforms app then you can do it this way. ASP.Net ComboBox can be done a different way.

Dictionary<string,>test = new Dictionary<string,>();
        test.Add("1", "dfdfdf");
        test.Add("2", "dfdfdf");
        test.Add("3", "dfdfdf");
        comboBox1.DataSource = new BindingSource(test, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";

// Get combobox selection (in handler)
string value = ((KeyValuePair<string,>)comboBox1.SelectedItem).Value;
Elighne 29-Apr-14 1:43am
Excelent, the one that fitted better for me was the one at Stack Overflow. Don't know why I didn't ran into that one when i was googling.
I was tempted to try new horizons with a DataTable, but that would have mean to fill 2 controls one from another and that is not precisely faster at runtime when working with more than 1000 archives or so.
Thank you very much.
ZurdoDev 29-Apr-14 7:14am
Glad to hear you got it working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100