5
Reply

Windows Button

Maha

Maha

Sep 22 2014 7:04 AM
683
What is reason for if we move the Button button1 = new Button(); within the constructor. Program is not working. Problem is highlighted.

Why is it called button1 is a private field?

using System;
using System.Windows.Forms;
using System.Drawing;

public class WindowWithButton : Form
{
//Button button1 = new Button();

public WindowWithButton()
{
Button button1 = new Button();

this.Size = new Size(300, 300);

this.Text = "Window Object With Button";
button1.Text = "Press";

this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });

this.button1.Location = new Point(100, 50);
}

public static void Main()
{
Application.Run(new WindowWithButton());
}
}


Answers (5)