2011-03-08 1 views
3

thousandsseperator가 true로 설정된 numbericupdown 개체를 사용할 때 포커스를 잃을 때 쉼표를 올바르게 표시하도록 텍스트 만 업데이트됩니다. 값이 변경 될 때마다 강제로 새로 고치는 방법이 있습니까?Numericupdown은 각 키 누르기 텍스트를 업데이트합니다.

+0

나에게 좋은 생각이 같은이 소리 ..., 사용자가 자리를 교체/단지 입력/삭제하기 전에 천 단위 구분 기호를 제거하고자하는 가정 당신은에 가고있다 그것을 그의 얼굴에 다시 넣어 라. – bart

+0

내가 처리하고 싶은 것은 값이 100 인 경우와 같습니다. 마지막에 0을 추가하여 1000으로 설정하면 0이 입력 될 때 1,000을 올바르게 표시해야합니다. 쉼표가 올바르게 채워지는데 초점이 없어 질 때까지 기다리지 않기를 바란다. – JonF

답변

1

이벤트를 수행해야합니다. 우리가 알다시피 thounsandseperator는 우리가 입력 할 때 단순히 호출 할 수있는 초점에 의해 트리거됩니다.

private void numericUpDown1_KeyUp(object sender, KeyEventArgs e) 
     { 
      numericUpDown1.Focus(); 
      //Edit: 
      numericUpDown1.Select(desiredPosition,0) 
     } 

그래서 사용자 유형으로, 우리는 상자에게 thousandsseperator 서식을 기억하는 해킹입니다 그것에 초점 등을 제공합니다.

참고 : 해킹 문제는 해킹이 더 많이 발생하는 이상한 상황입니다. 예 : 커서가 텍스트의 맨 앞으로 돌아옵니다. 문제를 해결하려면 다른 해킹이 필요합니다.

다른 사건을 실험하여 사례에 맞는 것을 찾으십시오.

편집 : 당신은 ...이와 함께 더욱 가고 싶은 경우, BTW

  1. 커서 추적합니다.
  2. keyup이 호출 될 때 커서를 올바른 위치로 되돌립니다. Setting the cursor position in numericUpDown control
0

는 보호하지만 NumericUpDown을 상속하는 클래스에서 액세스 할 수 있습니다 ParseEditText()를 호출해야 할 것입니다 컨트롤의 텍스트 값을 포맷합니다. 문제는 통화 후 커서가 첫 번째 문자 앞에 이동한다는 것입니다. 커서의 위치를 ​​제어하려면 NumericUpDown이 노출하지 않는 SelectionStart 속성에 대한 액세스가 필요합니다. NumericUpDown에는 여전히 UpDownEdit 유형의 upDownEdit라는 필드가 있습니다. UpDownEdit 클래스는 TextBox에서 상속 받고 하나처럼 작동합니다. 그래서 솔루션은 NumericUpDown에서 상속 받고 reflection을 사용하여 upDownEdit.SelectionStart의 값을 가져 오거나 설정하는 것입니다. 여기에 작업 할 수있는 일이다

public class NumericUpDownExt : NumericUpDown 
{ 
    private static FieldInfo upDownEditField; 
    private static PropertyInfo selectionStartProperty; 
    private static PropertyInfo selectionLengthProperty; 

    static NumericUpDownExt() 
    { 
     upDownEditField = (typeof(UpDownBase)).GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic); 
     Type upDownEditType = upDownEditField.FieldType; 
     selectionStartProperty = upDownEditType.GetProperty("SelectionStart"); 
     selectionLengthProperty = upDownEditType.GetProperty("SelectionLength"); 
    } 

    public NumericUpDownExt() : base() 
    { 
    } 

    public int SelectionStart 
    { 
     get 
     { 
      return Convert.ToInt32(selectionStartProperty.GetValue(upDownEditField.GetValue(this), null)); 
     } 
     set 
     { 
      if (value >= 0) 
      { 
       selectionStartProperty.SetValue(upDownEditField.GetValue(this), value, null); 
      } 
     } 
    } 

    public int SelectionLength 
    { 
     get 
     { 
      return Convert.ToInt32(selectionLengthProperty.GetValue(upDownEditField.GetValue(this), null)); 
     } 
     set 
     { 
      selectionLengthProperty.SetValue(upDownEditField.GetValue(this), value, null); 
     } 
    } 

    protected override void OnTextChanged(EventArgs e) 
    { 
     int pos = SelectionStart; 
     string textBefore = this.Text; 
     ParseEditText(); 
     string textAfter = this.Text; 
     pos += textAfter.Length - textBefore.Length; 
     SelectionStart = pos; 
     base.OnTextChanged(e); 
    } 
}