Visual Programming
Visual Programming
Visual Programming
Applications
Contents
Contrast btw the functions of Windows applications and console
applications
GUI - graphical user interfaces
Windows forms and form properties
Control objects such as buttons, labels, and text boxes to a form
Windows Based Applications
Windows applications function differently from console applications.
Windows applications look differently from console applications.
Contrasting Windows and Console
Applications by Functionality
Console applications
◦ Each line in Main( ) executed sequentially – then the program halts
Windows applications
◦ Once launched, sits and waits for an event
Select
File
New Windows
Project Forms Browse
Application to
template location
to store
your
Name
work
Propertie
s Window
Property value
Properties
Expand Form1.cs
node to reveal the
Form1.Designer.cs
file
17
using System.Windows.Forms; New // Line 1
namespace Windows0 namespace
{ Base class referenced
public class Form1 : Form // Line 2
{
Constructor
public Form1( ) // Line 3
{
this.Text = "Simple Windows Application"; // Line 4
Sets
title }
} bar
}caption
Inspecting the Code - Form1.cs
Output
generated
from last
code
Inspecting the Code -
Form1.Designer.cs
InitializeComponent( ) method included here
#region Windows Form Designer generated code preprocessor directive
◦ // do not modify the contents of this method with the Code Editor
◦ Keyword “this.” precedes property name
◦ Refers to current instance of the class
◦ #endregion // Ends the preprocessor directive
InitializeComponent( ) Method
Some of the auto BackColor = Color.FromArgb (((Byte)(255)),
generated code in the ((Byte)(224)), ((Byte)(192)));
method Font = new Font("Arial", 12F, FontStyle.Bold,
◦ Added as default values GraphicsUnit.Point, ((Byte)(0)));
for properties or from ForeColor = Color.Blue;
changing property values
Location = new Point(30, 30);
Margin = new Padding(4);
MaximizeBox = false;
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "First Windows Application";
Windows Form Events
Windows Form Events
Windows Form Events
Add code to respond to events
◦ Code goes into Form1.cs file
From the Properties window, select the lightning bolt (Events)
◦ Double-click on the event name to generate code
◦ Registers the event as being of interest
◦ Adds a heading for event-handler method
Windows Form Properties
Events
button
selected
Windows Form – Load Event
Code automatically added to register event
A short list of
some of the many
methods.
Explore MSDN
documentation
for more
Systems.Windows.Forms.Control methods
Controls (continued)
GUI controls
Label Objects
Provide descriptive text or labels for other controls
Instantiate object
Label labelName = new Label( );
Add Label
objects to
Form
object…
Use
options on
FORMAT
menu
TextBox properties
TextBox Objects (continued)
Events
Adding Button Objects to TaxApp
Form (continued)
•When you double-click on event, an event-handler method is created:
private void btnCompute_Click(object sender, System.EventArgs e)
{
}
•AND registers click event:
this.btnCompute.Click +=
new System.EventHandler(this.btnCompute_Click);
Adding Button Objects to TaxApp
Form (continued)
private void btnCompute_Click(object sender, System.EventArgs e)
{
string inValue;
double purchaseAmt, percent, ans;
inValue = txtPurchase.Text;
while (double.TryParse(txtPurchase.Text,out purchaseAmt)==false)
{
MessageBox.Show("Value entered must be numeric");
txtPurchase.Text = "0.0";
txtPurchase.Focus();
}
// end of source code for this method – see next slide
Adding Button Objects to
TaxApp Form (continued)
btnCompute_Click( ) ( … continued)
Parse( ) used
inValue = inValue.Remove(inValue.Length-1, 1); here as opposed
to TryParse( )
…since value is
percent = double.Parse(inValue) / 100; being retrieve
ans = (purchaseAmt * percent) + purchaseAmt; from TextBox
AcceptButton
property on the
form
was set to
btnCompute