2013-02-25 68 views
5

Làm cách nào để kiểm tra xem người dùng có để trống điều khiển NumericUpDown không, hãy xóa giá trị trên đó? Vì vậy, tôi có thể gán cho nó một giá trị 0.Kiểm tra xem NumericUpDown có trống không

+0

kiểm tra độ dài của biến - http://www.dotnetperls.com/string-length –

Trả lời

6
if(NumericUpDown1.Text == "") 
{ 
    // If the value in the numeric updown is an empty string, replace with 0. 
    NumericUpDown1.Text = "0"; 
} 
0
decimal d = 0 
if(decimal.TryParse(NumericUpDown1.Text, out d) 
{ 

} 
NumericUpDown1.Value = d; 
4

Nó có thể là hữu ích để sử dụng các sự kiện xác nhận và yêu cầu cho các văn bản tài sản

private void myNumericUpDown_Validated(object sender, EventArgs e) 
{ 
    if (myNumericUpDown.Text == "") 
    { 
     myNumericUpDown.Text = "0"; 
    } 
} 
0

Hãy thử điều này

if (string.IsNullOrEmpty(((Control)this.nud1).Text)) 
{ 
    //null 
} 
else 
{ 
    //have value 
} 
0

Nếu bạn muốn ant để cấm giá trị rỗng cho NumericUpDown, chỉ cần sử dụng lớp này. Hiệu ứng của nó là khi người dùng cố gắng xóa giá trị điều khiển bằng chọn tất cả + phím xóa lùi, giá trị số thực tế được đặt lại. Đây không thực sự là một phiền toái vì người dùng vẫn có thể chọn select-all + gõ một chữ số để bắt đầu chỉnh sửa giá trị số mới.

sealed class NumericUpDownEmptyValueForbidder { 
    internal NumericUpDownEmptyValueForbidder(NumericUpDown numericUpDown) { 
     Debug.Assert(numericUpDown != null); 
     m_NumericUpDown = numericUpDown; 
     m_NumericUpDown.MouseUp += delegate { Update(); }; 
     m_NumericUpDown.KeyUp += delegate { Update(); }; 
     m_NumericUpDown.ValueChanged += delegate { Update(); }; 
     m_NumericUpDown.Enter += delegate { Update(); }; 
    } 
    readonly NumericUpDown m_NumericUpDown; 
    string m_LastKnownValueText; 

    internal void Update() { 
     var text = m_NumericUpDown.Text; 
     if (text.Length == 0) { 
      if (!string.IsNullOrEmpty(m_LastKnownValueText)) { 
       m_NumericUpDown.Text = m_LastKnownValueText; 
      } 
      return; 
     } 
     Debug.Assert(text.Length > 0); 
     m_LastKnownValueText = text; 
    } 
    } 
Các vấn đề liên quan