h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

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))
                                      .Concat(controls)
                                      .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());

Result:
Total Controls: (no of Buttons in the form)