문제

I faced some difficulties in converting input data in textbox to primitive data types by selecting with combobox. Below is my code, there is no error in the code but the data i input in textbox just remains the same. Please assist me in doing it the right way. Thank you.

    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 WindowsFormsApplication1
    {

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

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.Add("Int");
        comboBox1.Items.Add("Double");
        comboBox1.Items.Add("Decimal");
        comboBox1.Items.Add("Float");
        comboBox1.Items.Add("String");
        comboBox1.Items.Add("Long");
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.ComboBox1_SelectedIndexChanged);
    }

    private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        calculate(((ComboBox)sender).SelectedItem.ToString());
    }

    private void calculate (string sign)
    {

        switch (sign)
        { 
            case "Int":
                System.Convert.ToInt32(textBox1);
                int.Parse(textBox1.Text);
                break;
            case "Double":
                System.Convert.ToDouble(textBox1.Text);
                double.Parse(textBox1.Text);
                break;
            case "Decimal":
                System.Convert.ToDecimal(textBox1.Text);
                decimal.Parse(textBox1.Text);
                break;
            case "Float":
                System.Convert.ToSingle(textBox1.Text);
                Single.Parse(textBox1.Text);
                break;
            case "String":
                System.Convert.ToString(textBox1.Text);
                break;
            case "Long":
                System.Convert.ToInt64(textBox1.Text);
                long.Parse(textBox1.Text);
                break;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}
}
}
도움이 되었습니까?

해결책

First off, you're attempting to parse the textbox itself into these types, not the value inside the textbox. You should be using the Text property on the textbox to get the value:

int.Parse(textBox1.Text); //Note the .Text

Second, you're not assigning the result of the operation to the textbox. To assign a value to a textbox, again use the Text property:

textBox1.Text = int.Parse(textBox1.Text).ToString();

Third, this is going to fail if what is inside the textbox is not valid for the type you select. For instance, if you enter "hello" into the textbox and select int the code will crash because you can't parse "hello" into an int. You can remedy this by catching a FormatException.

try
{
    int.Parse(textBox1.Text);
}
catch (FormatException fe)
{
    //Display an error
}

Alternatively you can use the TryParse methods on each type (int.TryParse)

Fourth, in some instances you won't notice any thing change anyway. If you enter 123 into the textbox and parse it to an int and replace the textbox value, it will still say 123 because that's what the string representation of the int value 123 is.

Fifth, what are you trying to accomplish here? This code basically does nothing.

다른 팁

These are some of the methods you are using:

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

http://msdn.microsoft.com/en-us/library/b3h1hf19.aspx

Notice in the documentation that it mentions a return value.

These functions don't affect anything in your application. They just return to you the result of an operation. It's up to you to do something with the result.

If you want to update your TextBox with the result of the conversion operation, then you need to assign it like so:

textBox1.Text = Int32.Parse(textBox1.Text);

That said:

1) Never use object names like textBox1.

2) You don't need to call both Convert.ToXXX() and XXX.Parse(), you want to do one or the other.

3) Consider using the TryParse method instead of the Parse method, or add some exception handling. As it is right now if the user enters something in the TextBox which can't be converted to that type you will throw an unhandled exception.

int.Parse(textbox.Text), double.Parse(textbox.Text), etc will not change values in textbox.Text. These method will return the value in numeric type. If you want to reassign the value back to the textbox.Text, you will have to do

textbox.Text = int.Parse(textbox.Text).toString();

But, this code will do nothing useful.

Better usage of Parse method would be something like this:

int a = int.Parse(textbox1.Text);
int b = int.Parse(textbox2.Text);
textbox3.Text = (a+b).toString();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top