Error: Expected class, delegate, or interface struct
Hello,
I am new to programming and attemting to work my way through your course. I am working on chapter 3; Interger Division app,
I figured out why I was getting errors on the "private void" statements. I needed to add the calc code inside the last bracket.
Now the program compiles without errors, and looks great. It just does not work. I can enter values into poerand 1 and operand2 and I can click the calculate button. and nothing happens. Not even the exit button works. The code is "exact" as in the book.
Below is my code:
using System;
using System.Windows.Forms;
publicclassfrmMain : Form
{
privateLabel label2;
privateTextBox txtOperand1;
privateTextBox txtOperand2;
privateTextBox txtResult;
privateButton btnCalc;
privateButton btnExit;
privateButton btnClear;
privateLabel label1;
#region Windows Code
privatevoid InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtOperand1 = new System.Windows.Forms.TextBox();
this.txtOperand2 = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.btnCalc = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(32, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(126, 15);
this.label1.TabIndex = 0;
this.label1.Text = " Enter first interger value:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(17, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(142, 15);
this.label2.TabIndex = 1;
this.label2.Text = "Enter second interger value:";
//
// txtOperand1
//
this.txtOperand1.Location = new System.Drawing.Point(183, 27);
this.txtOperand1.Name = "txtOperand1";
this.txtOperand1.Size = new System.Drawing.Size(100, 20);
this.txtOperand1.TabIndex = 2;
//
// txtOperand2
//
this.txtOperand2.Location = new System.Drawing.Point(183, 54);
this.txtOperand2.Name = "txtOperand2";
this.txtOperand2.Size = new System.Drawing.Size(100, 20);
this.txtOperand2.TabIndex = 3;
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(28, 93);
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(244, 20);
this.txtResult.TabIndex = 4;
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(28, 143);
this.btnCalc.Name = "btnCalc";
this.btnCalc.Size = new System.Drawing.Size(75, 23);
this.btnCalc.TabIndex = 5;
this.btnCalc.Text = "Calculate";
this.btnCalc.UseVisualStyleBackColor = true;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(196, 142);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 6;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(111, 202);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23);
this.btnClear.TabIndex = 7;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Visible = false;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(292, 252);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalc);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.txtOperand2);
this.Controls.Add(this.txtOperand1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public frmMain()
{
InitializeComponent();
}
publicstaticvoid Main()
{
frmMain main = newfrmMain();
Application.Run(main);
}
privatevoid btnCalc_Click(object sender, EventArgs e)
{
bool flag;
float operand1;
float operand2;
float answer;
// Input Step
// Check first input...
flag = float.TryParse(txtOperand1.Text, out operand1);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
txtOperand1.Focus();
return;
}
// Check second input
flag = float.TryParse(txtOperand2.Text, out operand2);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
txtOperand2.Focus();
return;
}
// Process Step
answer = operand1 / operand2;
// Display Step
txtResult.Text = operand1.ToString() + " divided by " +
operand2.ToString() +
" equals " + answer.ToString();
txtResult.Visible = true;
}
privatevoid btnExit_Click(object sender, EventArgs e)
{
Close();
}
}
Last edited by cashpot; November 5th, 2009 at 12:18 PM..
Reason: I added Help comments for clarity of question
|