Results 1 to 6 of 6

Thread: set the max and min value of a NumericUpdown using code

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2022
    Posts
    45

    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.

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    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)

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2022
    Posts
    45

    Re: set the max and min value of a NumericUpdown using code

    Tank you so much , that did the trick perfectly

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: set the max and min value of a NumericUpdown using code

    Quote Originally Posted by Tajaldeen View Post
    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…

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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
  •  



Click Here to Expand Forum to Full Width