Windows窗体-将NumericUpDown定制为可为空 [英] Windows Forms - customizing NumericUpDown to be nullable

查看:372
本文介绍了Windows窗体-将NumericUpDown定制为可为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个自定义类,它允许NumericUpDown接收可为空的值.我以一种编辑形式使用它,该形式可以为空,也可以填充来自需要可空值的数据库中的数据.

I found this custom class that allows NumericUpDown to receive nullable values. I use it in an edit form which can be empty or could be populated with a data from a database from where the need for nullable values comes from.

这是当前代码:

public partial class NullableNumericUpDown : NumericUpDown
    {
        public NullableNumericUpDown()
        {
            InitializeComponent();
        }

        public NullableNumericUpDown(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }



        private int? _value;

        [Bindable(true)]
        public new int? Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                if (value != null)
                {
                    base.Value = (int)value;
                    Text = Value.ToString();
                }
                else
                {
                    Text = "";
                }
            }
        }

        private void NullableNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            _value = (int)base.Value;
        }



        void NullableNumericUpDown_TextChanged(object sender, System.EventArgs e)
        {
            if (Text == "")
            {
                _value = null;
            }
        }
    }

我对C#还是很陌生,所以我不能说我完全理解代码,但是我遇到的问题是,当我用数据库中的数据填充Form时,以及NullableNumericUpDown确实具有某些值时,假设-3.当我将此值更改为5时,从表单中的字段收集数据的最终结果是53.另外,如果我删除3,然后按递增箭头,则会得到4.似乎原始数据在此控件的整个生命周期中都已保存,我尝试在某些当前位置设置0,我认为这可能会有所帮助,但这样做并没有,除了我需要摆脱掉这一事实之外初始值的变化,如果控件的值实际上被更改,仅使其成为0是不够的,因为如果我有空控件,则必须可以将其记录为null而不是0.

I'm fairly new to C# so I can't say I fully understand the code but however the problem that I have is when I populate the Form with data from the database and exactly when the NullableNumericUpDown has some value, let's say - 3. When I change this value to say 5, the final result when I collect the data from the filed in the form is 53. Also if I delete 3 and then hit the incrementing arrow I get 4. It seems that that the initial data is saved throughout the life cycle of the this control, I tried to set 0 at some current places I thought it might help, but it wouldn't and besides the fact that I need to get rid of this initial value if the value of the control is changed in fact it's not enough to just make it 0 cause if I have empty control this must be ok and record it as null instead 0.

仅此而已,这就是我为NullableNumericUpDown控件设置数据的方式:

Just to be complete here is how I set the data for the NullableNumericUpDown control :

numUpDnAreas.Value = entity.AreasCnt;

这发生在我的form_load事件上.当我单击Save按钮时,我将使用以下命令收集数据:

this happens on my form_load event. And when I click Save button I collect the data with this :

entity.AreasCnt = numUpDnAreas.Value;

可以重构此代码以满足我的需要吗?还是应该保留它,而只使用MaskedTextBox之类的东西?

Can this code be refactored to match my needs or should I leave it and just use something like MaskedTextBox or other?

推荐答案

您是否可以尝试使用Extended WPF工具包中的UpDown控件. http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown&referringTitle=Home 它可以解决您的问题,并且是开源的.

Could you try to use UpDown controls from Extended WPF toolkit. http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown&referringTitle=Home It may solve your problem and is opensource.

我们在项目中有类似的要求,最后我们通过创建一个包装文本框并设置Binding的自定义控件来编写自己的控件. AllowNullValue转换为string.empty

We've had the similiar requirements in our project and we ended up writing our own by creating a custom control wrapping a textbox and setting Binding. AllowNullValue to string.empty

这篇关于Windows窗体-将NumericUpDown定制为可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆