注册 登录
编程论坛 C# 论坛

VS编程清空textbox出现错误,输入字符串的格式不正确。

haojiayong 发布于 2022-03-12 02:18, 1187 次点击
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Double F, Y, f, L, δ = 0, ΔL1, ΔL2, ΔL, Result ,a;
        String Flag;  


        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            F = Convert.ToDouble(textBox2.Text);

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            f = Convert.ToDouble(textBox3.Text);
            
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            L = Convert.ToDouble(textBox4.Text)*1000;
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //string str = textBox1.Text;
            ΔL1 = (F * δ * Math.Pow(L, 2)) / (Math.Pow(f, 2) + F * δ * L);
            ΔL2 = (F * δ * Math.Pow(L, 2)) / (Math.Pow(f, 2) - F * δ * L);
            ΔL = ΔL1 + ΔL2;
            //a = F * δ * Math.Pow(L, 2);
            //Result = F + δ;
            MessageBox.Show(Convert.ToString(ΔL));
            //MessageBox.Show(Convert.ToString(ΔL1));
            //MessageBox.Show(Convert.ToString(ΔL2));
            //MessageBox.Show(Convert.ToString(ΔL));
        }


        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }


        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            //if (textBox3.Text.Trim() != "")
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            //if (textBox2.Text.Trim() != "")
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            //if (textBox4.Text.Trim() != "")
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }
1 回复
#2
apull2022-03-12 19:30
清空直接用
textBox4.Text= ""
1