Compare two textbox in c# - c#

How can I compare two textboxes in c# using greater than or less than. Im working on billing system. I want the cash shoould be bigger than the total. if not. it will pop up messagebox for error. I already try convert to int. to double. to string.
Here's my current code right now.
if (txtCash.TextLength < txtTotal.TextLength){
MessageBox.Show("Cash must me bigger than the total");
txtCash.Focus();
return;
}
Any one can help me how?
I know text length is wrong. but im just using it.

I'm working on billing system. I want the cash should be bigger than
the total. if not. it will pop up message box for error.
You might be looking for something like this:
if (double.Parse(txtCash.TextLength) <= double.Parse(txtTotal.TextLength))
{
MessageBox.Show("Cash must me bigger than the total");
txtCash.Focus();
return; // i dont know why you put this here but i'll leave it there anyway.
}
else
{
var amount = double.Parse(txtCash.TextLength);
// DO something
}
You may want to further validate the user input if you wish. Example, if the user doesn't enter numbers...
Better ways to achieve your task in the most robust way possible, you should consider the below links:
How do I make a textbox that only accepts numbers?
double.TryParse(...)
Update:
Seems, you're not sure about the keyword var. I suggest you have a deep read of the MSDN reference page for var.

Related

How do I add decimals in Xamarin Forms Entries using C#

For starters I'm very very new to writing code! :)
What I have so far...
So far I've used Xamarin.Forms to create a user interface for a sort of specialized calculator. I'm using a Grid Layout containing: a first column of Labels, a second column of Entries (that I have named in Xaml), and a third column of Steppers (so I can change the entries by typing or using the stepper). These 3 views on each row repeat for several rows with different label text on each row and at the bottom of the Grid Layout I have an Entry for the output.
The problem...
Basically, I want to buy a certain product at different weights and prices...among other criteria....and I want to quickly figure out how much money I'll make at a future possible sale price. Simply put... I'm trying to add/subtract/multiply/divide using Xamarin.Forms Entries. I've looked everywhere and can't seem find anyone giving an example of how to do this. I've tried different methods and usually end with an error of not being able to convert the Xamarin.Forms entry to a string...So I'm back to zero. Can I get an example of a Method where I would be able to add/subtract/multiply/divide 2 Xamarin.Forms Entry views together in the C# code behind? This seems very simple to me...what am I missing??? Is there a thread/article/video somewhere that I haven't found that covers this?? And like I said, I'm very new so the answer is probably very simple.
Thanks in advance!
Steven
Entries deal with strings, not numeric values, so you need to convert them before doing calculations.
var amount = Decimal.Parse(EntryA.Text);
var price = Decimal.Parse(EntryB.Text);
var total = amount * price;
// you can use a format string as an argument to ToString()
// to control the output - ie, how many decimals, commas, etc
LabelTotal.Text = total.ToString();
In a real app you will want to validate the input in case the user enters text instead of a value number (the Parse method will throw an exception if the input is bad);

C# user int array declaration

I am having issues with c# (new to it) but with some programming experience (not great, many moons ago, Python)
I must be doing something wrong and looking for the answer incorrect, spent a couple of days searching for this. I think its really simple and I must be doing something wrong.
Basically I want to take a user input of an interger and store it as an array so I perform maths on individual numbers. However, when I try and get the console to read the number I can not then access the 4th position etc because as c# keeps telling me.
Cannot apply indexing with [] to an expression of type int
what elemental mistake am I making here guys,
Edited to improve original question.
sorry all, that was a little vague. Let me show you how my idea works in python and maybe that would help matters. Basically, i'm trying to access the positions of an integer.
Number = (input("Please enter a Number number"))
check_digit = number[7]
loyalty = number[:-1]
I want the user to enter the number and then the program access different numbers in the array.
Judging from the exception, you probably declare you Array like this:
int myArray;
Instead of this:
int[] myArray;
Error 1 Cannot apply indexing with [] to an expression of type 'int'

Windows Forms - customizing MaskedTextBox

I need to use basic functionality of the MaskedTextBox. I can get use of the 5 digit mask but there are few things that I want to change. Right now the box is looking like this:
and there are two thing I don't like. First - the Prompt char which is undersoce _. I deleted the field value in order to leave it empty (as I would like it to appear) but this gives an error - The property value is invalid. So is there a way to get rid of these underscores? And second - I use this value for one of my entity properties which is of integer type so I make a convertion :
if (txtNumOfAreas.Text != "")
{
string temp = txtNumOfAreas.Text;
bool result = Int32.TryParse(temp, out convertValue);
if (result)
{
entity.AreasCnt = convertValue;
}
else
{
MessageBox.Show(Resources.ERROR_SAVE, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
else
{
entity.AreasCnt = null;
}
which works fine unless someone decides to make experiments and insert something like _1__5_ then the conversion fails, but at first place I would like to make possible to write the digits only one after another. So is this possible too?
It looks like your MaskedEdit is more trouble than it's worth to deal with your particular range of issues. A better control to use might be the NumericUpDown.
The upside to NumericUpDown:
There are no underscore prompts to try and get rid of
It will only accept numeric input
So there is no need to try and convert the value. You will always have an integer
Setting Minimum and Maximum value properties gives you automatic data entry validation
Provides multiple modes of data entry:
Typing
Clicking up/down buttons with a mouse
Up/down with keyboard
If you like, you could hide the up/down buttons altogether: C# WinForms numericUpDown control (removing the spin box)
So to get the _ to be a space you just need to set the mask character to a single space. And to resolve the conversion error, just set the AllowPromptAsInput property to false so that the user can't actually end up with something like your example.

how can I take the value of a NumericUpDown in C# and use it for something ex. use as an int

I'm making a Higher Lower game, and the number of tries the user has to guess the computer's number, I want to set using a NumbericUpDown.
How can I do this?
(Should I paste my code here so you can see what I have so far?)
Set the Value property of the control? See the examples here.
For example,
this.upDown.Value = 5;

Test for filled textboxes and correct type of data

I am working in C#, Windows Form Application.
I have 6 text boxes that accept user input. When the 'Submit' button is clicked it stores the values in an array. I want to create something to check the text boxes for two things 1.) there is something in the text boxes and 2.) the information is only a number, nothing else.
I have been told that a try catch loop will be able to do this, but I have yet to figure out how.
Thank you for any help you can provide
You do not want to use a try catch block for this - you should not use exceptions for standard application flow as it is a relatively expensive option.
Checking if it is empty
string.IsNullOrEmpty(textbox1.Text);
Checking if it is a number
int result;
int.TryParse(textbox1.Text), out result);
// In button_click event or somewhere like that
if (!this.CheckInput(txt_TextBox1))
return;
if (!this.CheckInput(txt_TextBox2))
return;
if (!this.CheckInput(txt_TextBox3))
return;
// Everything OK, do something
Then a method like this:
private bool CheckInput(TextBox textbox) {
int test;
if (!int.TryParse(textbox.Text.Trim(), out test)) {
MessageBox.Show("Invalid input");
return false;
}
return true;
}
To check if something is in the textbox you can call the string.IsNullOrEmpty method.
As for checking if a Textbox only contains numeric values, you might want to consider preventing users from entering non-numeric values. This would be a more user friendly approach than preventing it after entering the values.
You can use isNullOrEmtpy as Bas suggested above in order to check if there is any user input.
For numeric numbers only, i suggest you use Regex. You can find a solution here

Categories

Resources