C
C#17mo 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
ero17mo ago
why does it need to be a textbox? does NumericUpDown not work for your purpose?
FaNim
FaNim17mo 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
ero17mo ago
are you using WPF?
FaNim
FaNim17mo ago
ye
ero
ero17mo ago
ah it doesn't have that control built in winforms does
FaNim
FaNim17mo ago
ah okay thats why i see textbox as only option xd
ero
ero17mo ago
FaNim
FaNim17mo ago
okay it looks good
ero
ero17mo 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
FaNim17mo ago
Okay thank you ❤️
Want results from more Discord servers?
Add your server
More Posts