0

I have a list of existing products presented in a datagridview. User can add new product using this window field

Some of the fields can be accepted as empty. The text field must have char only and the int fields must have positive int only. ID, price, playtime and status must be positive ints. The rest must be chars, when they aren't empy that is. The code i have works but only when every field that could be empty is empty. It doesn't work if some are and others aren't.

It would also be nice if you could solve the issue of accepting empty int fields.myint.ToString().Length; is not getting the job done seems like. Maybe the answer is easy but I'm sorta new to C# and .Net.

Here is the code i wrote

                if (!plist.type.Any() || !plist.author.Any() || !plist.genre.Any() || !plist.format.Any() || !plist.language.Any() || !plist.platform.Any())
                {
                    if (plist.id != Math.Abs(plist.id) || plist.price != Math.Abs(plist.price)
                        || plist.playtime != Math.Abs(plist.price) || plist.status != Math.Abs(plist.price))
                    {
                        DialogResult = DialogResult.No;
                    }
                    else if (plist.type.Any(char.IsDigit) || plist.name.Any(char.IsDigit)
                        || plist.author.Any(char.IsDigit) || plist.genre.Any(char.IsDigit)
                        || plist.format.Any(char.IsDigit) || plist.language.Any(char.IsDigit) || plist.platform.Any(char.IsDigit)) 
                    {
                        DialogResult = DialogResult.No;
                    }
                    else
                    {
                        DialogResult = DialogResult.OK;
                    }

                }
                else
                {
                    DialogResult = DialogResult.OK;
                }

Let me know if there is anything left to clear up.

I appreaciate any suggestions you got for me!

Abel
  • 15
  • 2
  • I'd suggest you read this answer: [Validating user input / Give .NET controls status OK or NOK](https://stackoverflow.com/a/35993185/3110834); See this [example](https://stackoverflow.com/a/33080822/3110834), and this [blog post](https://www.reza-aghaei.com/dataannotations-validation-attributes-in-windows-forms/). – Reza Aghaei Feb 16 '23 at 16:53
  • It's another way(and better by the looks of it) of doing what i'm trying to do but i think i'll be forced to redo everything, not just the code i've posted here. Is there any way i can get mine to work? I'll still look at your links and see if there is anything i can extract. Thank you! – Abel Feb 17 '23 at 12:40

2 Answers2

1

Assuming that type, author, genre, format, language and platform are mandatory fields, you should be setting DialogResult to DialogResult.No instead of OK in your bottom most else statement. Otherwise, you will be skipping your logic and returning the wrong result.

if (!plist.type.Any() || !plist.author.Any() || !plist.genre.Any() || !plist.format.Any() || !plist.language.Any() || !plist.platform.Any())
{
    if (plist.id != Math.Abs(plist.id) || plist.price != Math.Abs(plist.price)
        || plist.playtime != Math.Abs(plist.price) || plist.status != Math.Abs(plist.price))
    {
        DialogResult = DialogResult.No;
    }
    else if (plist.type.Any(char.IsDigit) || plist.name.Any(char.IsDigit)
        || plist.author.Any(char.IsDigit) || plist.genre.Any(char.IsDigit)
        || plist.format.Any(char.IsDigit) || plist.language.Any(char.IsDigit) || plist.platform.Any(char.IsDigit)) 
    {
        DialogResult = DialogResult.No;
    }
    else
    {
        DialogResult = DialogResult.OK;
    }

}
else
{
    DialogResult = DialogResult.No;
}

As for the empty integer length issue, make sure that you are not trying to turn a null value to a string. Consider using a numericUpDown instead of a textbox as the numericUpDown is designed to work with numbers. You can even set limits to you numbericUpDown, like a lower number limit to prevent negative numbers.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.numericupdown?view=windowsdesktop-7.0

  • Having muliple Dialog.Results was apparently causing the issus. I used a variable instead and set Dialog.Result to that variable depending on the value of the variable. But thanks anyway – Abel Apr 08 '23 at 11:23
0

You can control the input by modifying the textchanged for each textbox. If a user puts in letters on ID for example, you can show an error message and clear the field. Also you shouldn't put multiple of the same Dialog.Results. You should keep it at one for each. I hope this helps. Have a good day.

easy peezy
  • 16
  • 1