Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency Calculator with error which i cant see in debug

Status
Not open for further replies.

desiamerican

Programmer
Jan 20, 2010
1
GB
Hiya all, This Currency Calculator i spend few weeks on it, its complete code to run in Microsoft Visual Studio 2008. but its not working fully. i remove so many errors now i dont get any Error in Debug but few strange thing when i run the program..for example i cant input . decimal anyhow some one help me please i just give up on it.....but i will keep trying and i will try to ask some friends.. Kindly help me if you can
Many Thank. there is no error in code thats why i cant point out where is the problem in code.

Code:
-----------------------------------------------------------
Form1.cs
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace testbaby
{

    public partial class Form1 : Form

    {

        public Form1()
        {

            InitializeComponent();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.txtDisplay.Text = (decimal.Parse(this.txtDisplay.Text) / decimal.Parse(this.txtRate.Text)).ToString("N");
            this.just_cleared = true;
        }

        private void btnBackspace_click(object sender, EventArgs e)
        {
            if (this.txtDisplay.Text.Length > 1)
            {
                this.txtDisplay.Text = this.txtDisplay.Text.Substring(0, this.txtDisplay.Text.Length - 1);
            }
            else
            {
                this.txtDisplay.Text = "0";
                this.just_cleared = true;
                
            }
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            this.txtDisplay.Text = (decimal.Parse(this.txtDisplay.Text) * decimal.Parse(this.txtRate.Text)).ToString("N");
            this.just_cleared = true;
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            this.txtDisplay.AppendText(".");
            this.just_cleared = false;
        }

        private void Button0_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "0";
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("0");
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "1";
                this.just_cleared = false;
            }
            else if ((this.txtDisplay.Text != "0") && (this.txtDisplay.Text.Length < 9))
            {
                this.txtDisplay.AppendText("1");
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "2";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("2");
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "3";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("3");
            }
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "4";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("4");
            }
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "5";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("5");
            }
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "6";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("6");
            }
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "7";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("7");
            }
        }

        private void Button8_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "8";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("8");
            }
        }

        private void Button9_Click(object sender, EventArgs e)
        {
            if (this.just_cleared)
            {
                this.txtDisplay.Text = "9";
                this.just_cleared = false;
            }
            else if (this.txtDisplay.Text.Length < 9)
            {
                this.txtDisplay.AppendText("9");
            }
        }

        private void chkLock_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkLock.Checked)
            {
                this.txtRate.BackColor = Color.Silver;
                this.txtRate.ReadOnly = true;
                
            }
            else
            {
                this.txtRate.BackColor = Color.White;
                this.txtRate.ReadOnly = false;
                
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            string startupPath = Application.StartupPath;
            try
            {
                StreamWriter writer = new StreamWriter(startupPath + @"\ccrate.dat");
                writer.WriteLine(this.txtRate.Text);
                writer.Close();
            }
            catch
            {
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string startupPath = Application.StartupPath;
            if (File.Exists(startupPath + @"\ccrate.dat"))
            {
                StreamReader reader = new StreamReader(startupPath + @"\ccrate.dat");
                this.txtRate.Text = reader.ReadLine();
                reader.Close();
            }
            else
            {
                this.txtRate.Text = "1.0";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void txtDisplay_TextChanged(object sender, EventArgs e)
        {
            this.btnPoint.Enabled = !this.txtDisplay.Text.Contains(".");
        }

        private void txtRate_Leave(object sender, EventArgs e)
        {
            
            try
            {
                double num = double.Parse(this.txtRate.Text);
                if (num > 0.0001)
                {
                    this.txtRate.Text = num.ToString();
                    this.old_rate = num;
                    this.txtDisplay.Text = "0";
                    this.just_cleared = true;
                }
                else 
                {
                    this.txtRate.Text = this.old_rate.ToString();
                }
            }
            catch
            {
                this.txtRate.Text = this.old_rate.ToString();
            }
        }
    }
}
-----------------------------------------------------------------------
form1.Designer.cs
-----------------------------------------------------------------------
namespace testbaby
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;


        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.txtDisplay = new System.Windows.Forms.TextBox();
            this.btnBackspace = new System.Windows.Forms.Button();
            this.Button0 = new System.Windows.Forms.Button();
            this.Button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.button8 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            this.chkLock = new System.Windows.Forms.CheckBox();
            this.txtRate = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.btnBack = new System.Windows.Forms.Button();
            this.btnForward = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnPoint = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // txtDisplay
            // 
            this.txtDisplay.BackColor = System.Drawing.Color.Black;
            this.txtDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtDisplay.ForeColor = System.Drawing.Color.Lime;
            this.txtDisplay.Location = new System.Drawing.Point(26, 16);
            this.txtDisplay.Name = "txtDisplay";
            this.txtDisplay.Size = new System.Drawing.Size(155, 26);
            this.txtDisplay.TabIndex = 0;
            this.txtDisplay.TextChanged += new System.EventHandler(this.txtDisplay_TextChanged);
            // 
            // btnBackspace
            // 
            this.btnBackspace.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnBackspace.Image = ((System.Drawing.Image)(resources.GetObject("btnBackspace.Image")));
            this.btnBackspace.Location = new System.Drawing.Point(143, 87);
            this.btnBackspace.Name = "btnBackspace";
            this.btnBackspace.Size = new System.Drawing.Size(38, 64);
            this.btnBackspace.TabIndex = 62;
            this.btnBackspace.Text = "z";
            this.btnBackspace.UseVisualStyleBackColor = true;
            this.btnBackspace.Click += new System.EventHandler(this.btnBackspace_click);
            // 
            // Button0
            // 
            this.Button0.BackColor = System.Drawing.SystemColors.ActiveBorder;
            this.Button0.Location = new System.Drawing.Point(29, 180);
            this.Button0.Name = "Button0";
            this.Button0.Size = new System.Drawing.Size(70, 24);
            this.Button0.TabIndex = 2;
            this.Button0.Text = "0";
            this.Button0.UseVisualStyleBackColor = false;
            this.Button0.Click += new System.EventHandler(this.Button0_Click);
            // 
            // Button1
            // 
            this.Button1.Location = new System.Drawing.Point(29, 147);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(32, 24);
            this.Button1.TabIndex = 63;
            this.Button1.Text = "1";
            this.Button1.UseVisualStyleBackColor = true;
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(67, 147);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(32, 24);
            this.button2.TabIndex = 64;
            this.button2.Text = "2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(105, 147);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(32, 24);
            this.button3.TabIndex = 65;
            this.button3.Text = "3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.Button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(29, 117);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(32, 24);
            this.button4.TabIndex = 66;
            this.button4.Text = "4";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.Button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(67, 117);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(32, 24);
            this.button5.TabIndex = 67;
            this.button5.Text = "5";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.Button5_Click);
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(105, 117);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(32, 24);
            this.button6.TabIndex = 68;
            this.button6.Text = "6";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.Button6_Click);
            // 
            // button7
            // 
            this.button7.Location = new System.Drawing.Point(29, 87);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(32, 24);
            this.button7.TabIndex = 69;
            this.button7.Text = "7";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.Button7_Click);
            // 
            // button8
            // 
            this.button8.Location = new System.Drawing.Point(67, 87);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(32, 24);
            this.button8.TabIndex = 70;
            this.button8.Text = "8";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.Button8_Click);
            // 
            // button9
            // 
            this.button9.Location = new System.Drawing.Point(105, 87);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(32, 24);
            this.button9.TabIndex = 71;
            this.button9.Text = "9";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.Button9_Click);
            // 
            // chkLock
            // 
            this.chkLock.AutoSize = true;
            this.chkLock.Location = new System.Drawing.Point(153, 237);
            this.chkLock.Name = "chkLock";
            this.chkLock.Size = new System.Drawing.Size(62, 17);
            this.chkLock.TabIndex = 72;
            this.chkLock.Text = "Locked";
            this.chkLock.UseVisualStyleBackColor = true;
            this.chkLock.CheckedChanged += new System.EventHandler(this.chkLock_CheckedChanged);
            // 
            // txtRate
            // 
            this.txtRate.Location = new System.Drawing.Point(26, 240);
            this.txtRate.Name = "txtRate";
            this.txtRate.Size = new System.Drawing.Size(121, 20);
            this.txtRate.TabIndex = 73;
            this.txtRate.Leave += new System.EventHandler(this.txtRate_Leave);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(26, 224);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(81, 13);
            this.label1.TabIndex = 74;
            this.label1.Text = "Exchange Rate";
            // 
            // btnBack
            // 
            this.btnBack.Location = new System.Drawing.Point(26, 48);
            this.btnBack.Name = "btnBack";
            this.btnBack.Size = new System.Drawing.Size(73, 31);
            this.btnBack.TabIndex = 75;
            this.btnBack.Text = "Convert <--";
            this.btnBack.UseVisualStyleBackColor = true;
            this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
            // 
            // btnForward
            // 
            this.btnForward.Location = new System.Drawing.Point(105, 48);
            this.btnForward.Name = "btnForward";
            this.btnForward.Size = new System.Drawing.Size(76, 31);
            this.btnForward.TabIndex = 76;
            this.btnForward.Text = "Convert -->";
            this.btnForward.UseVisualStyleBackColor = true;
            this.btnForward.Click += new System.EventHandler(this.btnForward_Click);
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(143, 157);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(38, 48);
            this.btnClear.TabIndex = 77;
            this.btnClear.Text = "C";
            this.btnClear.UseVisualStyleBackColor = true;
            // 
            // btnPoint
            // 
            this.btnPoint.Font = new System.Drawing.Font("Microsoft Sans Serif", 5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnPoint.Image = ((System.Drawing.Image)(resources.GetObject("btnPoint.Image")));
            this.btnPoint.Location = new System.Drawing.Point(105, 180);
            this.btnPoint.Name = "btnPoint";
            this.btnPoint.Size = new System.Drawing.Size(32, 24);
            this.btnPoint.TabIndex = 78;
            this.btnPoint.Text = ".";
            this.btnPoint.UseVisualStyleBackColor = true;
            this.btnPoint.Click += new System.EventHandler(this.btnPoint_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(218, 273);
            this.Controls.Add(this.btnPoint);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnForward);
            this.Controls.Add(this.btnBack);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtRate);
            this.Controls.Add(this.chkLock);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.Button1);
            this.Controls.Add(this.Button0);
            this.Controls.Add(this.btnBackspace);
            this.Controls.Add(this.txtDisplay);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtDisplay;
        private System.Windows.Forms.Button btnBackspace;
        private System.Windows.Forms.Button Button0;
        private System.Windows.Forms.Button Button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.CheckBox chkLock;
        private System.Windows.Forms.TextBox txtRate;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnBack;
        private System.Windows.Forms.Button btnForward;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnPoint;
        private bool just_cleared = true;
        private double old_rate;
        
        
    }
}
 
its complete code to run in Microsoft Visual Studio 2008
VS is just and IDE that makes writing code easier. once deployed you should not need VS installed, only the .net framework.
i remove so many errors now i dont get any Error in Debug but few strange thing when i run the program[/qoute]
check out the concept/tools related to unit testing. if you can write code in an isolated manner you can test logical segments, without needing all the code. the most popular unit testing frameworks for .net are nUnit and MbUnit.

one other thing which may help (depending on if OOP makes sense to you). separate the UI from the logic. right now you have one huge procedural block of code. I would start by encapsulating the input controls.
Code:
protected virtual decimal Input
{
    get { return Decimal.Parse(this.txtDisplay.Text); }
    set { this.txtDisplay.Text = value.ToString("N"); }
}

protected virtual decimal Rate
{
    get { return Decimal.Parse(this.txtRate.Text); }
    set { this.txtRate.Text = value.ToString("N"); }
}


protected virtual void ResetInput()
{
   Input = 0;
}
you can then use these methods in your event handlers like
Code:
ResetInput();
Input = Input / Rate;
which is much easier to read than what you currently have.
I also see you checking the length of the txtDisplay control for logical operations, but I cannot see why. this may be preventing you from getting the desired result.

finally, name your controls something meaningful.
button1_click doesn't tell you anything about what button1 is or does. however naming the control AddButton or DivideButton immediately tells you what this button is related to.

i also do not see an immediate reason to keep the just_cleared flag.

there is also a try/catch block in the handler txtRate_Leave. remove and let the error bubble up. chances are this is the problem. moving forward you should not just swallow exceptions. at a minimum log it.
Code:
try
{
   if (Rate > 0.0001)
   {
      old_rate = Rate;
      Input = 0;
      just_cleared = true;
   }
   else
   {
      Rate = old_rate;
   }
}
catch(ASpecificException exception)
{
   logger.Error(exception.Message, exception);
   Rate = old_rate;
}
where logger.Error is either your code or a 3rd party logging framework like log4net.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
It helps to step through the program line by line using the debug tools in visual studio.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top