【问题标题】:How to enter a default value when a TextBox is empty文本框为空时如何输入默认值
【发布时间】:2013-07-27 23:51:46
【问题描述】:

我有这个method SUM 在那些textboxes 中的值,我想改进它所以如果这些textboxes 中的任何一个是empty 我想插入它"0" 但我没有不知道去哪里以及到底放了什么让它像我想要的那样工作。这个问题我想了很久,请问有大神推荐一下吗?

void vypocti_naklady()
    {
        double a, b,c,d,e,f,g,h;
        if (
            !double.TryParse(p_ub_s.Text, out a) ||
            !double.TryParse(p_poj_s.Text, out b) ||
            !double.TryParse(p_jin_s.Text, out c) ||
            !double.TryParse(p_dop_s.Text, out d) ||
            !double.TryParse(p_prov_s.Text, out e) ||
            !double.TryParse(p_pruv_s.Text, out f) ||
            !double.TryParse(p_rez_s.Text, out g) ||
            !double.TryParse(p_ost_s.Text, out h) 
        )
        {
            naklady.Text = "0";
            return;
        }

        naklady.Text = (a+b+c+d+e+f+g+h).ToString();
    }

感谢大家的帮助和时间。

【问题讨论】:

  • @sircodesalot WinForms,感谢您抽出宝贵时间。

标签: c# winforms methods textbox operand


【解决方案1】:

您可以创建一个文本框验证事件(因为如果为空,您只需插入 0 并且不保留焦点),并将所有其他文本框订阅到该文本框验证事件。

例如:您有 5 个文本框订阅(例如通过单击 textbox1 属性窗口|事件并双击已验证),而其他文本框将其已验证事件订阅到该文本框,然后在其中放入:

private void textBox1_Validated(object sender, EventArgs e)
{
    if (((TextBox)sender).Text == "")
    {
        ((TextBox)sender).Text = "0";
    }
}

【讨论】:

    【解决方案2】:
    private double GetValue(string input)
    {
      double val;
    
      if(!double.TryParse(input,out val))
      {
        return val;
      }
    
      return 0;
    }
    
    var sum = this.Controls.OfType<TextBox>().Sum(t => GetValue(t.Text));
    

    试试上面的。只需在文本框的父级上运行 OfType(父级可能是表单本身)

    这会将任何无效输入计为 0

    【讨论】:

      【解决方案3】:

      试试这个:

      // On the textboxes you want to monitor, attach to the "LostFocus" event.
      textBox.LostFocus += textBox_LostFocus;
      

      这会监视 TextBox 何时失去焦点(已被点击离开)。如果有,则运行以下代码:

      static void textBox_LostFocus(object sender, EventArgs e) {
          TextBox theTextBoxThatLostFocus = (TextBox)sender;
      
          // If the textbox is empty, zeroize it.
          if (String.IsNullOrEmpty(theTextBoxThatLostFocus.Text)) {
              theTextBoxThatLostFocus.Text = "0";
          }
      }
      

      如果有效,您将观看TextBox.LostFocus 事件。然后当用户点击离开框时,它将运行textBox_LostFocus。如果TextBox 为空,那么我们将值替换为零。

      【讨论】:

        【解决方案4】:

        另一种方法是不直接使用 TextBox 文本并对其进行解析,而是将数据绑定到属性并改用它们。 Binding 本身将进行解析和验证,使您的变量始终保持干净并可供使用。

        public partial class Form1 : Form
        {
            // Declare a couple of properties for receiving the numbers
            public double ub_s { get; set; }
            public double poj_s { get; set; }   // I'll cut all other fields for simplicity
        
            public Form1()
            {
                InitializeComponent();
        
                // Bind each TextBox with its backing variable
                this.p_ub_s.DataBindings.Add("Text", this, "ub_s");
                this.p_poj_s.DataBindings.Add("Text", this, "poj_s");
            }
        
            // Here comes your code, with a little modification
            private void vypocti_naklady()
            {
               if (this.ub_s == 0 || this.poj_s == 0 /*.......*/)
               {
                   naklady.Text = "0";
                   return;
               }
               naklady.Text = (this.ub_s + this.poj_s).ToString();
            }
        }
        

        您只需使用已安全键入为double 的属性,而无需考虑格式化和解析。您可以通过将所有数据移动到 ViewModel 类并将逻辑放在那里来改进这一点。理想情况下,您也可以通过数据绑定将相同的想法应用到输出 TextBox,但要实现这一点,您必须实现 INotifyPropertyChanged,以便绑定知道何时更新 UI。

        【讨论】:

          猜你喜欢
          • 2019-10-18
          • 1970-01-01
          • 2012-04-23
          • 2015-09-20
          • 2013-09-02
          • 1970-01-01
          • 1970-01-01
          • 2021-04-07
          • 1970-01-01
          相关资源
          最近更新 更多