The name 'txtOperande1' does not exist in the current context
Hi Dr Purdum,
I am a junior programmer looking to learn more about the .NET side of things, along with OOP methods, your book has been the bext book by far for an aspiring developer like myself.
Can you please help me with this.
I am currently working through PART 2, Chapter 3, Page 58 of your book.
I am working through the Integer Division problem, i have entered the code precisely, however i am constantly receiving the following error when trying to build the code.
"The name 'txtOperand1' does not exist in the current context"
where am i going wrong??????
Kindest Regards
Steve
p.s have pasted my code below
using System;
using System.Drawing;
using System.Windows.Forms;
publicclassfrmMain : Form
{
privateLabel int1input;
privateLabel label1;
privateTextBox textBox1;
privateTextBox textBox2;
privateButton btnCalc;
privateButton btnExit;
privateLabel label2;
#region Windows code
privatevoid InitializeComponent()
{
this.int1input = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnCalc = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// int1input
//
this.int1input.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.int1input.Location = new System.Drawing.Point(36, 42);
this.int1input.Name = "int1input";
this.int1input.Size = new System.Drawing.Size(152, 20);
this.int1input.TabIndex = 0;
this.int1input.Text = "Enter first integer value:";
this.int1input.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(36, 76);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(152, 20);
this.label1.TabIndex = 1;
this.label1.Text = "Enter first second value:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(216, 41);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(137, 20);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(216, 76);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(137, 20);
this.textBox2.TabIndex = 3;
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(33, 131);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(320, 46);
this.label2.TabIndex = 4;
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(33, 210);
this.btnCalc.Name = "btnCalc";
this.btnCalc.Size = new System.Drawing.Size(129, 23);
this.btnCalc.TabIndex = 5;
this.btnCalc.Text = "&Calculate";
this.btnCalc.UseVisualStyleBackColor = true;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(224, 210);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(129, 23);
this.btnExit.TabIndex = 6;
this.btnExit.Text = "&Exit";
this.btnExit.UseVisualStyleBackColor = true;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(390, 256);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalc);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.int1input);
this.Name = "frmMain";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public frmMain()
{
InitializeComponent();
}
publicstaticvoid Main()
{
frmMain main = newfrmMain();
Application.Run(main);
}
privatevoid frmMain_Load(object sender, EventArgs e)
{
}
privatevoid btnCalc_Click(object sender, EventArgs e)
{
bool flag;
int operand1;
int operand2;
int answer;
// Input Step
// Check first input....
flag = int.TryParse(txtoperand1.Text, out operand1);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
txtOperand1.Focus();
return;
}
// Check second input...
flag = int.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 Exitbtn_Click(object sender, EventArgs e)
{
Close();
}
}
|