30 October 2012

Pass TextBox Values into another form ComboBox in C#.net

Today ,we will discuss about Pass One control Value to another Form in C#.net. · Create a Project in VS 2008. · Create Two Form .Form 1 and Form2. · Create TextBoxes and One Button on Form1. · Rename name the TextBox .Start with txt. · Create ComboBox on Form2. Now , we will go to Our scenario. Copy and Paste the following codes in Form 1 Code View Section
public static string[] textvalues; List values = new List(); private void button1_Click(object sender, EventArgs e) { foreach (Control con in this.Controls) { if (con.Name.StartsWith("txt")) { values.Add(con.Text); } } textvalues = values.ToArray(); Form2 frm = new Form2(); frm.Show(); }
Copy and Paste the following codes in Form 2 Code View Section
private void Form2_Load(object sender, EventArgs e) { comboBox1.Items.Clear(); foreach (string str in Form1.textvalues) { comboBox1.Items.Add(str); } }
Just Run the Application and Enter the values and click Button1 .

Count of Controls in Form using C#.Net

                Today , we will discuss about the Count of Controls in Form.The following are code for getting no of controls in Form.

Method for getting no of controls in Form:
  public IEnumerable<Control> GetAll(Control control, Type type)
            var controls = control.Controls.Cast<Control>();
            return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                      .Where(c => c.GetType() == type);

 call Method in the Form Loading event(where ever you want)
var c = GetAll(this, typeof(Button));
MessageBox.Show("Total Controls: " + c.Count());

Total Controls: (no of Buttons in the form)

24 October 2012

Enable USB Port using C#.Net

Today ,We will discuss about Enable and Disable USB Port using C Sharp .Net. First ,Create a Application for Enable and Disable USB Port using C Sharp .Net. In our Example: Two Buttons in the form. One is Enable. Another one is disable. Now, we are going to our Coding Section:
using System.Security.Principal;
public static bool IsUserAnAdministrator;
private void btn_Enable_Click(object sender, EventArgs e)
{ Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord);
private void btn_Disable_Click(object sender, EventArgs e)
{ Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord);
private void FrmEnabledisableUSBPort_Load(object sender, EventArgs e)
if (IsAnAdministrator())
btn_Disable.Enabled = true;
btn_Enable.Enabled = true;
btn_Disable.Enabled = false;
btn_Enable.Enabled = false;
To check User is Admin or Not.
bool IsAnAdministrator()
WindowsIdentity MyIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);
return MyPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
conclusion: Thanks for reading this Article.