简体   繁体   English

将光标置于错误的文本框上

[英]Position cursor on textbox that has error

I have a simple form that takes 9 decimal numbers from 9 textboxes and I put some validation so that the users can only enter decimal numbers and nothing else. 我有一个简单的表单,它从9个文本框中获取9个十进制数字,并且进行了一些验证,以便用户只能输入十进制数字,而不能输入其他任何内容。 Now the challenge I'm having is how to set the cursor in the textbox that had no decimal number after showing the error message in the try-catch statement? 现在,我面临的挑战是,在try-catch语句中显示错误消息后,如何在没有小数的文本框中设置光标?

Here's my code: 这是我的代码:

  private void btn_Aceptar_Click(object sender, EventArgs e)
  {
      POI GPI = new POI();
      POI VIM = new POI();
      POI ST = new POI();

      try
      {
          GPI.POI_x = Convert.ToDecimal(txt_GPIx.Text);
          GPI.POI_y = Convert.ToDecimal(txt_GPIy.Text);
          GPI.POI_z = Convert.ToDecimal(txt_GPIz.Text);
          VIM.POI_x = Convert.ToDecimal(txt_VIMx.Text);
          VIM.POI_y = Convert.ToDecimal(txt_VIMy.Text);
          VIM.POI_z = Convert.ToDecimal(txt_VIMz.Text);
          ST.POI_x = Convert.ToDecimal(txt_STx.Text);
          ST.POI_y = Convert.ToDecimal(txt_STy.Text);
          ST.POI_z = Convert.ToDecimal(txt_STz.Text);
      }
      catch (Exception)
      {
          MessageBox.Show("Ingrese solamente números en las variables GPI/VIM/ST", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//Set the cursor in the first textbox that had no decimals..
          return;
      }    
      Comisurales Comisurales = new Comisurales();
      Comisurales.calculo_coord_comisurales(PC, AC, IHP, GPI, VIM, ST);                
  }

Let me add that I also have a function to ensure the user is only limited to enter decimals but I wasn't able to figure how to avoid the "." 我还要补充一点,我还有一个功能可以确保仅限制用户输入小数,但我无法弄清楚如何避免使用“。” only or this for example: "1." 仅此或例如:“ 1”。

As an addition to my question, here's what gets validated every time the user press a key in the textbox: 除了我的问题外,这是每次用户在文本框中按下键时都会验证的内容:

  private void ValidarDecimal(object sender, KeyPressEventArgs e)
  {
      // permitir 0-9, backspace, y decimal
      if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46))
      {
           e.Handled = true;
            return;
      }

      // chequear solamente un decimal
      if (e.KeyChar == 46)
      {
          if ((sender as TextBox).Text.IndexOf(e.KeyChar) != -1)
               e.Handled = true;
      }
  }

I guess I have 2 ways to resolve my issue. 我想我有2种方法可以解决我的问题。 Number one would be find a way to ensure the user never ever enters something weird in the textbox (which I've done partially) and number 2 would be to use the try-catch with the current limitations I mentioned above and then point the user to the textbox that has issues, both are acceptable. 排名第一的方法是确保用户永远不会在文本框中输入任何奇怪的内容(我已部分完成),而排名第二的方法是使用try-catch并使用上面提到的当前限制,然后指向用户到有问题的文本框,都可以接受。

The Decimal class has a TryParse method that could be used to avoid all this logic driven by catching exceptions (a very expensive approach in terms of performance) Decimal类具有TryParse方法,该方法可用于避免所有由捕获异常驱动的逻辑(就性能而言,这是一种非常昂贵的方法)

decimal value;
if(decimal.TryParse(txt_GPIx.Text, out value))
    GPI.POI_x = value;
else
{
    MessageBox.Show("Invalid decimal value");
    txt_GPIx.Focus();
}

Of course this code needs to be repeated for every control in your list, but you could write a generic function like this one 当然,列表中的每个控件都需要重复此代码,但是您可以编写像这样的通用函数

private decimal GetValueAndValidate(Textbox txt, out bool isOK)
{
    isOK = true;
    decimal value = 0m;
    if(!decimal.TryParse(txt.Text, out value))
    {
        MessageBox.Show("Invalid decimal value");
        txt.Focus();
        isOK = false;
    }
    return value;
}

and then use the following approach in your code inside the button click 然后在代码中使用以下方法在按钮内单击

 bool isOK = true;
 if(isOK) GPI.POI_x = GetValueAndValidate(txt_GPIx, out isOK);
 if(isOK) GPI.POI_y = GetValueAndValidate(txt_GPIy, out isOK);
 .... and so on for the other fields ....

For the second part of your question, finding a way to completely control the input logic is not easy. 对于问题的第二部分,找到一种完全控制输入逻辑的方法并不容易。 What happens for example if your user PASTE an invalid text in your textbox? 例如,如果您的用户粘贴文本框中的无效文本,会发生什么情况? There are very edge case situations that takes a lot of effort to code correctly. 在某些极端情况下,需要付出很多努力才能正确编码。 It is a lot more easy to leave freedom of typing to your user and apply a strict logic when you get that input. 将输入的自由留给用户并在输入时应用严格的逻辑要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM