본문 바로가기
C#/WinForms

NumericUpDown.Value 실시간 동기화하기

by Falto 2023. 6. 4.

NumericUpDown.ValueChanged Event (System.Windows.Forms) | Microsoft Learn

 

NumericUpDown.ValueChanged Event (System.Windows.Forms)

Occurs when the Value property has been changed in some way.

learn.microsoft.com

에 따르면, 

Remarks

For the ValueChanged event to occur, the Value property can be changed in code, by clicking the up or down button, or by the user entering a new value that is read by the control. The new value is read when the user hits the ENTER key or navigates away from the control. If the user enters a new value and then clicks the up or down button, the ValueChanged event will occur twice.

사용자가 엔터 키를 치거나 컨트롤 밖으로 나갈 때 new value가 읽힌다고 한다. 그 말인즉슨 엔터 키를 치거나 컨트롤 밖으로 나가기 전까지는 Value가 그대로라는 뜻이다. 화면에는 12가 입력되어있어도 실제 Value는 24인 경우가 가능하다는 거지. 그래서 키보드를 누르면 실시간으로 동기화가 되는 프로그램을 작성해보았다.

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 WinFormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            var s = (NumericUpDown)sender;
            Text = s.Value.ToString();
        }


        private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
        {
            var s = (NumericUpDown)sender;
            s.Focus();
        }
    }
}

Form1에는 NumericUpDown 하나만 배치해놓은 상태다.

저렇게 하면 키보드로 숫자를 입력하고 KeyUp 이벤트가 발생하면 폼의 제목이 바로 업데이트된다.

'C# > WinForms' 카테고리의 다른 글

IsXValueIndexed  (0) 2023.05.23
C#) KHOpenAPI 한글 안 깨지게 하는 방법  (0) 2023.05.18
WinForms Chart - 0부터 시작하지 않게 하기  (0) 2023.05.15

댓글