Microsoft KB Archive/316680

= HOW TO: Use the .NET Framework Properties and Events in Visual J# .NET =

Article ID: 316680

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q316680



IN THIS TASK

 * SUMMARY
 * How to Gain Access to the .NET Framework Properties
 * How to Define the .NET Framework Predefined Events
 * Step-by-Step Procedure
 * REFERENCES



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 top

How 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_  to gain access to a .NET Framework property, and then use set_  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(&quot;Send&quot;); back to the top

How 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_  method to add an event handler for an event. Use the remove_  method to remove the event handler for that event. For example, to add a Click event handler for a button, use the add_  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_  method as follows: this.sendButton.remove_Click(new System.EventHandler(this.sendButton_Click)); back to the top

Step-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(&quot;Adding an Event Handler for a Button Click Event&quot;); 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(&quot;Button Text&quot;); // Getting Text Property of Button MessageBox.Show(&quot;New Text Property Value of Button1 &quot; + button1.get_Text); MessageBox.Show(&quot;Now Removing the Existing Event Handler&quot;); // Removing the Event this.button1.remove_Click(new System.EventHandler(this.MyHandler)); } </li> On the Debug menu, click Start.</li> 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.</li> Click button1 again. Notice that the messages do not appear.</li></ol>

back to the top

<div class="references_section">