SUMMARY
This step-by-step article discusses how to use the Microsoft
.NET Framework properties and the .NET Framework predefined events in your
Microsoft Visual J# .NET application.
back to the topHow to Gain Access to the .NET Framework Properties
Through Visual J# .NET, you cannot gain direct access to the .NET
Framework properties and events. However, you can use the
get_Property method and the
set_Property method in Visual J# .NET to gain access to the .NET Framework
properties.
Use
get_Property Name to gain access to a .NET Framework property, and then use
set_Property Name to set the property. For example, to get the value of the
Text property of a button, use the
get_Property method as follows:
String buttonText = this.sendButton.get_Text();
To set the value of the
Text property of the button, use the
set_Property method as follows:
this.sendButton.set_Text("Send");
back to the topHow to Define the .NET Framework Predefined Events
Visual J# .NET provides the
add_Event method and the
remove_Event method to define event handlers for the .NET Framework predefined
events. Use the
add_Event method to add an event handler for an event. Use the
remove_Event method to remove the event handler for that event. For example,
to add a
Click event handler for a button, use the
add_Event method as follows:
this.sendButton.add_Click( new System.EventHandler(this.sendButton_Click) );
To remove a
Click event handler for a button, use the
remove_Event method as follows:
this.sendButton.remove_Click(new System.EventHandler(this.sendButton_Click));
back to the topStep-by-Step Procedure
- Start Microsoft Visual Studio .NET.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click Visual
J# Projects, and then click Windows Application under
Templates.
- In the Name text box, type
MyJSharpWindowsApplication, and then type
C:\ in the Location text box. Click
OK.
- In Design view, drag a Button from the
Toolbox to Form1.
- Double-click Form1, and then paste the
following Form_Load event sample code in the button1_Click event:
MessageBox.Show("Adding an Event Handler for a Button Click Event");
this.button1.add_Click(new System.EventHandler(this.MyHandler));
- After the Form1_Load method, paste the following sample code:
private void MyHandler(Object sender,System.EventArgs e)
{
// Setting Text Property of the Button
button1.set_Text("Button Text");
// Getting Text Property of Button
MessageBox.Show("New Text Property Value of Button1 " + button1.get_Text());
MessageBox.Show("Now Removing the Existing Event Handler");
// Removing the Event
this.button1.remove_Click(new System.EventHandler(this.MyHandler));
}
- On the Debug menu, click
Start.
- Click button1. In the Form1_Load event handler, notice that the add_Click method is added to the button. Also, notice that the text on
button1 has changed to Button Text.
- Click button1 again. Notice that the
messages do not appear.
back to the
top