-1
if (double.TryParse(txtDays.Text, out days) == false)
{
   MessageBox.Show("Enter a whole number for days.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
   txtDays.Focus();
   return;
}

I'm a newbie at C# so any help would be appreciated :)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
k3llyjam
  • 1
  • 1

3 Answers3

2

You want to prevent that the user enters a decimal-separator? So he is allowed to use integers only? Then don't try to parse to double but to int:

if (!int.TryParse(txtDays.Text.Trim(), out days))
{
   MessageBox.Show("Enter a whole number for days.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
   txtDays.Focus();
   return;
}

Maybe it's even better to use uint.TryParse since that prevents the - sign, but that depends on if negative days are possible.

But you should consider to use the NumericUpDown-control

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

There are multiple approaches and all are covered here:
How do I make a textbox that only accepts numbers?

Most common approach is to use a NumericUpDown control rather than a TextBox.

If you want a message box, Tim's answer is pretty much exactly what you need.

Community
  • 1
  • 1
bokibeg
  • 2,081
  • 16
  • 23
-1

You would be better to use a regular expression to check for this I suspect. The following code:

Regex regex = new Regex(@"^\d$");

will check that the string is an integer, well actually it checks that a single digit is an integer, the link below shows how to amend it to check a full string.

(The code comes from this question: Regex for numbers only)

Community
  • 1
  • 1
Pheonyx
  • 851
  • 6
  • 15
  • Regex for decimal parsing? o.O – Soner Gönül Mar 09 '15 at 15:54
  • @SonerGönül, no to confirm that what is entered is an integer not a decimal as per his "Validate user does not enter a decimal". If it passes the regex then it is valid, else it's not. – Pheonyx Mar 09 '15 at 15:55