“Everything’s an object” Let see how windows application follow OOP

The phrase “Everything’s an object” is even more true when it comes to Windows applications. From the form that runs to the controls on the form, you need to make use of OOP techniques all the time.

  • First adding a new button to the Form1 form. This button is an object, called Button.
  • Double-clicking the button, added an event handler to listen for the click event the Button object generates. This event handler is added into the code for the Form object the encapsulates your application, as a private method.
  • The first line of code you added changes the text on the button that is clicked. This makes use of polymorphism.
  • The Button object representing the button that you click is sent to the event handler as an object parameter, which you cast into a Button type – [this is possible because the Button object inherits from System.object, which is the .NET class that object is an alias for]
  • Then register event handler as a listener for the Click event, using some overloaded operator syntax.
  • Use of Controls property. This property is an object that is a collection of all the controls on your form.
Private void button1_Click(object sender, System.EventArgs e)
{

((Button)sender).Text="Clicked!";

Button newButton= new Button();
newButton.Text="New Button!";
newButton.Click+= new EventHandler(newButton_Click);
Controls.Add(newButton);

}


private void newButton_Click(object sender, System.EventArgs e)
{
((Button)sender).Text="Clicked";

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s