HOW TO: Use the .NET Framework Properties and Events in Visual J# .NET (316680)



The information in this article applies to:

  • Microsoft Visual J# .NET (2003)
  • Microsoft Visual J# .NET (2002)
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1

This article was previously published under Q316680

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_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 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_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 top

Step-by-Step Procedure

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual J# Projects, and then click Windows Application under Templates.
  4. In the Name text box, type MyJSharpWindowsApplication, and then type C:\ in the Location text box. Click OK.
  5. In Design view, drag a Button from the Toolbox to Form1.
  6. 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));
  7. 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));
    }
  8. On the Debug menu, click Start.
  9. 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.
  10. Click button1 again. Notice that the messages do not appear.
back to the top

REFERENCES

For more information about Visual J# .NET, visit the following Microsoft Web site:back to the top

Modification Type:MajorLast Reviewed:8/7/2003
Keywords:kbProperties kbEvent kbJava kbHOWTOmaster kbhowto KB316680 kbAudDeveloper