`private void Dot_Click_1(object sender, EventArgs e)
{
if (NumBox1.Text.IndexOfAny(char['.'])
{
}`
I think the solution for the restriction of one decimal point is here.
if (!string.IsNullOrEmpty(NumBox1.Text)
{
numbox1.text = "0" + ".";
}
}
this is when the textbox is empty. Then I clicked dot sign to get automatically a result of "0." inside the textbox. But, it only returns "."
It's not clear why you've got char['.']
at all, or what you expect it to mean. I suspect you just want the character literal '.'
and use IndexOf
.
else if (NumBox1.Text.IndexOf('.') == -1 && ...)
You only want to use IndexOfAny
if you're looking for multiple things, in which case you'd want something like:
IndexOfAny(new[] { '.', ',' })
Or even more simply:
else if (!NumBox1.Text.Contains(".") && ...)
I strongly suspect that your conditions really aren't what you want - basically at the moment you'll always set the textbox value to "0." if you don't have a dot (ignoring any previous input), and NumBox1.Text
will never be null
- but you need to work through that for yourself.
EDIT: Using a single call to IndexOf
isn't going to tell you if there's more than one occurrence of .
. One simple way to do that is:
if (text.IndexOf('.') != text.LastIndexOf('.'))
{
...
}
See more on this question at Stackoverflow