• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 64
  • Last Modified:

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?
0
Abhishek Modak
Asked:
Abhishek Modak
  • 2
1 Solution
 
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
 
Abhishek ModakStudentAuthor Commented:
Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now