Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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
Posted
Updated 28-Jul-16 9:04am
Comments
ZurdoDev 28-Jul-16 11:48am    
Where are you stuck?
PeejayAdams 28-Jul-16 11:50am    
I can see plenty of problems there. Firstly, don't validate a number directly from a string - you check for "0" but "0.00" ".0" and many other things equate to zero. Your test for "0" is actually negated anyway as you effectively say "if (empty) ... if ([something we already know is true] or [it doesn't matter what goes here because we already have a true]) Your try ... catch block will only catch exceptions and you could be generating a fair few of those - how would you know where the exception came from? What about negative values?

In all honesty, I'm a bit confused by the whole thing and your question doesn't really give much clue as to what you're trying to achieve but if all you want to validate is that the product of the three is positive, you only need to test the product not look for individual zeroes.

C#
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.
 
Share this answer
 
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.
 
Share this answer
 
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:
C#
if (txtNumber.Text == string.Empty)
{
    number = (txtNumber.Text == string.Empty || txtNumber.Text == "0") ? 0 :
    Convert.ToInt32(txtNumber.Text);

}
can be simplified to
C#
if (txtNumber.Text == string.Empty)
{
    number = 0;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900