C
C#2y ago
FaNim

How to make TextBox numeric and set min and max value to it

private void InstanceNumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsValid(((TextBox)sender).Text + e.Text);
}

public static bool IsValid(string str)
{
return int.TryParse(str, out int i) && i >= 1 && i <= 32;
}
private void InstanceNumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsValid(((TextBox)sender).Text + e.Text);
}

public static bool IsValid(string str)
{
return int.TryParse(str, out int i) && i >= 1 && i <= 32;
}
I've tried something like that but it gives problems
10 Replies
ero
ero2y ago
why does it need to be a textbox? does NumericUpDown not work for your purpose?
FaNim
FaNimOP2y ago
hmm everywhere i see as the best way is to use textbox idk why i didn't know about NumericUpDown i am gonna check this now
ero
ero2y ago
are you using WPF?
FaNim
FaNimOP2y ago
ye
ero
ero2y ago
ah it doesn't have that control built in winforms does
FaNim
FaNimOP2y ago
ah okay thats why i see textbox as only option xd
ero
ero2y ago
FaNim
FaNimOP2y ago
okay it looks good
ero
ero2y ago
maybe something similar works still though try doing as little as possible in code in wpf many things will be necessary, but you can do much more than you think in just xaml
FaNim
FaNimOP2y ago
Okay thank you ❤️
Want results from more Discord servers?
Add your server