많이 사용될 예제임.
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
Int32 selectionStart = textBox.SelectionStart;
Int32 selectionLength = textBox.SelectionLength;
String newText = String.Empty;
foreach (Char c in textBox.Text.ToCharArray())
{
if (Char.IsDigit(c) || Char.IsControl(c)) newText += c;
}
textBox.Text = newText;
textBox.SelectionStart = selectionStart <=
textBox.Text.Length ? selectionStart : textBox.Text.Length;
}
추가적으로
// 알파벳이면1 아니면0
int IsAlpha(char ch);
// 숫자면1 아니면0
int IsNumeric(char ch);
// 구분자이면1 아니면0
int IsDelimiter(char ch);
// 구두점이면1 아니면0
int IsPunctuation(char ch);
// 연산자이면1 아니면0
int IsOperator(char ch);
// 여기에필요한구분자들을모두집어넣는다
int IsDelimiter(char ch)
{
char delimiters[] = " \t\n";
int i;
for ( i = 0; delimiters[i] != '\0'; i++ )
{
if ( ch == delimiters[i] )
return 1;
}
return 0;
}
// 여기에는필요한구두점을모두집어넣는다
int IsPunctuation(char ch)
{
char punctuations[] = ":#()=";
int i;
for ( i = 0; punctuations[i] != '\0'; i++ )
{
if ( ch == punctuations[i] )
return 1;
}
return 0;
}
int IsOperator(char ch)
{
// 연산자들을모두집어넣음
char operators[] = "+-*/^~";
int i;
for ( i = 0; operators[i] != '\0'; i++ )
{
if ( ch == operators[i] )
return 1;
}
return 0;
}
'프로그래밍 > C#' 카테고리의 다른 글
C# 문자열 자르기(string cut) (0) | 2012.02.29 |
---|---|
c# 메일전송 (0) | 2012.02.27 |
C# WindowForm TextBox(텍스트박스) 숫자만 입력과 단위(콤마)찍어주기 (0) | 2012.02.11 |
C# date형을 string형으로 형 변환 및 time 크기 비교 (0) | 2012.02.08 |
C# 시스템의 날짜 (0) | 2012.02.08 |