블로그 이미지
worhkd2

calendar

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
2012. 2. 8. 02:15 프로그래밍/C#

많이 사용될 예제임.

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;

}

posted by worhkd2