주로 숫자 값의 데이터 캡처를 수행하는 응용 프로그램이 있습니다. 일반적으로 텍스트 상자를 사용하고 있습니다. 이 텍스트를 정리하기 위해 입력 한 숫자를 그룹화하고 그룹 사이에 공백을 넣기로했습니다.TextChanged 함수를 사용할 여러 TextBox의 클래스에 넣기
'555555555'와 같이 '555 555 555'처럼 표시되어 whatsapp 또는 viber signup 텍스트 상자 효과와 비슷한 것을 제공합니다. 이 작업을 수행하려면 TextChanged 이벤트를 사용하고 있습니다.
여기까지 내가 지금까지 해본 방법의 예입니다. 이것은 전화 번호를 입력 할 수있는 텍스트 상자입니다.
private void cell_TextChanged(object sender, TextChangedEventArgs e)
{
string phrase = cell.Text;
if (phrase != null)
{
// This is to reset all spaces in the text back to none before the code below puts new ones.
// This avoids puting a space directly next to another one previously set.
// For some reason the textbox becomes difficult to work with if I don't do this.
phrase = Regex.Replace(phrase, @"\s+", "");
}
// If the text is smaller than 4 characters;
if (phrase.Length <= 4)
{
// Do nothing to it
cell.Text = phrase;
}
// if the text is equal to five characters
if (phrase.Length == 5)
{
// group the first 3 characters
string first = phrase.Substring(0, 3);
// and the last 2 to characters
string second = phrase.Substring(3, 2);
// then put a space between them
string paste = (first + " " + second);
//This string goes into the TextBox 'cell'
cell.Text = paste;
}
// if the text is equal to six characters
if (phrase.Length == 6)
{
// group the first 3 characters
string first = phrase.Substring(0, 3);
//And the last 3 characters
string second = phrase.Substring(3, 3);
// then put a space between them
string paste = (first + " " + second);
//This string goes into the TextBox 'cell'
cell.Text = paste;
}
if (phrase.Length == 7)
{
// group the first 4 characters
string first = phrase.Substring(0, 4);
// then the next 3
string second = phrase.Substring(4, 3);
// then put a space between them
string paste = (first + " " + second);
//This string goes into the TextBox 'cell'
cell.Text = paste;
}
if (phrase.Length == 8)
{
// group the first 4 characters
string first = phrase.Substring(0, 4);
// then the next 3
string second = phrase.Substring(4, 3);
// then the next character
string third = phrase.Substring(7, 1);
// then put a space between the first, second and third string
string paste = (first + " " + second + " " + third);
//This string goes into the TextBox 'cell'
cell.Text = paste;
}
if (phrase.Length == 9)
{
// group the first 4 characters
string first = phrase.Substring(0, 4);
// then the next 3
string second = phrase.Substring(4, 3);
// then the next 2 characters
string third = phrase.Substring(7, 2);
// then put a space between the first, second and third string
string paste = (first + " " + second + " " + third);
//This string goes into the TextBox 'cell'
cell.Text = paste;
}
if (phrase.Length == 10)
{
// group the first 4 characters
string first = phrase.Substring(0, 4);
// then the next 3
string second = phrase.Substring(4, 3);
// then the next 3 characters after that
string third = phrase.Substring(7, 3);
// then put a space between the first, second and third string
string paste = (first + " " + second + " " + third);
//This string goes into the TextBox 'cell'
cell.Text = paste;
}
//This is to keep the cursor at the end of the TextBox's text when I enter a new character other wise it goes back to the start.
cell.CaretIndex = cell.Text.Length;
}
문제는 우선이 코드는 내가 (내 강사가 이미 이런 일을 반복했다 나쁜 연습과 I 언급 번호를 구분해야하는 모든 텍스트 상자에 배치 할 여유가 없다 너무 오래입니다 그것을 위해 벌을받을지도 모른다). 그러나 나는 이것을 할 수 있어야하는 많은 세포를 가지고있다. 그러나 나는 그것을 행동으로 바꾸거나 그것을 클래스에 넣고 여전히 기능하게하는 기술이 부족하다.
그래서 기본적으로 내가 알고 싶은 것은 여러 페이지에 걸쳐 사용되는 get, set 클래스 또는 behavior 클래스 (또는 그 문제에 대해 간과 할 수있는 anyother)에서이를 구현하는 방법입니다. 누구 제안?
코드를 별도의 메서드로 추출하고 해당 메서드를 처리기에서 호출하십시오. –
요구 사항이 맞으면 Mvvm Approach [StackReference] (http://stackoverflow.com/questions/20089739/wpf-mvvm-textbox-text-binding-vs-changedtext-event)에서이 작업을 수행하면 성능이 현저하게 향상됩니다 너무. – Eldho
그러면 도움이 될 것입니다 : 'for (int i = 3; i
franssu