Dynamically adding .EndsWith() from combobox

visual basic combobox selected item
how to add items in combobox in vb
vb.net combobox add item
c# combobox add item and value
combobox example
dynamically add items to combobox c#
combo box in c#
how to set combobox value in c#

I have a combo box called comboFileTypes. Inside that is a drop down list containing:

MP4
MOV
MKV
VOB

And after a button press I have the following code to scan a directory for files:

var files = Directory
    .EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories)
    .Where(s => 
        s.EndsWith(".mp4") || 
        s.EndsWith(".mov") || 
        s.EndsWith(".vob") ||
        s.EndsWith(".MP4") || 
        s.EndsWith(".MOV") || 
        s.EndsWith(".VOB"));

Which is hardcoded. I want the WHERE option to be dynamically generated from the combobox instead, so that the user can add another type of file if they need to. (Also case insensitive, if that's possible, otherwise I'll just add both cases)

Any help would be appreciated.

You can get values from ComboBox by

var values = comboFileTypes.Items.Cast<string>()

and use it in like this:

var files = Directory
    .EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories)
    .Where(s => values.Any(v => s.EndsWith(v, StringComparison.OrdinalIgnorecase));

Mastering Microsoft Visual Basic 2008, 560-564 DriveInfo class, 560-561 drives, system, 561 DropDown ComboBox, TimeSpan class, 494-495 dynamic arrays, 81—82 dynamic content, adding to 366 EndsWith method, String class, 468—469 EndsWith/StartsWith methods,  Step 3: After drag and drop you will go to the properties of the ComboBox control to set the properties of the ComboBox according to your need. Output: Run-Time: It is a little bit trickier than the above method. In this method, you can set create your own ComboBox control using the ComboBox class. Steps to create a dynamic ComboBox:

Well, maybe I'm missing something big, but...

void onOpen()
{
    var selected = comobFileTypes.SelectedItem;

    if ( selected != null ) {
        string ext = "." + selected.ToString();

        var files = Directory.EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories).Where(
                s => s.EndsWith( ext );

        // ...more things...
    }

    return;
}

The key here is that ComboBox has a SelectedItem property, which can be null if nothing is selected, or hold the selected option.

If you want to select all the extensions, then you can use the Items property.

void onOpen()
{
    string[] items = new string[ comboFileTypes.Items.Length ];

    foreach(int i = 0; i < items.Length; ++i) {
        items[ i ] = "." + comboFileTypes.Items[ i ].ToString();
    }

    var files = new List<string>();
    var allFiles = Directory.EnumerateFiles( sourceDIR.Text, "*.*", SearchOption.AllDirectories );

    foreach(string ext in items) {
        files.AddRange( allFiles.Where( s => s.EndsWith( ext ) );
    }

    // ...more things... with files...
    return;
}

Hope this helps.

Dreamweaver CS3: The Missing Manual: The Missing Manual, 948 Dropdown Menus snippet, 652 DTDs (Document Type Definitions), 481 dummy 750 vs. dynamic Web sites, 750 dynamic information adding to databases, 77, 369–370 Ends With comparison operator, 787 Enforce pattern function,  please follow the above code for rest of the combo boxes and while adding the value of the second combo box add a comma in between the values. After building the string in this format you can then append it to your query and now when you execute the query you should not find any kind of errors.

try this code:

List<string> items = new List<string>();
for (int i = 0; i < comboBox1.Items.Count; i++)
{
    items.Add(comboBox1.Items[i].ToString().ToLower());
}

var files = Directory
        .EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories)
        .Where(file => items.Any(item => file.ToLower().EndsWith(item)));

Populate Combo boxes dynamically (JavaFX forum at Coderanch), Since this is a JavaFX question, I've added it to the appropriate section. "it searches the JavaFX form and whenever it finds a combobox with matching name it populates it" endsWith( ".class" )); toString().substring( 0 ,fff. The name of the combo box will be used further in the code. Step 3: Add the next combo box. Similar to the first combo box, we will add the second combo box for options and set it’s properties 'Add the second Combo Box for options Set optionsComboBox = newForm.designer.Controls.Add("Forms.combobox.1") With optionsComboBox .Name = "cbOptions

Get all extensions from combobox:

 var extensions = comboFileTypes.Items.Cast<string>().Select(x => "." + x);

Search ignoring case by enumerating all files in directory:

var files = Directory.EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories).Where(x => extensions.Any(y => y.Equals(Path.GetExtension(x), StringComparison.OrdinalIgnoreCase)));

As alternative you could search for specified extensions only:

 var files = extensions.AsParallel().SelectMany(x => Directory.EnumerateFiles(sourceDIR.Text, "*" + x, SearchOption.AllDirectories));

VB.Net - ComboBox Control, Net - ComboBox Control - The ComboBox control is used to display a drop-down list Let's create a combo box by dragging a ComboBox control from the Toolbox and Clear() End Sub 'displaying the selected item on the label Private Sub  Populating a combo box isn't hard, but getting the combo box to communicate with a constantly changing list is a little more difficult. Learn how to populate your combo box with a dynamic list.

another possibility, would be to use the search pattern. Unfortunately it is not possible to search for multiple patterns at once. But you could use SelectMany to scan through the pattern list and flatten the search results into a single list:

var allPatterns = comboFileTypes.Items.Cast<string>();
allPatterns.SelectMany(pattern => Directory.EnumerateFiles(@"F:\temp", $"*.{pattern}", SearchOption.AllDirectories));

The filenames will be ordered as the pattern list

Add item to combobox dynamically, You are aware that you actually have to set the selected item in the combobox before it shows up in the edit field, right? Hide Copy Code. ff. Let's create a combo box by dragging a ComboBox control from the Toolbox and dropping it on the form. You can populate the list box items either from the properties window or at runtime. To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control.

Build Lambda Expressions Dynamically, Apart from the UI specific code to fill this dropdown lists, to add another Value.​EndsWith("@email.com") || (p.Name != null && p.Name.Trim(). The font tuple is assigned to the Combobox by font = fontExample. app.option_add('*TCombobox*Listbox.font', fontExample) We also need to specify the font of the list box of Combobox otherwise the text in the list of Combobox still uses the system default font but not the fault specified to the Combobox. Python Tkinter ttk Combobox Event Binding

Define a dynamically populated ComboBox, Notice that the new field ( country.codedesc ) has been added to the form in the upper left. Move it to its correct location and convert it to a ComboBox widget. Hello, you need to decide how you want to persist the new item you are adding to the combo box. Every time you start/stop your application those dynamically added items will be erased from memory unless you store it to disk or database. Decide if you need to store it in a database (Access, SQL Server) or on disk (XML, Dataset, flat text, CSV, etc).

HTML DOM Select add() Method, Select add() Method. ❮ Select Object. Example. Add a "Kiwi" option at the end of a drop-down list:. After assigning values to these properties dynamically, I set the datagrid itemsource as this observable collection. One of the columns in the wpf datagrid is of combobox type. The values required to populate the combobox is obtained from an inner observablecollection which is mentioned in the parent observablecollection that i wrote earlier.

Comments
  • do you want to scan for the selected item? or all items/fileendings in the combobox?
  • @MongZhu All files ending with the extensions listed in the combobox. Currently they are hardcoded which is not what I want.
  • What's the role of combo box in the scenario? Use can just select a single item from ComboBox. May be you are talking about a multi-select listbox of a checkedlistbox? Or if it's a single combo box item, is it containing multiple extensions separated by | or something?
  • Anyway, use one of the solutions here: How to filter Directory.EnumerateFiles with multiple criteria?. You just need to get the extensions from somewhere, an item, selected items or .....
  • It looks good, but i'm getting an red underline on the ComboBoxItem, namespace could not be found?
  • Much better: Where(s => values.Any(v => s.EndsWith(v, StringComparison.OrdinalIgnorecase));. This also covers mixed-case file extensions like e.g. ".Mp4".
  • @KlausGütter Yes, you're absolutely right! I've updated the answer.
  • Thank you very much!
  • Thank you - but that only adds the selected item in the combobox, I want all of them.