-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Pandurang_Nayak
Message 2 in Discussion
I prefer replying to queries using MSDN wherever possible. So here
goes:
A common
task in application development is adding (and removing) controls to any
container control on your forms (such as the Panel or GroupBox control, or even
the form itself). At design time, controls can be dragged directly onto a panel
or group box. At run time, these controls maintain a Controls collection, which
keeps track of what controls are placed on them.
Note that
the example below applies to any control that maintains a collection of controls
within it.
To add a
control to a collection programmatically
- Create an instance of the control
to be added.
- Set properties of the new control.
- Add the control to
the Controls collection of the parent control.
The
following example shows how to create an instance of the Button control. It
assumes a form with a Panel control and that the event-handling method for the
button being created, NewPanelButton_Click, already exists.
' Visual
Basic
Public NewPanelButton As New Button()
Public Sub
AddNewControl()
' The Add method will accept as a parameter any
object that derives
' from the Control class. In this case, it
is a Button control.
Panel1.Controls.Add(NewPanelButton)
' The event handler
indicated for the Click event in the code
' below is used as an
example. Substite the appropriate event
' handler for your
application.
AddHandler NewPanelButton.Click, AddressOf
NewPanelButton_Click
End Sub
//
C#
public Button newPanelButton = new Button();
public void
addNewControl()
{
// The Add method will accept as a
parameter any object that derives
// from the Control class. In
this case, it is a Button control.
panel1.Controls.Add(newPanelButton);
// The event handler
indicated for the Click event in the code
// below is used as
an example. Substite the appropriate event
// handler for your
application.
this.newPanelButton.Click += new
System.EventHandler(this. NewPanelButton_Click);
}
To remove controls from
a collection programmatically
Remove the event handler from the event. In
Visual Basic, use the RemoveHandler keyword; in C#, use the -= operator.
Use
the Remove method to delete the desired control from the panel's Controls
collection.
Call the Dispose method to release all the resources used by the
control.
' Visual Basic
Public Sub RemoveControl()
' NOTE: The code
below uses the instance of
' the button (NewPanelButton) from the previous
example.
If Panel1.Controls.Contains(NewPanelButton)
Then
RemoveHandler NewPanelButton.Click,
AddressOf _
NewPanelButton_Click
Panel1.Controls.Remove(NewPanelButton)
NewPanelButton.Dispose()
End If
End Sub
//
C#
private void removeControl(object sender, System.EventArgs e)
{
//
NOTE: The code below uses the instance of
// the button (newPanelButton)
from the previous example.
if(panel1.Controls.Contains(newPanelButton))
{
this.newPanelButton.Click -= new
System.EventHandler(this.
NewPanelButton_Click);
panel1.Controls.Remove(newPanelButton);
newPanelButton.Dispose();
}
}
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]