| |
|
|
| |
|
|
| |
| |
|
User Interfaces in VC# & VS: a simple calculator:
In VS, setup a Windows Forms Application
(not a Console
Application!!) project (File --> New
--> Project --> Windows Forms Application).
Notice how the IDE automatically generates an empty form
for you. Change the file name in the Solution Explorer
from Form1.cs into Adder.cs (VS will ask you if it should change
all references to that file in the project as well. Answer affirmatively.)
In the Design View,
select right-click-->properties
and change the Text
attribute from Form1 to
simple adder. Also
change the (Name)
attribute to AddForm. Now check the results in
both Design View and code view.
Remember!! A form's Name attribute refers to its class name. The Text attribute only refers to the default Text attribute displayed in the form window's header.
Add some interactivity to the form:

private void CloseButton_Click(object sender, EventArgs e)
{
Dispose();
}
Compile and run and notice how the form stays on the
screen until you click the Close
button. Also notice how when you click the Add button,
it depresses but still does not result in an action (still no event handler defined
for it yet).
private void AddButton_Click(object sender, EventArgs e)
{
SumBox.Text = System.Convert.ToString(System.Convert.ToDouble(FirstBox.Text) +
System.Convert.ToDouble(SecondBox.Text));
}
Note how this subroutine gets called when AddButton is clicked. All it does is convert the Text properties in FirstBox and SecondBox to doubles, adds them up, converts the result back into a string which is then displayed in the SumBox.
First deal with exceptions in general. Add a try/catch block to the AddButton_Click() function that catches any exception:
private void AddButton_Click(object sender, EventArgs e)
{
try
{
SumBox.Text = System.Convert.ToString(System.Convert.ToDouble(FirstBox.Text) +
System.Convert.ToDouble(SecondBox.Text));
}
catch {}
}
private void AddButton_Click(object sender, EventArgs e)
{
try
{
SumBox.Text = System.Convert.ToString(System.Convert.ToDouble(FirstBox.Text) +
System.Convert.ToDouble(SecondBox.Text));
}
catch(System.Exception f)
{
MessageBox.Show(f.message);
}
}
private void FirstBox_TextChanged(object sender, EventArgs e)
{
SumBox.Text = "";
}
private void SecondBox_TextChanged(object sender, EventArgs e)
{
SumBox.Text = "";
}
Close.
Compile and run. Notice how the application now starts with MenuForm and how its menus are interactive. However, since we have not coded any event handlers on the menu items, clicking them has no effect.
Double-clicking on the menu item creates the skeleton event handler. Add the Dispose() call to its contents:
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Dispose();
}
Compile and make sure this works.
private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
{
MyForm = new AddForm();
MyForm.ShowDialog();
}
In other words; when the 'Caculator...' option is clicked, make a new AddForm object.
Notice that this will not compile as the compiler complains that MyForm is not declared. Reason: the calculatorToolStripMenuItem_Click function lives on the MenuForm class, but this class does not yet have an object MyForm of class AddForm declared.
Add the following line at the top of the code for MenuForm:
User interfaces in C# consist of objects that have other
objects that live on them. For instance, a form object
may have a few button
and textbox
objects as well as other form objects living on it.
The VS IDE provides wizards (Design View) that help create
and layout the skeleton code for these objects.
All the objects have properties and behavior.
Objects are connected with mouse-based interaction through
event handlers. These event handlers are functions that get
automatically called when the specified mouse event occurs. Other types
of event handlers exist as well.
Objects of specific classes can create objects of other classes.
Once you have a basic set of classes and event handlers, different user interfaces become mere variations on a theme.