[C# 윈폼] NumericUpDown 컨트롤 최대값, 최소값 지정하기

안녕하세요.

 

오늘은 C# 윈폼에서 NumericUpDown 컨트롤 사용 방법에 대해서 알아보려고 합니다.

 

그 중에서도 최대값, 최소값을 지정해서 해당 최대값 혹은 최소값을 넘어가면 다시 값들은 초기화 시켜주는 것까지 하는 방법을 예제 코드를 통해서 보여드리도록 하겠습니다.

 

먼저 아래와 같이 빈 윈폼 프로젝트를 생성해 주시고, NumericUpDown 컨트롤을 배치해 주시기 바랍니다.

 

 

빈 윈폼 프로젝트 생성 및 NumericUpDown 컨트롤 배치

위와 같이 컨트롤을 배치해 주셨다면, 이제 비하인드 소스코드를 아래와 같이 작성해 주시기 바랍니다.

 

예제 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace NumericUpDownTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            //Max = 10, Min -1 로 지정
            this.uiNumeric_Main.Maximum = 26;
            this.uiNumeric_Main.Minimum = -1;
 
            //NumericUpDown 컨트롤 값 변경 이벤트 선언
            this.uiNumeric_Main.ValueChanged += UiNumeric_Main_ValueChanged;
        }
 
        /// <summary>
        /// Numeric Up Down 컨트롤 최대값, 최소값 설정 이벤트 핸들러
        /// </summary>
        /// <param name="sencer"></param>
        /// <param name="e"></param>
        private void UiNumeric_Main_ValueChanged(object sencer, EventArgs e)
        {
            //Max는 20
            if (this.uiNumeric_Main.Value > 20)
            {
                this.uiNumeric_Main.Value = 0;
            }
 
            if (this.uiNumeric_Main.Value < 0)
            {
                this.uiNumeric_Main.Value = this.uiNumeric_Main.Maximum - 1;
            }
        }
    }
}
 
cs
실행 결과

 

위와 같이 해당 NumericUpDown 컨트롤을 Up, Down 해보시면 숫자 20을 넘어가게 되면 다시 0으로 초기화 되고, 0 아래 값으로 Down 하게 되면 Down이 안되고 그대로 0 값으로 유지하는 것을 확인하실 수 있습니다.

 

감사합니다.^^

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY