(wL) Forums
-C#- Numeric Input - Printable Version

+- (wL) Forums (https://war-lords.net/forum)
+-- Forum: Resources (https://war-lords.net/forum/forum-31.html)
+--- Forum: Programming (https://war-lords.net/forum/forum-33.html)
+--- Thread: -C#- Numeric Input (/thread-5029.html)



-C#- Numeric Input - MindHACKer - Nov 20 2011

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!");
        }

     
    }
}



RE: -C#- Numeric Input - Helices - Nov 29 2011

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!


RE: -C#- Numeric Input - MindHACKer - Nov 29 2011

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.