|
The Form design,
calculator.jsl
Create Visual J# .Net Forn - calculator.jsl
Classes used in this
Form: ListBox, TextBox,
Button
- On the View menu ,
click Solution Explorer
- In Solution
Explorer
In Solution Explorer, right click the
work_VJnet, select Add on the shortcut menu to
open other shortcut menu, click Add New Item,
the Add New Item - work_VJnet dialog box
appears. |

|
- In the Add New
Item -work_VJnet dialog box:
- In the
Categories pane, select Local Project
Items
- In the Templates
pane, select Windows form . A message
appears - (a form for Windows Applications).
- In the Name
box, type calculator.jsl
- Click
Open
|

|
- In the windows
Forms Designer appears the empty window Form
design
|
|
|
1. |
The
calculator.jsl
design |
|
 |
The Properties of
the Form - calculator.jsl... |
Name: Icon: Maximize: Menu: Size: Start
Position: Text: Window
State: |
calculator Icon False (none) 368,332 CenterScreen calculator
... Normal | | | |
3. |
From the Toolbox/Windows Form add these
controls .... 1 ListBox contol, 1
TextBox contol, 21 Button contols
|
|
- Add the ListBox
control - ListBox1, ListBox
Properties:
Name: Size
: |
ListBox1 176,
172 | |
- Add the TextBox
control - CalcField
Properties:
Name: Font: Size: Text: |
CalcField Times
New Roman, 8.25pt 184,
20 | |
- Add 21 Button
controls
- Btndetail ,
Button
Properties
Name: BackColor: Size: Text: |
Btndetail Silver 50,
20 detail | |
- CalcPlus -
Size: 40,
32
- CalcSub -
Size: 40,
32
- CalcMul -
Size: 40,
32
- CalcDiv
- Size: 40,
32
- CalcDec
- Size: 40,
32
- CalcSign
- Size: 40,
32
- CalcRes -
Size: 40,
32
- CalcCan
- Size: 50,
32
- CalcCE
- Size: 50,
32
- CalcBS
- Size:
96, 32
- Calc0
... Calc9 (
10 Button controls
)
- Size: 40,
32
| | | |
|
calculator.jsl file, the codes after modification
...
The
text
Code is
red
color, the codes added
manuel |
|
package
work_VJnet; import
System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
/**
* Summary description for calculator.
*/
public
class
calculator extends
System.Windows.Forms.Form
{
private
System.Windows.Forms.RichTextBox richTextData;
System.Windows.Forms.Button Btndetail;
System.Windows.Forms.ListBox ListBox1;
System.Windows.Forms.TextBox CalcField;
System.Windows.Forms.Button CalcRes;
System.Windows.Forms.Button CalcPlus;
System.Windows.Forms.Button CalcDec;
System.Windows.Forms.Button CalcSign;
System.Windows.Forms.Button Calc0;
System.Windows.Forms.Button CalcCan;
System.Windows.Forms.Button CalcSub;
System.Windows.Forms.Button Calc3;
System.Windows.Forms.Button Calc2;
System.Windows.Forms.Button Calc1;
System.Windows.Forms.Button CalcCE;
System.Windows.Forms.Button CalcMul;
System.Windows.Forms.Button Calc6;
System.Windows.Forms.Button Calc5;
System.Windows.Forms.Button Calc4;
System.Windows.Forms.Button CalcBS;
System.Windows.Forms.Button CalcDiv;
System.Windows.Forms.Button Calc9;
System.Windows.Forms.Button Calc8;
System.Windows.Forms.Button Calc7;
/**
* Required designer variable.
*/
private
System.ComponentModel.Container components =
null;
//variables
double mOp1,
mOp2; //Previously input operand.
int mNumOps;
//Number of operands.
//Operation
mLastInput; //Indicate type of last keypress event.
String mLastInput;
String mOpFlag;
//Indicate pending
operation.
String mOpPrev; //Previous operation
String mMinus; //Minus operator "-"
String mDecValue;
boolean
mAllowBackSpace; //Allow backspace
boolean
FirstFlag;
//public enum
Operation()
//{
// None = 0,
// Operand = 1,
// Operator = 2,
// CE = 3,
// Cancel = 4
// }
public
calculator()
{
//
// Required for
Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any
constructor code after InitializeComponent call
//
//Get decimal
separator based upon machine local settings.
mDecValue =
System.Globalization.NumberFormatInfo.get_CurrentInfo().get_NumberDecimalSeparator();
this.CalcDec.set_Text(mDecValue);
mMinus = "-";
}
/**
* Clean up any resources being used.
*/
protected
void
Dispose(boolean
disposing)
{
if
(disposing)
{
if
(components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}
Windows Form Designer
generated code |
private void
calculator_Load (Object sender, System.EventArgs e)
{
this.set_Width(this.CalcCan.get_Left() +
this.CalcCan.get_Width() + this.Calc6.get_Left()
* 3 / 2);
this.Btndetail.set_Text("detail");
this.Btndetail.set_Left(this.CalcCan.get_Left());
this.Btndetail.set_Top(this.CalcField.get_Top() +
this.CalcField.get_Height() - this.Btndetail.get_Height());
this.ListBox1.set_Top(this.CalcField.get_Top() + (((this.CalcBS.get_Top()
+ this.CalcBS.get_Height()) - this.CalcField.get_Top()) /
2) - (this.ListBox1.get_Height()
/ 2));
this.ListBox1.set_Left(this.CalcCan.get_Left() +
this.CalcCan.get_Width() + (this.Calc6.get_Left()
* 5 / 4));
}
private void
calculator_Activated (Object sender, System.EventArgs e)
{
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.get_Items().Clear();
FirstFlag =
true;
//''''''
}
private void
Calc0_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
CalcNum_Click(Object sender, System.EventArgs e)
{
System.Windows.Forms.Button
SelButton;
mAllowBackSpace =
true; //now allow backspace
if
(mLastInput != "Operation.Operand")
this.CalcField.set_Text("0");
//this.CalcField.set_Text("");
SelButton = (Button) sender;
FormatEditField(SelButton.get_Text());
mLastInput = "Operation.Operand";
}
//format the
entry in the text box!
private void
FormatEditField(String NewChar)
{
String Value;
Value =
this.CalcField.get_Text();
//Determine if
there are more than one .'s
if
(NewChar.Trim().Equals(mDecValue.Trim()))
{
//If it's found.
this.richTextData.set_Text(Value);
if (this.richTextData.Find(mDecValue)
> -1) //Don't do anything.
return;
Value = Value + mDecValue;
}
else
{
if
(Value.Trim().Equals("0"))
Value = NewChar;
else
Value = Value + NewChar;
}
this.CalcField.set_Text(Value);
}
private void
Calc1_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc2_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc3_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc4_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc5_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc6_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc7_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc8_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc9_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
CalcDec_Click (Object sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
CalcPlus_Click (Object sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
//Handles +, -,
*, /, =
private void
CalcRes_Click (Object sender, System.EventArgs e)
{
System.Windows.Forms.Button
ButtonPressed;
String vstr;
String vline;
int
i;
vline ="";
//Now do not
allow backspace(CalcBS) actions after the result has been
//calculated and
displayed.
mAllowBackSpace = false ;
if
(mLastInput.Equals("Operation.Operand"))
mNumOps = mNumOps + 1;
switch(mNumOps)
{
case
1:
mOp1 =
System.Double.Parse(CalcField.get_Text());
break;
case
2:
mOp2 =
System.Double.Parse(CalcField.get_Text());
if(mOpFlag.Trim().Equals("+"))
{
mOp1 = mOp1 + mOp2;
}
if(mOpFlag.Trim().Equals("-"))
{
mOp1 = mOp1 - mOp2;
}
if(mOpFlag.Trim().Equals("*"))
{
mOp1 = mOp1 * mOp2;
}
if(mOpFlag.Trim().Equals("/"))
{
if
(mOp2 == 0)
MessageBox.Show("Can't divide by
zero! ...", work_VJnet.mainform.mainform_cl.Title,
MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
else
mOp1 = mOp1 / mOp2;
}
if(mOpFlag.Trim().Equals("="))
{
mOp1 = mOp2;
}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if
(mOpFlag == "=")
{
}
else
{
this.ListBox1.get_Items().Add(mOpFlag);
if
(mNumOps == 2)
this.ListBox1.get_Items().Add("
" + System.Convert.ToString(mOp2));
}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.CalcField.set_Text(System.Convert.ToString(mOp1));
mNumOps = 1;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if
(mOpFlag == "=")
{
vstr = " " + ".......... new
operation ..........";
this.ListBox1.get_Items().Add(vstr);
vstr = " " +
System.Convert.ToString(mOp1) ;
this.ListBox1.get_Items().Add(vstr);
this.ListBox1.set_SelectedIndex(ListBox1.get_Items().get_Count()
- 1);
}
else
{
vstr =
System.Convert.ToString(mOp1);
for
(i = 0 ; i <= (vstr.get_Length() * 2) - 4; i++)
{
vline = vline + "-";
}
this.ListBox1.get_Items().Add("
" + vline);
this.ListBox1.get_Items().Add("
" + System.Convert.ToString(mOp1));
this.ListBox1.set_SelectedIndex(ListBox1.get_Items().get_Count()
- 1);
}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
break;
}
mLastInput = "Operation.Operator";
mOpPrev = mOpFlag;
ButtonPressed = (Button) sender;
mOpFlag = ButtonPressed.get_Text();
//
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if (FirstFlag ==
true)
{
vstr = " " + ".......... new
operation ..........";
this.ListBox1.get_Items().Add(vstr);
if
(mNumOps == 1)
{
vstr = " " + mOp1;
this.ListBox1.get_Items().Add(vstr);
}
}
FirstFlag =
false;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
private void
CalcSub_Click (Object sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
private void
CalcDiv_Click (Object sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
private void
CalcMul_Click (Object sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
//+/- click
event.
private void
CalcSign_Click (Object sender, System.EventArgs e)
{
if
(mLastInput != "Operation.Operand")
this.CalcField.set_Text("0");
else
{
if (this.CalcField.get_Text().substring(0,
1).Trim().Equals(mMinus.Trim()))
this.CalcField.set_Text(this.CalcField.get_Text().substring(1));
else
{
if (this.CalcField.get_Text()
!= "0")
this.CalcField.set_Text(mMinus + this.CalcField.get_Text());
}
}
mLastInput = "Operation.Operand";
}
//Cancel click
event.
private void
CalcCan_Click (Object sender, System.EventArgs e)
{
Reset();
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.get_Items().Clear();
FirstFlag =
true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
// Helper to
initialize the vals.
private void
Reset()
{
mOp1 = 0;
mOp2 = 0;
mNumOps = 0;
mLastInput = "Operation.None";
mOpFlag = "";
mOpPrev = "";
this.CalcField.set_Text("0");
mAllowBackSpace =
true;
}
//Cancel entry
click event.
private void
CalcCE_Click (Object sender, System.EventArgs e)
{
if
(mLastInput.Equals("Operation.Operand"))
this.CalcField.set_Text("0");
else
{
if
(mLastInput.Equals("Operation.Operator"))
mOpFlag = mOpPrev;
}
mLastInput = "Operation.CE";
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.get_Items().Clear();
FirstFlag =
true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
private void
CalcBS_Click (Object sender, System.EventArgs e)
{
String Value;
//Check if we can
backspace before removing items from CalcField.get_Text().
if (mAllowBackSpace
== true)
{
Value =
this.CalcField.get_Text();
if
(Value.get_Length() > 1)
Value = Value.substring(0, (Value.get_Length()
- 1));
else
Value = "0";
//if all we are
left is the minus sign, reset.
if
(Value.Trim().Equals("-"))
Value = "0";
this.CalcField.set_Text(Value);
}
}
private void
Btndetail_Click (Object sender, System.EventArgs e)
{
if (this.Btndetail.get_Text().Equals("detail"))
{
this.set_Width(this.CalcCan.get_Left() +
this.CalcCan.get_Width() + (this.Calc6.get_Left() * 5 / 2)
+ this.ListBox1.get_Width());
this.Btndetail.set_Text("hide");
}
else if (this.Btndetail.get_Text().Equals("hide"))
{
this.set_Width(this.CalcCan.get_Left() +
this.CalcCan.get_Width() + this.Calc6.get_Left()
* 3 / 2);
this.Btndetail.set_Text("detail");
}
}
} | | |
|
|