728x90
반응형
728x170
▶ TextBox 클래스 : 숫자 텍스트 박스 만들기 예제
NumericTextBox numericTextBox = new NumericTextBox();
...
numericTextBox.IntegerPartLength = 20; // 최대 정수부 자릿수
numericTextBox.FractionPartLength = 3; // 최대 실수부 자릿수
728x90
▶ TextBox 클래스 : 숫자 텍스트 박스 만들기
using System;
using System.Globalization;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 숫자 텍스트 박스
/// </summary>
public class NumericTextBox : TextBox
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 최대 정수부 길이
/// </summary>
private int maximumIntegerPartLength = 10;
/// <summary>
/// 최대 소수부 길이
/// </summary>
private int maximumFractionPartLength = 2;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 최대 정수부 길이 - MaximumIntegerPartLength
/// <summary>
/// 최대 정수부 길이
/// </summary>
public int MaximumIntegerPartLength
{
get
{
return this.maximumIntegerPartLength;
}
set
{
this.maximumIntegerPartLength = value;
}
}
#endregion
#region 최대 소수부 길이 - MaximumFractionPartLength
/// <summary>
/// 최대 소수부 길이
/// </summary>
public int MaximumFractionPartLength
{
get
{
return this.maximumFractionPartLength;
}
set
{
this.maximumFractionPartLength = value;
}
}
#endregion
#region 정수 값 - IntegerValue
/// <summary>
/// 정수 값
/// </summary>
public int IntegerValue
{
get
{
return Int32.Parse(Text);
}
}
#endregion
#region 십진수 값 - DecimalValue
/// <summary>
/// 십진수 값
/// </summary>
public decimal DecimalValue
{
get
{
return Decimal.Parse(Text);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - NumericTextBox()
/// <summary>
/// 생성자
/// </summary>
public NumericTextBox()
{
TextAlign = HorizontalAlignment.Right;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 키 PRESS 처리하기 - OnKeyPress(e)
/// <summary>
/// 키 PRESS 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
string inputKey = e.KeyChar.ToString();
string previousText = Text;
NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
string numberDecimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string numberGroupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;
int numberDecimalSeparatorIndex = previousText.IndexOf(numberDecimalSeparator);
int currentIndex = SelectionStart;
if(Char.IsDigit(e.KeyChar))
{
// 소수점이 있는 경우
if(numberDecimalSeparatorIndex > -1)
{
// 현재 인덱스가 소수점 인덱스보다 큰 경우
if(currentIndex > numberDecimalSeparatorIndex)
{
// 소수부를 구한다.
string fractionPart = previousText.Substring(numberDecimalSeparatorIndex);
// 소수부 길이가 최대 소수부 길이보다 큰 경우
if(fractionPart.Length > this.maximumFractionPartLength)
{
// 입력을 취소한다.
e.Handled = true;
}
}
else
{
// 정수부를 구한다.
string integerPart = previousText.Substring(0, numberDecimalSeparatorIndex);
// 마이너스 기호 인덱스를 구한다.
int negativeSignIndex = integerPart.IndexOf(negativeSign);
// 마이너스 기호가 있는 경우
if(negativeSignIndex > -1)
{
// 정수부 길이가 최대 정수부 길이보다 큰 경우
if(integerPart.Length > this.maximumIntegerPartLength)
{
// 입력을 취소한다.
e.Handled = true;
}
}
else // 마이너스 기호가 없는 경우
{
// 정수부 길이가 (최대 정수부 길이 - 1)보다 큰 경우
if(integerPart.Length > (this.maximumIntegerPartLength - 1))
{
// 입력을 취소한다.
e.Handled = true;
}
}
}
}
else // 소수점이 없는 경우
{
// 마이너스 기호 인덱스를 구한다.
int negativeSignIndex = previousText.IndexOf(negativeSign);
// 마이너스 기호가 있는 경우
if(negativeSignIndex > -1)
{
// 이전 텍스트 길이가 최대 정수부 길이보다 큰 경우
if(previousText.Length > this.maximumIntegerPartLength)
{
// 입력을 취소한다.
e.Handled = true;
}
}
else
{
// 이전 텍스트 길이가 (최대 정수부 길이 - 1)보다 큰 경우
if(previousText.Length > (this.maximumIntegerPartLength - 1))
{
// 입력을 취소한다.
e.Handled = true;
}
}
}
}
else if(inputKey.Equals(numberDecimalSeparator)) // 소수점을 입력한 경우
{
// 소수점이 있는 경우
if(previousText.IndexOf(numberDecimalSeparator) > -1)
{
// 입력을 취소한다.
e.Handled = true;
}
}
else if(inputKey.Equals(numberGroupSeparator)) // 콤마를 입력한 경우
{
// 입력을 취소한다.
e.Handled = true;
}
else if(inputKey.Equals(negativeSign)) // 마이너스 기호를 입력한 경우
{
// 마이너스 기호 인덱스를 구한다.
int negativeSignIndex = previousText.IndexOf(negativeSign);
// 마이너스 기호가 있는 경우
if(negativeSignIndex > -1)
{
// 입력을 취소한다.
e.Handled = true;
}
// 현재 위치가 맨 앞이 아닌 경우
if(currentIndex != 0)
{
// 입력을 취소한다.
e.Handled = true;
}
}
else
{
// 입력을 취소한다.
e.Handled = true;
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 모서리가 둥근 윈도우 폼 만들기 (0) | 2015.11.15 |
---|---|
[C#/WINFORM] DataGridView 클래스 : 수직 스크롤바 표시 여부 구하기 (0) | 2015.11.11 |
[C#/WINFORM] DataGridView 클래스 : 수직 스크롤바 표시 여부 구하기 (0) | 2015.11.11 |
[C#/WINFORM] CheckedListBox 클래스 : 마우스 드래그 항목 체크하기 (0) | 2015.11.05 |
[C#/WINFORM] CheckedListBox 클래스 : 데이터 바인딩 하기 (0) | 2015.11.05 |
[C#/WINFORM] TextBox 클래스 : 워터마크 문자열 표시하기 (0) | 2015.09.02 |
[C#/WINFORM] 문자열 너비 구하기 (0) | 2015.09.02 |
[C#/WINFORM] 히스토그램 비트맵 구하기 (0) | 2015.06.19 |
[C#/WINFORM] 비트맵 설정하기 (0) | 2015.06.19 |
[C#/WINFORM] 비트맵 배열 구하기 (0) | 2015.06.19 |
댓글을 달아 주세요