Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
-C#- Numeric Input
#1
How to make it possible to use different Numeric Variables in the Input process:
As we all know the only control that could be used to enter input like "Characters & Numbers & ..etc" is the Text-box, the problem is that the program cant process mathematics processes because he thinks that the Text-box is a String only, that's why you get an error when you sub 2 numbers or divide others.

..this is a Sample on how to solve the issue:

Code:
float doc = Convert.ToSingle(textBox1.Text) / Convert.ToSingle(textBox3.Text);
textBox4.Text = doc.ToString();

So the main idea is to convert it back to string in the end.

Here is a full "Simple Calculator" i made from scratch...

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
           
            switch (textBox2.Text)
            {
                case "":
                    MessageBox.Show("Enter +,-,*,/ Only");
                    break;

                default:
                    int x = Convert.ToInt32(textBox1.Text);
                    int y = Convert.ToInt32(textBox3.Text);
                    int z;
                    char t = Convert.ToChar(textBox2.Text);

                    switch (t)
                    {
                        case '+':
                            z = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox3.Text);
                            textBox4.Text = z.ToString();
                            break;
                        case '-':
                            z = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox3.Text);
                            textBox4.Text = z.ToString();
                            break;
                        case '*':
                            z = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox3.Text);
                            textBox4.Text = z.ToString();
                            break;
                        case '/':
                            float doc = Convert.ToSingle(textBox1.Text) / Convert.ToSingle(textBox3.Text);
                            textBox4.Text = doc.ToString();
                            break;
                    }
                    break;
            }

                   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("NeptuneHACK!");
        }

     
    }
}
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]
#2
I need to sharpen up on c#..

I'd have used type-casting for this in a C based program!

But clearly there are some large differences in programing styles.!

This would prove quite useful! Thanks!
#3
Helices Wrote:I need to sharpen up on c#..

I'd have used type-casting for this in a C based program!

But clearly there are some large differences in programing styles.!

This would prove quite useful! Thanks!
NP, Glad I could help.
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Users browsing this thread: 1 Guest(s)