How to compare items stored in a combo box or drop down list and a list in visual studio?

I have created a combo box that stores names of Bluetooth devices and a list that stores names of available paired Bluetooth devices. Now I want to compare a selected item from combo box to all items stored in the list . Can anyone help me by specifying how can I do it programmatically?
Abhishek ModakStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Without knowing the data you are storing in the Combobox vs. the List, I can only make assumptions, but let's say for the sake of argument that you are storing the Description of said interfaces with like an ID value; for the list lets say you have a complete object which represents the interface.  You could simply use linq to find matches based on the name; e.g. -

Form1.cs -
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Management;
using System.Windows.Forms;

namespace EE_Q29089153
{
    public partial class Form1 : Form
    {
        readonly List<USBDeviceInfo> devices;
        public Form1()
        {
            InitializeComponent();
            if (devices == null)
                devices = GetUsbDevices();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            comboBox1.DataSource = (from device in devices select new { Value = device.DeviceID, Display = device.Description }).ToList();
            comboBox1.DisplayMember = "Display";
            comboBox1.ValueMember = "Value";
        }

        List<USBDeviceInfo> GetUsbDevices()
        {
            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_USBHub"))
                collection = searcher.Get();
            return (from device in collection.Cast<ManagementObject>()
                    select new USBDeviceInfo
                    {
                        DeviceID = device.GetPropertyValue("DeviceID").ToString(),
                        PnpDeviceID = device.GetPropertyValue("PNPDeviceID").ToString(),
                        Description = device.GetPropertyValue("Description").ToString()
                    }).ToList();
        }

        private void OnSelectedValueChanged(object sender, EventArgs e)
        {
            if (sender is ComboBox)
            {
                var cmb = sender as ComboBox;
                var matches = (from device in devices where cmb.SelectedValue.Equals(device.DeviceID) select device);
                if (matches != null && matches.Count() >= 1)
                {
                    textBox1.Clear();
                    textBox1.AppendText(string.Join($"{Environment.NewLine}{Environment.NewLine}", from device in matches select device));
                }
            }
        }
    }

    class USBDeviceInfo
    {
        public string DeviceID { get; set; }
        public string PnpDeviceID { get; set; }
        public string Description { get; set; }
        public override string ToString()
        {
            return $"Device ID: {DeviceID}{Environment.NewLine}Pnp Device ID: {PnpDeviceID}{Environment.NewLine}Description: {Description}";
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29089153
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(92, 13);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(377, 21);
            this.comboBox1.TabIndex = 0;
            this.comboBox1.SelectedValueChanged += new System.EventHandler(this.OnSelectedValueChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(74, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "USB Devices:";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(15, 41);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(453, 208);
            this.textBox1.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(481, 261);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.comboBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
    }
}

Open in new window

Which produces the following output -Capture.PNGCapture.PNGCapture.PNG-saige-
0
Abhishek ModakStudentAuthor Commented:
Both the combo box and the list stores the Bluetooth devices names and these names are of type string.the combo box gets the device names that are stored in a database.while the list contains the names of Bluetooth devices that are currently paired and are available. So I want to compare a particular string value i.e a device name from the combo box and search it whether this device name exists in the list also that gets generated after the application scans for available paired devices.
0
MrunalCommented:
Hi Abhishek,
Not sure you are asking for events you need to catch or just simple logic you need to read combo box values one by one and compare that value with description in list.

You can use below code for getting items in combo box:
for (int i = 0; i < myComboBox.Items.Count; i++)
{
     string value = myComboBox.GetItemText(myComboBox.Items[i]); 
}

Open in new window


Code to loop through list items:
foreach(List<string> li in myList)
{
    //li
}

Open in new window


You can use String.Compare to compare string values.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abhishek ModakStudentAuthor Commented:
Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.