Wednesday, March 10, 2010

Auto-complete feature in .net for winforms controls

Many a times it is required for us to use the auto-complete feature in the controls of the winforms .net application. This feature of auto-complete can be applied both to textbox and combobox alike. The code for it in c#.net is as follows:

public void AutoCompleteTextBox(string tableName, string fieldName, TextBox txtToAutoComp)
        {
            AutoCompleteStringCollection txtCollection = new AutoCompleteStringCollection();
            DataTable dtAutoComp = Dal.ExecuteSP("StoredProcedure", fieldName, tableName);
            if (dtAutoComp.Rows.Count >= 0)
            {
                for (int count = 0; count < dtAutoComp.Rows.Count; count++)
                {
                    txtCollection.Add(dtAutoComp.Rows[count][fieldName].ToString());
                }
            }
            txtToAutoComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtToAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtToAutoComp.AutoCompleteCustomSource = txtCollection;
        }
For implementing the code for combobox just replace the textbox variable with combobox variable. Everything else remains same.

Saturday, March 6, 2010

Finding Hardware ID, CPU ID, Motherboard ID, Hard-Disk ID of a computer

Many a times it is required that we find the hardware id of a system. The hardware can be Motherboard, processor or hard-disk. The manufactures of these devices embed a unique id to them which can be retrieved anytime. Further they are sometimes also used to get the unique id for the machine. In this post I am going to show how to retrieve these id's in C#.net code. First of all we would be requiring the following namespace:

System.Management

The following code will retrieve the CPU ID (processor id):

ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
mbsList = mbs.Get();
string id="";
foreach (ManagementObject mo in mbsList)
{
id = mo["ProcessorID"].ToString();
}


The following code will generate hard-disk id:

ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""c:""");
dsk.Get();
string id = dsk["VolumeSerialNumber"].ToString();


The following code will generate motherboard id:

ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectCollection moc = mos.Get();
string serial="";
foreach (ManagementObject mo in moc)
{
serial = (string)mo["SerialNumber"];
}

By using these we can generate the id's of the respective hardware. Another use of these can be to generate a unique id for that system. But one should be aware that these id's are not always unique. It has been seen that some hardware manufactures use a single id for a whole lot of hardware. In this scenario we can use combination of all these id's to generate a single unique id for the machine.

LinkWithin

Related Posts with Thumbnails