-
Jan 12th, 2023, 11:38 AM
#1
Thread Starter
Member
set the max and min value of a NumericUpdown using code
I have a comboBox that stored months of the year(Jan – Dec) and a NumericUpDown with maximum value of 100
I choose the month from comboBox and insert a number into NumericUpDown then press save button.
The issue is that when I choose (June or December) from the comboBox I want the maximum value of NumericUpdown to accept only 30 or less, down to zero , or at least gives a warning message that I should insert the right value which supposed to be 30 or less for June and Dec.
Code:
If ComboBox1.Text = "Jan" Or ComboBox1.Text = "Dec" Then
End If
Last edited by Tajaldeen; Jan 12th, 2023 at 11:43 AM.
-
Jan 12th, 2023, 01:52 PM
#2
Re: set the max and min value of a NumericUpdown using code
Don't forget April and September. Why not use a DateTimePicker which takes care of all that for you? If you must use a NumericUpDown control just change the Maximum value:
Code:
If ComboBox1.Text = "Apr" Or ComboBox1.Text = "Jun" Or ComboBox1.Text = "Sep" Or ComboBox1.Text = "Nov" Then
NumericUpDown1.Maximum = 30
Else
NumericUpDown1.Maximum = 31
End If
You still need to accommodate February and leap years, so the DateTimePicker is definitely the best way to go.
-
Jan 12th, 2023, 06:46 PM
#3
Re: set the max and min value of a NumericUpdown using code
In your ComboBox selectedindes_changed event, put…
Code:
NumericUpDown1.Maximum = DateTime.DaysInMonth(Now.Year, ComboBox1.Text)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2023, 10:17 PM
#4
Thread Starter
Member
Re: set the max and min value of a NumericUpdown using code
Tank you so much , that did the trick perfectly
-
Jan 13th, 2023, 05:37 AM
#5
Re: set the max and min value of a NumericUpdown using code
Originally Posted by Tajaldeen
Tank you so much , that did the trick perfectly
Make sure you specify the correct year. February has 28 days this year, but 29 next year. Other than that, all of the other months have the same number of days every year…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2023, 05:53 AM
#6
Re: set the max and min value of a NumericUpdown using code
It's worth noting that DaysInMonth expects the month as a number from 1 to 12, so you probably ought to be using 'ComboBox1.SelectedIndex + 1'.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|