2017-12-08 9 views
0

내 응용 프로그램에 numericupdown 컨트롤을 사용하고 싶습니다. 나는 대신에 일반 텍스트 상자를 사용할 수 있다는 것을 잘 알고 있지만,이 특정 컨트롤의 UI가 내 응용 프로그램에서하는 것과 맞먹는 것을 좋아합니다.Padleft on numericupdown

또한 원하는 텍스트 출력마다 왼쪽에 0이 있어야합니다. 내가 잘못 본 것이 아니라면 표준 숫자 축소판 컨트롤에서 지원되지 않습니다. 길이가 4 자리를 초과해서는 안됩니다. 그러나, 더 많은 것을 입력하면, 새로운 키 스트로크 (keystrokes)와 가장 왼쪽 자리 (left-most digits)를 대신 나타내야합니다. 위쪽 및 아래쪽 화살표는 일반적인 동작 당 값을 증가/감소시킵니다. 값을 입력 한 후에도.

부정적인 실행을 허용해서는 안됩니다. 전체 정수만 수용해야합니다. 이것은 재고 기능에 의해 쉽게 처리됩니다.

+0

현재 형식의 질문은 [codereview.stackexchange.com] (https://codereview.stackexchange.com)에 더 적합합니다. stackoverflow에 보관하려면 게시물을 편집하고 질문을 한 다음 자신의 답변을 게시하십시오. 그러면 독자가 더 유용 할 것입니다. –

+0

codereview에 대해 알지 못했습니다. 그것을 언급 주셔서 감사합니다. 나는 그걸 할거야. –

답변

0

따라야 할 다른 사람들의 답변을 부분적으로 게시하십시오. 부분적으로 내가 바보가 아니란 확신을 찾고 있습니다.

이 해킹은 최종 값을 추출하기 전에 Sync()를 실행하는 것에 따라 달라집니다. 타이머는 꽤 빠르게 작동하지만 정확한 순서로 일이 진행되는 것은 아닙니다. 값을 추출하기 직전에 Sync()를 수동으로 트리거하면 아프지 않을 수도 있습니다.

public class UpDownWith0 : System.Windows.Forms.NumericUpDown 
{ 

    private System.Windows.Forms.Timer addzeros = new System.Windows.Forms.Timer(); 

    public UpDownWith0() 
    { 
    this.addzeros.Interval = 500; //Set delay to allow multiple keystrokes before we start doing things 
    this.addzeros.Stop(); 
    this.addzeros.Tick += new System.EventHandler(this.Sync); 
    } 

    protected override void OnTextBoxTextChanged(object source, System.EventArgs e) 
    { 
    this.addzeros.Stop(); //reset the elapsed time every time the event fires, handles multiple quick proximity changes as if they were one 
    this.addzeros.Start(); 
    } 

    public void Sync(object sender, System.EventArgs e) 
    { 
    int val; 
    this.addzeros.Stop(); 
    if (this.Text.Length > 4) 
    { 
     //I never want to allow input over 4 digits in length. Chop off leftmost values accordingly 
     this.Text = this.Text.Remove(0, this.Text.Length - 4); 
    } 
    int.TryParse(this.Text, out val); //could use Value = int.Parse() here if you preferred to catch the exceptions. I don't. 
    if (val > this.Maximum) { val = (int)this.Maximum; } 
    else if (val < this.Minimum) { val = (int)this.Minimum; } 
    this.Value = val; //Now we can update the value so that up/down buttons work right if we go back to using those instead of keying in input 

    this.Text = val.ToString().PadLeft(4, '0'); //IE: display will show 0014 instead of 14 
    this.Select(4, 0); //put cursor at end of string, otherwise it moves to the front. Typing more values after the timer fires causes them to insert at the wrong place 
    } 
}