TryParse方法首先会尝试将字符串转换为指定数值类型,如果转换成功 则 返回 True 并对参数中变量赋值。 否则 返回False 语法如下:

double.TryParse(string str, out double result);

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 Example47

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            double P_dbl_value;

            if (double.TryParse(textBox1.Text, out P_dbl_value))

            {

                MessageBox.Show("输入了正确的值!" + P_dbl_value.ToString(), "提示");

            }

            else

            {

                MessageBox.Show("输入的值有误,请重新输入!", "提示");

            }

        }

    }

}

posted on 2011-10-23 13:15  C#_初学者  阅读(239)  评论(0编辑  收藏  举报