Hi Guys
NP128 AddRange()
In some program without AddRange() method, button won’t appear on the Form. But in some program without AddRange() method, button appears on the Form. Actually what is the function of the AddRange(). Please explain.
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
Thank you
Program with AddRange()
namespace _9_WindowCreatedWithIDE
{
public class Form1 : System.Windows.Forms.Form
{
private Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// button1
this.button1.Location = new System.Drawing.Point(80, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Press Me";
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
//this.Controls.Add(this.button1);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });
this.Name = "Form1";
this.Text = "My IDE Form";
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
}
}
Program without AddRange()
namespace _10_WindowCreatedWithIDE
{
public class Form1 : System.Windows.Forms.Form
{
private Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// button1
this.button1.Location = new System.Drawing.Point(80, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Press Me";
this.button1.Click += new System.EventHandler(this.button1_Click);
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "My IDE Form";
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Thank you");
}
}
}
AlanPosted Aug 18, 2008, 10:44 AM
Whereas the Add() method adds a single control to the form's controls collection (i.e. its Controls property), the AddRange() method adds a collection of controls.
So, in the code you posted, an array of controls is created (even though it only contains a single Button) and is then added to the form's controls collection using the AddRange() method.
So, in more normal code, use AddRange() to add several controls in a single operation.
Posted Aug 18, 2008, 6:10 PM
Thank you very much for explanation.
AlanPosted Aug 18, 2008, 5:58 PM
Click is a public event defined within the System.Windows.Forms.Control class and therefore inherited by the Button class. It's type is System.EventHandler.
It can therefore be accessed via an instance of the Button class (button1).
To add a method to its invocation list, you need to create a new instance of System.EventHandler which takes the method to be called (or, more correctly, a delegate encapsulating that method) as its argument. This is done using the special operator +=. We therefore have:
this.button1.Click += new System.EventHandler(this.button1_Click);
Posted Aug 18, 2008, 4:56 PM
In the above second program, change is occurring by clicking a button. But public event EventHandler click; is not declared, even though this.button1.Click += new System.EventHandler(this.button1_Click); is used. Please explain.
AlanPosted Aug 18, 2008, 11:52 AM
In the second program, you have this line:
this.Controls.Add(this.button1);
You don't therefore need the AddRange() method because the (single) Button is being added by the Add() method.
Posted Aug 18, 2008, 11:30 AM
This reply is explaining difference between the Add() method and the AddRange() method. I had a problem in understanding that. Thank you very much for explanation.
In the above first program button to appear on the Form AddRange() method is required.
But in the above second program AddRange() method is not required for button to appear on the Form. Please explain the reason.
Guest UserPosted Aug 18, 2008, 10:44 AM
Please see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.addrange.aspx for details.