【问题标题】:TextBox entry Validation based on SQL data object基于 SQL 数据对象的 TextBox 条目验证
【发布时间】:2014-08-06 19:56:02
【问题描述】:

我正在验证用户输入的文本,输入到文本框中,以过滤掉非数字,然后将 >= /

   int classRPM;
        int fanRPM;
        string actualdata = string.Empty;
        char[] entereddata = txfanrpm.Text.ToCharArray();
        foreach (char aChar in entereddata.AsEnumerable())
        {
            if (Char.IsDigit(aChar))
            {
                actualdata = actualdata + aChar;

                using (Fanrpm ds = new Fanrpm(cbdesigntype.SelectedValue.ToString(), cbfansize.SelectedValue.ToString(), cbfanclass.SelectedValue.ToString()))
                {
                    DataTable dt = ds.dataset.Tables[0];
                    classRPM = Convert.ToInt32(dt.Rows[0].Field<string>("ClassRPM"));

                   // MessageBox.Show(aChar.ToString());
                    fanRPM = Convert.ToInt32(actualdata);
                    if (fanRPM >= classRPM)
                    {
                        MessageBox.Show("hi");
                    }
                    else if (fanRPM < classRPM)
                    {
                        MessageBox.Show("Hide");
                    }
                }
            }
            else
            {
                MessageBox.Show(aChar + " is not numeric");
                actualdata.Replace(aChar, ' ');
                actualdata.Trim();
            }
        }
        txfanrpm.Text = actualdata;

【问题讨论】:

  • “继续跳过我的 if /else 语句”是什么意思?
  • 因为在程序中没有单步执行它来显示 hi 的消息框或隐藏。 IT 确实会执行第一个和最后一个 if/else,因为任何非数字都会提示 * is not numeric else。
  • 您是否将此代码包装在异常处理程序中?你是在单步执行代码吗?当你有你的MessageBox.Show(aChar.ToString()); 代码时,你有没有看到一条消息?
  • 使用调试器。在 for 行 (F9) 上设置断点,然后使用 F10 单步执行您的代码。请务必检查 DEBUG->Exceptions->Common Language Runtime Exceptions for User Unhandled。当错误发生时,您应该能够在异常消息属性中看到原因。
  • 那段代码 (MessageBox.Show(aChar.ToString())) 是一个错误,它被取消注释,而它被取消注释它什么也没做,也被跳过了,还没有错误处理.

标签: c# sql winforms


【解决方案1】:

在上面的 cmets 之后,我认为您可以通过这种方式更改您的代码

// If the reading from the database gives always the same value is not correct to 
// exexute this code inside the foreach. Just do it one time here and go on....

float classRPM = 0.0f;
using (Fanrpm ds = new Fanrpm(cbdesigntype.SelectedValue.ToString(),
                              cbfansize.SelectedValue.ToString(), 
                              cbfanclass.SelectedValue.ToString()))
{
     DataTable dt = ds.dataset.Tables[0];
     classRPM = dt.Rows[0].Field<float>("ClassRPM");
}

float fanRPM;
string actualdata = string.Empty;

// No need to use AsEnumerable.... 
// And also this code could be easily replaced by single line float.TryParse 
// if you don't need to show a message box for every wrong char....
foreach (char aChar in txfanrpm.Text.ToCharArray())
{
    if (Char.IsDigit(aChar))
        actualdata = actualdata + aChar;
    else
        MessageBox.Show(aChar + " is not numeric");
}
// Now you could start your comparisons....
fanRPM = Convert.ToSingle(actualdata);
if (fanRPM >= classRPM)
   MessageBox.Show("hi");
else
   MessageBox.Show("Hide");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-29
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多