将文本字段的值设置为不低于零

时间:2015-07-09 08:22:05

标签: c# winforms

如何设置文本字段以使其不能低于0?

enter image description here

4 个答案:

答案 0 :(得分:4)

如果您使用的是Windows表单,则只需使用NumericUpDown,它还会公开MinimumMaximum值字段。

如果由于某种原因你需要自己动手,你可能需要将自己附加到更改文本时触发的事件。如果文本字段的内容是数字且小于0,则只需使用0或您希望提供的任何值覆盖文本。

答案 1 :(得分:0)

win form代码: -

private void btnArrowUp_Click(object sender, EventArgs e)
{
   int val = 0;
   if (Convert.ToInt32(textBoxValue.Text) <= 1000) //convert the textBox value to integer and check for your Upper Limit (here i have set it to 1000)
   {
        // if so, increment by your step (i used 1)
        val = Convert.ToInt32(textBoxValue.Text);
        val += 1;
        textBoxValue.Text = val.ToString();
        labelErrorMessage.Visible = false;
   }
   else
   {
        // otherwise, give a message and reset to your Default (i used 0)
        textBoxValue.Text = "0";
        labelErrorMessage.Text = "1000 (Zero) is the Max limit !";
        labelErrorMessage.Visible = true;
   }
 }

 private void btnArrowDown_Click(object sender, EventArgs e)
 {
    int val = 0;
    if (Convert.ToInt32(textBoxValue.Text) > 0) //convert the textBox value to integer and check for your Lower Limit (here i have set it to 0)
    {
        // if so, decrement by your step (i used 1)
        val = Convert.ToInt32(textBoxValue.Text);
        val -= 1;
        textBoxValue.Text = val.ToString();
        labelErrorMessage.Visible = false;
    }
    else
    {
        // otherwise, give a message and reset to your Default (i used 0)
        textBoxValue.Text = "0";
        labelErrorMessage.Text = "0 (Zero) is the lowest limit !";
        labelErrorMessage.Visible = true;
    }
 }

答案 2 :(得分:0)

使用NumericUpDown代替文字字段并设置Minimum = 0或按照此

添加Balloon.cs

public enum TooltipIcon
{
    None,
    Info,
    Warning,
    Error
}
public class Balloon
{
    private Control m_parent;
    private string m_text = "FMS Balloon Tooltip Control Display Message";
    private string m_title = "FMS Balloon Tooltip Message";
    private TooltipIcon m_titleIcon = TooltipIcon.None;
    private const int ECM_FIRST = 0x1500;
    private const int EM_SHOWBALLOONTIP = ECM_FIRST + 3;
    [DllImport("User32", SetLastError = true)]
    private static extern int SendMessage(
        IntPtr hWnd,
        int Msg,
        int wParam,
        IntPtr lParam);
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct EDITBALLOONTIP
    {
        public int cbStruct;
        public string pszTitle;
        public string pszText;
        public int ttiIcon;
    }
    public Balloon()
    {
    }
    public Balloon(Control parent)
    {
        m_parent = parent;
    }
    public void Show()
    {
        EDITBALLOONTIP ebt = new EDITBALLOONTIP();
        ebt.cbStruct = Marshal.SizeOf(ebt);
        ebt.pszText = m_text;
        ebt.pszTitle = m_title;
        ebt.ttiIcon = (int)m_titleIcon;
        IntPtr ptrStruct = Marshal.AllocHGlobal(Marshal.SizeOf(ebt));
        Marshal.StructureToPtr(ebt, ptrStruct, false);
        System.Diagnostics.Debug.Assert(m_parent != null, "Parent control is null", "Set parent before calling Show");
        int ret = SendMessage(m_parent.Handle, EM_SHOWBALLOONTIP, 0, ptrStruct);
        Marshal.FreeHGlobal(ptrStruct);
    }
    public void Show(string text, Control parent, string title, TooltipIcon icon)
    {

    }
    public string Title
    {
        get
        {
            return m_title;
        }
        set
        {
            m_title = value;
        }
    }
    public TooltipIcon TitleIcon
    {
        get
        {
            return m_titleIcon;
        }
        set
        {
            m_titleIcon = value;
        }
    }
    public string Text
    {
        get
        {
            return m_text;
        }
        set
        {
            m_text = value;
        }
    }
    public Control Parent
    {
        get
        {
            return m_parent;
        }
        set
        {
            m_parent = value;
        }
    }
}

将以下代码添加到表单

private readonly Balloon _balloonTip = new Balloon();
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = HandledNumerics(sender, e);
    }
    private bool HandledNumerics(object sender, KeyPressEventArgs e)
    {
        var handled = !(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back) || (e.KeyChar == (char)Keys.Delete));
        if (handled)
        {
            ShowBalloon("Warning", "Invalid Input!\nOnly numeric value is acceptable", (TextBox)sender);
        }
        return handled;
    }
    private void ShowBalloon(string title, string text, Control parent, TooltipIcon icon = TooltipIcon.Warning)
    {
        _balloonTip.Title = title;
        _balloonTip.Text = text;
        _balloonTip.Parent = parent;
        _balloonTip.TitleIcon = icon;
        _balloonTip.Show();
    }

得到结果就像

enter image description here

答案 3 :(得分:0)

简单的方法,只需创建一个函数来验证值是否低于0并在文本更改时调用它,或者在down_click函数中如果文本字段被锁定到键盘输入那么。

private void bellowZero()
{
     if (Convert.ToInt32(textBox1.Text) < 0)
    {
        textBox1.Text = "0";
    }
}