我需要在3个文本框中输入正数 [英] I need to positive number result in 3 text boxes

查看:63
本文介绍了我需要在3个文本框中输入正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个文本框,2个文本框值可能大于零,一个文本框值为0。但我需要3个文本框值结果是积极的。 for.eg:5*5*0=25



 尝试 
{
if (txtNumber.Text == string .Empty)
{
number =(txtNumber.Text == string .Empty || txtNumber.Text == 0)? 0
Convert.ToInt32(txtNumber.Text);

}
else
{
number = Convert.ToInt32(txtNumber.Text);
}

if (txtLength.Text == string 。空)
{
lenght =(txtLength.Text == string .Empty || txtLength.Text == 0)? 0
Convert.ToDouble(txtLength.Text);

}
else
{
lenght = Convert.ToDouble(txtLength.Text);
}
if (txtHeight.Text == string .Empty)
{
height =(txtHeight.Text == string .Empty || txtHeight.Text == 0)? 0
Convert.ToDouble(txtHeight.Text);

}
else
{
height = Convert.ToDouble(txtHeight.Text);
}
if (txtBreadth.Text == string .Empty)
{
breadth =(txtBreadth.Text == string .Empty || txtBreadth.Text == 0)? 0
Convert.ToDouble(txtBreadth.Text);
}
else
{
breadth = Convert.ToDouble(txtBreadth.Text);
}

txtQuantity.Text =(number * lenght * breadth * height).ToString();

}
catch (例外)
{
MessageBox.Show( 发生错误,请再试一次!!!);
}





我的尝试:



我需要在3个文本框中输入正数

解决方案

  if (txtNumber.Text ==  string  .Empty)
{
number =(txtNumber.Text == string .Empty || txtNumber.Text == 0)? 0
Convert.ToInt32(txtNumber.Text);

}



当你已经确定文本框是空的时,检查0或转换为整数是什么意思?您应该在每个文本字符串上使用 int.TryParse ,以便正确验证输入。


这里如何:创建一个数字文本框 [ ^ ]你会发现一个很好的例子来自MSDN来解决您的要求一般和可重复使用。它是从TextBox派生的NumericTextBox。



优势:

a。)您将学习UserControl编程的基础知识。不要害怕,这个例子很小,很容易有概述。

b。)你可以在你的任何其他应用程序中重用该组件。



我希望它有所帮助。


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



只有1个问题:

<前lang =c#> if (txtNumber.Text == string .Empty)
{
number =(txtNumber.Text = = string .Empty || txtNumber.Text == 0)? 0
Convert.ToInt32(txtNumber.Text);

}

可以简化为

  if (txtNumber.Text ==  string  .Empty)
{
number = 0 ;

}


I have 3 textboxes and 2 textboxes value is maybe greater than zero and one text boxes values is 0 . But I need to the 3 textbox values result is positive. for.eg:5*5*0=25

try
           {
               if (txtNumber.Text == string.Empty)
               {
                   number = (txtNumber.Text == string.Empty || txtNumber.Text == "0") ? 0 :
                   Convert.ToInt32(txtNumber.Text);

               }
               else
               {
                   number = Convert.ToInt32(txtNumber.Text);
               }

               if (txtLength.Text == string.Empty)
               {
                   lenght = (txtLength.Text == string.Empty || txtLength.Text == "0") ? 0 :
                   Convert.ToDouble(txtLength.Text);

               }
               else
               {
                   lenght = Convert.ToDouble(txtLength.Text);
               }
               if (txtHeight.Text == string.Empty)
               {
                   height = (txtHeight.Text == string.Empty || txtHeight.Text == "0") ? 0 :
                   Convert.ToDouble(txtHeight.Text);

               }
               else
               {
                   height = Convert.ToDouble(txtHeight.Text);
               }
               if (txtBreadth.Text == string.Empty)
               {
                   breadth = (txtBreadth.Text == string.Empty || txtBreadth.Text == "0") ? 0 :
                   Convert.ToDouble(txtBreadth.Text);
               }
               else
               {
                   breadth = Convert.ToDouble(txtBreadth.Text);
               }

               txtQuantity.Text = (number * lenght * breadth * height).ToString();

           }
           catch (Exception)
           {
               MessageBox.Show("Error occured, Please try again!!!");
           }



What I have tried:

I need to positive number result in 3 text boxes

解决方案

if (txtNumber.Text == string.Empty)
{
   number = (txtNumber.Text == string.Empty || txtNumber.Text == "0") ? 0 :
   Convert.ToInt32(txtNumber.Text);

}


What is the point of checking for "0" or converting to an integer, when you have already established that the textbox is empty? You should just use int.TryParse on each text string so you can properly validate the inputs.


Here How to: Create a Numeric Text Box[^] you will find a great example from MSDN to solve your request generally and reusable. It is a NumericTextBox derived from TextBox.

The Advantage:
a.) You learn the very Basics of UserControl programming. Don't be afraid, this example is small, so easy to have the overview.
b.) You can reuse the component in any other of your applications.

I hope it helps.


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Just 1 problem:

if (txtNumber.Text == string.Empty)
{
    number = (txtNumber.Text == string.Empty || txtNumber.Text == "0") ? 0 :
    Convert.ToInt32(txtNumber.Text);

}

can be simplified to

if (txtNumber.Text == string.Empty)
{
    number = 0;

}


这篇关于我需要在3个文本框中输入正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆