View Single Post
  #5 (permalink)  
Old January 2nd, 2011, 02:44 AM
RyanL RyanL is offline
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Sorry to bug (and to bring up such an old topic), but I seem to be having a similar issue. The thing is I'm using doubles not integers, yet I'm still getting a 0 for my answer (from the 5 / 9 calculation, as far as I can tell).

Am I making a mistake someplace else?

Thanks in advance for any help!

Code:
using System;
using System.Windows.Forms;

public class frmMain : Form
{
    private TextBox txtFar;
    private TextBox txtResult;
    private Button btnCal;
    private Button btnExit;
    private Label label1;
    #region Windows code
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.txtFar = new System.Windows.Forms.TextBox();
        this.txtResult = new System.Windows.Forms.TextBox();
        this.btnCal = new System.Windows.Forms.Button();
        this.btnExit = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label1.Location = new System.Drawing.Point(12, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(139, 20);
        this.label1.TabIndex = 0;
        this.label1.Text = "Enter Fahrenheit Temp";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // txtFar
        // 
        this.txtFar.Location = new System.Drawing.Point(172, 9);
        this.txtFar.Name = "txtFar";
        this.txtFar.Size = new System.Drawing.Size(100, 20);
        this.txtFar.TabIndex = 1;
        // 
        // txtResult
        // 
        this.txtResult.Location = new System.Drawing.Point(13, 82);
        this.txtResult.Multiline = true;
        this.txtResult.Name = "txtResult";
        this.txtResult.ReadOnly = true;
        this.txtResult.Size = new System.Drawing.Size(259, 46);
        this.txtResult.TabIndex = 2;
        // 
        // btnCal
        // 
        this.btnCal.Location = new System.Drawing.Point(12, 43);
        this.btnCal.Name = "btnCal";
        this.btnCal.Size = new System.Drawing.Size(75, 23);
        this.btnCal.TabIndex = 3;
        this.btnCal.Text = "&Convert";
        this.btnCal.UseVisualStyleBackColor = true;
        this.btnCal.Click += new System.EventHandler(this.btnCal_Click);
        // 
        // btnExit
        // 
        this.btnExit.Location = new System.Drawing.Point(197, 43);
        this.btnExit.Name = "btnExit";
        this.btnExit.Size = new System.Drawing.Size(75, 23);
        this.btnExit.TabIndex = 4;
        this.btnExit.Text = "E&xit";
        this.btnExit.UseVisualStyleBackColor = true;
        this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
        // 
        // frmMain
        // 
        this.ClientSize = new System.Drawing.Size(284, 147);
        this.Controls.Add(this.btnExit);
        this.Controls.Add(this.btnCal);
        this.Controls.Add(this.txtResult);
        this.Controls.Add(this.txtFar);
        this.Controls.Add(this.label1);
        this.Name = "frmMain";
        this.Text = "Temp Converter";
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    #endregion

    public frmMain()
    {
        InitializeComponent();
    }

    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

    private void btnCal_Click(object sender, EventArgs e)
    {
        bool flag;
        double Farenheit;
        double Celcius;

        flag = double.TryParse(txtFar.Text, out Farenheit);
        if (flag == false)
        {
            MessageBox.Show("Enter a number!");
            txtFar.Focus();
            return;
        }

        Celcius = 5 / 9 * (Farenheit - 32);

        txtResult.Text = Farenheit.ToString() + " degrees Fahrenheit is equal to " + Celcius.ToString() + " degrees Celsius";
        txtResult.Visible = true;
    
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Close();
    }

}