Hi, i have problems with exercise 2 in chapter 3. When i click btnCalc, no matter what number i've typed into the fahrenheit box, the program gives me the answer 0! I've used the following code:
Code:
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private Label label1;
private Label label2;
private TextBox txtTf;
private TextBox txtTc;
private Button btnCalc;
#region Windows code
private void InitializeComponent()
{
this.btnCalc = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtTf = new System.Windows.Forms.TextBox();
this.txtTc = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(107, 87);
this.btnCalc.Name = "btnCalc";
this.btnCalc.Size = new System.Drawing.Size(75, 23);
this.btnCalc.TabIndex = 0;
this.btnCalc.Text = "Calculate";
this.btnCalc.UseVisualStyleBackColor = true;
this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(12, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(131, 15);
this.label1.TabIndex = 1;
this.label1.Text = "Temperature in Celcius:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 25);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(131, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Temperature in fahrenheit:\r\n";
//
// txtTf
//
this.txtTf.Location = new System.Drawing.Point(149, 25);
this.txtTf.Name = "txtTf";
this.txtTf.Size = new System.Drawing.Size(100, 20);
this.txtTf.TabIndex = 3;
//
// txtTc
//
this.txtTc.Location = new System.Drawing.Point(150, 52);
this.txtTc.Name = "txtTc";
this.txtTc.ReadOnly = true;
this.txtTc.Size = new System.Drawing.Size(100, 20);
this.txtTc.TabIndex = 4;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(284, 118);
this.Controls.Add(this.txtTc);
this.Controls.Add(this.txtTf);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnCalc);
this.Name = "frmMain";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public frmMain()
{
InitializeComponent();
}
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
private void btnCalc_Click(object sender, EventArgs e)
{
bool flag;
float Tf;
float Tc;
//check input
flag = float.TryParse(txtTf.Text, out Tf);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "input error");
txtTf.Focus();
return;
}
//process step...
Tc = 5 / 9 * (Tf - 32);
txtTc.Text = Tc.ToString();
txtTc.Visible = true;
}
}