通过使用组合框进行选择,我在将文本框中的输入数据转换为原始数据类型时遇到了一些困难。下面是我的代码,代码中没有错误,但我在文本框中输入的数据保持不变。请帮助我以正确的方式进行操作。谢谢你。
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();
}
}
}
}