none
question RRS feed

  • Question

  • Hello,

    I'm making a converter program in C# but it isn't working, can somebody help me please.

    I want that if you have entered a number and hit enter the answer will be displayed, this is my code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                int i = int.Parse(amound_txt.Text);
                var km = i / 1.609344;
                display_txt.Text = km.ToString();
            }
        }
    }

    Thank you for helping me,

    Silke.

    Thursday, February 2, 2017 2:37 PM

All replies

  • Hello there.

    It would help if, when posting, you said exactly what "isn't working"!

    What you have looks basically ok; the only immediate problems I can see are:

    1) Do you expect your user to enter fractional amounts, such as 3.2?

    Because at the moment your code only allows for whole numbers. And...

    2) You are not handling error situations such as if the user enters something other than a numeric value.

    To allow fractional values, use a double instead of an int:

    double i = double.Parse(amound_txt.Text);

    To enforce the user enters only a number you could use a MaskedTextBox rather than a TextBox and set the Mask property (though I personally think the Windows Forms MaskedTextBox control is a bit...messy!). Alternately, check the input by using TryParse rather than Parse:

    double i;
    
    if (double.TryParse(textBox1.Text, out i))
    {
       var km = i / 1.609344;
       textBox2.Text = km.ToString();
    }
    else
    {
       MessageBox.Show("Please enter a number!");
    }


    Thursday, February 2, 2017 2:53 PM
  • What's not working?  Just the enter key?  Right now (assuming the event handlers are added correctly) the code looks good, in that it will do it's job when you click button1.

    To handle the enter key, add a KeyDown handler to your text box (amound_txt) and then do you calculation if e.KeyCode == Keys.Enter.

    You'll probably also want to refactor so that the calculation is done in a common method that both button1_Click and amound_txt_KeyDown can call.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click( object sender, EventArgs e )
        {
            Calc();
        }
    
        private void amound_txt_KeyDown( object sender, KeyEventArgs e )
        {
            if( e.KeyCode == Keys.Enter ) {
                Calc();
            }
        }
    
        private void Calc()
        {
            int i = int.Parse( amound_txt.Text );
            var km = i / 1.609344;
            display_txt.Text = km.ToString();
        }
    }
    

    You can add method handlers in the designer by hitting F4, and checking the events button in the properties pane (lightning bolt icon) and double clicking the event (Such as KeyDown)

    If you did it correctly, then your Form1.Designer.cs should have these lines somewhere:  (Don't type them manually,. Use the designer, and these lines will be automatically generated.)

                this.button1.Click += new System.EventHandler(this.button1_Click);
    
                this.amound_txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.amound_txt_KeyDown);
    
    


    Thursday, February 2, 2017 2:56 PM
  • No its still not working (if I press the enter button nothing happens)
    Thursday, February 2, 2017 3:48 PM
  • No its still not working (if I press the enter button nothing happens)

    Your program will only convert the number when the button is clicked, not when the enter button is pressed.
    Thursday, February 2, 2017 3:56 PM
  • yes but that is what I'm doing
    Thursday, February 2, 2017 4:12 PM
  • yes but that is what I'm doing

    Then I suspect you haven't hooked up to the button1_Click event.

    Highlight the button in question. Look at the properties and list the available events (the icon looks like a lightning rod). Double-click on the Click event and add the button1_Click method as the callback function.

    Thursday, February 2, 2017 4:26 PM
  • yes but that is what I'm doing

    Then I suspect you haven't hooked up to the button1_Click event.

    Highlight the button in question. Look at the properties and list the available events (the icon looks like a lightning rod). Double-click on the Click event and add the button1_Click method as the callback function.

    You can manually add this line to your Form1 Constructor to see if it fixes it:

      public Form1()
        {
            InitializeComponent();
            this.button1.Click += new System.EventHandler(this.button1_Click);
        }


    Friday, February 3, 2017 4:11 AM
  • Hi Silke29,

    There is a way to do it. Set the button as the window accept button.

    Details reference the gif at this https://social.msdn.microsoft.com/Forums/getfile/997159 link.

    Hope it helpful.

    Friday, February 3, 2017 7:09 AM