如:
Char.IsNumber(ctl.Text.Trim(),2);
可这种方法只能判断textBox中的某个字符,
我想判断整个字符传是否为数值型该如何做???

解决方案 »

  1.   

    try{
      Convert.ToInt(ctl.Text);
      //是数值型
    }
    catch(FormatException)
    {
      //非数值型
    }
      

  2.   

    try

    double d=double.Parse(ctl.Text.Trim());

    catch(Exception ex)
    {
    MessageBox.Show("输入有误:不是数值型!");
    }
      

  3.   

    如果是应用程序,可以自定义一个组件,全部源码如下
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;namespace NumericTextBox
    {
    /// <summary>
    /// UserControl1 的摘要说明。
    /// </summary>
    public class NumericTextBox : System.Windows.Forms.TextBox
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;        //自定义一个回车事件
            public delegate void PressEnter(object sender, EventArgs e);
            public event PressEnter OnPressEnter;        public const int WM_CONTEXTMENU = 0x007b;//右鍵菜單消息 
            public const int WM_CHAR = 0x0102;       //輸入字符消息(鍵盤輸入的,輸入法輸入的好像不是這個消息)
            public const int WM_CUT = 0x0300;        //程序發送此消息給一個編輯框或combobox來刪除當前選擇的文本
            public const int WM_COPY = 0x0301;       //程序發送此消息給一個編輯框或combobox來復制當前選擇的文本到剪貼板
            public const int WM_PASTE = 0x0302;      //程序發送此消息給editcontrol或combobox從剪貼板中得到數據
            public const int WM_CLEAR = 0x0303;      //程序發送此消息給editcontrol或combobox清除當前選擇的內容;
            public const int WM_UNDO = 0x0304;        //程序發送此消息給editcontrol或combobox撤消最后一次操作
            //public const int WM_NEXTDLGCTL = 0x0028;    //下一个控件得到焦点
            public const int WM_GETDLGCODE = 0x0087;    //发送此消息给某个与对话框程序关联的控件,widdows控制方位键和TAB键使输入进入此控件 public NumericTextBox()
    {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent();
                this.MaxLength = 7;
                OnPressEnter = new PressEnter(ReturnEnterKey);
    // TODO: 在 InitComponent 调用后添加任何初始化 } protected override void WndProc(ref Message m)
    {
                switch (m.Msg)
                {
                    case WM_CHAR:
                        System.Console.WriteLine(m.WParam);
                        bool isSign = ((int)m.WParam == 45);
                        bool isNum = ((int)m.WParam >= 48) && ((int)m.WParam <= 57);
                        bool isBack = (int)m.WParam == (int)Keys.Back;
                        //bool isDelete = (int)m.WParam == (int)Keys.Delete;//實際上這是一個"."鍵
                        bool isCtr = ((int)m.WParam == 24) || ((int)m.WParam == 22) || ((int)m.WParam == 26) || ((int)m.WParam == 3);                    if (isNum || isBack || isCtr)
                        {
                            base.WndProc(ref m);
                        }
                        if (isSign)
                        {
                            if (this.SelectionStart != 0)
                            {
                                break;
                            }
                            base.WndProc(ref m);
                            break;
                        }
                        //if (isDelete)
                        //{
                        //    if (this.Text.IndexOf(".") < 0)
                        //    {
                        //        base.WndProc(ref m);
                        //    }
                        //}
                        if ((int)m.WParam == 1)
                        {
                            this.SelectAll();
                        }                    if ((int)m.WParam == 13)
                        {
                            OnPressEnter(this, new EventArgs());
                            //m.Msg = WM_NEXTDLGCTL;
                        }
                        //MessageBox.Show(m.WParam.ToString());
                        break;
                    case WM_PASTE:
                        IDataObject iData = Clipboard.GetDataObject();//取剪貼板對象                    if (iData.GetDataPresent(DataFormats.Text)) //判斷是否是Text
                        {
                            string str = (string)iData.GetData(DataFormats.Text);//取數據
                            if (MatchNumber(str))
                            {
                                base.WndProc(ref m);
                                break;
                            }
                        }
                        m.Result = (IntPtr)0;//不可以粘貼
                        break;
                    //case WM_CUT:
                    //case WM_COPY:
                    //    string miss = "文文小屋输入保护控件";
                    //    Clipboard.SetDataObject(miss,true);
                    //    break;
                    //case WM_CONTEXTMENU:
                    //    MessageBox.Show("文文小屋输入保护控件");
                    //    break;
                    case WM_GETDLGCODE:
                        //使方向键能在不同输入框间切换焦点
                        break;
                    default:
                        base.WndProc(ref m);
                        break;
                }
    }        private void ReturnEnterKey(object sender, EventArgs e)
            {
            }        private bool MatchNumber(string ClipboardText)
            {
                int index = 0;
                //string strNum = "-0.123456789";
                string strNum = "0123456789";            //index = ClipboardText.IndexOf(strNum[0]);
                //if (index >= 0)
                //{
                //    if (index > 0)
                //    {
                //        return false;
                //    }
                //    index = this.SelectionStart;
                //    if (index > 0)
                //    {
                //        return false;
                //    }
                //}            //index = ClipboardText.IndexOf(strNum[2]);
                //if (index != -1)
                //{
                //    index = this.Text.IndexOf(strNum[2]);
                //    if (index != -1)
                //    {
                //        return false;
                //    }
                //}            for (int i = 0; i < ClipboardText.Length; i++)
                {
                    index = strNum.IndexOf(ClipboardText[i]);
                    if (index < 0)
                    {
                        return false;
                    }
                }
                return true;
            } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if( components != null )
    components.Dispose();
    }
    base.Dispose( disposing );
    } #region 组件设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    components = new System.ComponentModel.Container();
    }
    #endregion
    }
    }
      

  4.   

    .net里有服务器控件来做判断啊
      

  5.   

    jxufewbt(我的目标是5星) ( ) 
     
       用正则表达式
    (-?\d*)(\.\d+)?__________________________________
    我听说过正则表达式,可具体是什么意思,该如何用呢?  
     
      

  6.   

    用IsNumericIsNumeric(textbox.text)=true 説明是數值型