728x90
반응형
728x170
▶ TextFilter.cs
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 텍스트 박스 필터
/// </summary>
public static class TextBoxFilter
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 정수 패턴
/// </summary>
public static string IntegerPattern = "(?<Number>[0-9])";
/// <summary>
/// 십진수 패턴
/// </summary>
public static string DecimalPattern = @"(?<Number>^[0-9]*\.?[0-9]*)";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 숫자 구하기 - GetNumber(regexPattern, sourceText, isPositiveOnly)
/// <summary>
/// 숫자 구하기
/// </summary>
/// <param name="regexPattern">정규식 패턴</param>
/// <param name="sourceText">소스 텍스트</param>
/// <param name="isPositiveOnly">양수만 여부</param>
/// <returns>숫자 문자열</returns>
public static string GetNumber(string regexPattern, string sourceText, bool isPositiveOnly = false)
{
string newNumber = string.Empty;
if(!isPositiveOnly)
{
if(sourceText.StartsWith("-"))
{
newNumber += "-";
}
}
sourceText = sourceText.Replace("-", string.Empty);
Regex regex = new Regex(regexPattern);
Match match = regex.Match(sourceText);
while(match.Success)
{
newNumber += match.Groups["Number"].Value;
match = match.NextMatch();
}
return newNumber;
}
#endregion
#region 텍스트 설정하기 - SetText(textBox, filter)
/// <summary>
/// 텍스트 설정하기
/// </summary>
/// <param name="textBox">텍스트 박스</param>
/// <param name="filter">필터</param>
public static void SetText(TextBox textBox, string filter)
{
int currentPosition = textBox.SelectionStart;
string currentText = textBox.Text;
if(filter.Length < currentText.Length)
{
currentPosition--;
currentText = currentText.Remove(currentPosition, 1);
textBox.Text = currentText;
}
else
{
textBox.Text = filter;
}
if(currentPosition >= textBox.Text.Length)
{
textBox.SelectionStart = textBox.Text.Length;
}
else
{
textBox.SelectionStart = currentPosition;
}
}
#endregion
}
}
728x90
▶ MainForm.cs
using System;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.integerTextBox.TextChanged += integerTextBox_TextChanged;
this.decimalTextBox.TextChanged += decimalTextBox_TextChanged;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 정수 텍스트 박스 텍스트 변경시 처리하기 - integerTextBox_TextChanged(sender, e)
/// <summary>
/// 정수 텍스트 박스 텍스트 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void integerTextBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox.Text != string.Empty)
{
string number = TextBoxFilter.GetNumber(TextBoxFilter.IntegerPattern, textBox.Text, false);
TextBoxFilter.SetText(textBox, number);
}
}
#endregion
#region 십진수 텍스트 박스 텍스트 변경시 처리하기 - decimalTextBox_TextChanged(sender, e)
/// <summary>
/// 십진수 텍스트 박스 텍스트 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void decimalTextBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox.Text != string.Empty)
{
string number = TextBoxFilter.GetNumber(TextBoxFilter.DecimalPattern, textBox.Text, false);
TextBoxFilter.SetText(textBox, number);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] USB 제어하기 (0) | 2018.03.03 |
---|---|
[C#/WINFORM] 폼 위치 설정하기 (0) | 2018.03.03 |
[C#/WINFORM] 태스크바 위치 타입 구하기 (0) | 2018.03.03 |
[C#/WINFORM] ClickOnce 설치 파일 캐시 지우기 (0) | 2018.03.01 |
[C#/WINFORM] WIN32 API를 사용해 화면 캡처하기 (0) | 2018.02.18 |
[C#/WINFORM] 카메라 컨트롤 사용하기 (0) | 2018.02.18 |
[C#/WINFORM] DataGridView 클래스 : 달력 컨트롤 컬럼 사용하기 (0) | 2018.02.18 |
[C#/WINFORM] DataGridView 클래스 : 동적 콤보 박스 사용하기 (0) | 2018.02.18 |
[C#/WINFORM] TreeView 클래스 : 동적 노드 생성하기 (0) | 2018.02.18 |
[C#/WINFORM] WPF 사용자 컨트롤 사용하기 (0) | 2018.02.17 |
댓글을 달아 주세요