【问题标题】:Custom Numericupdown自定义 Numericupdown
【发布时间】:2015-06-16 03:25:36
【问题描述】:

我正在尝试创建一个自定义 numericUpDown 控件。我正在尝试将文本添加到 numericUpDown 并且效果很好。但是,现在我想找到一种方法,在控件中单击时只选择数字,因为此时当我手动“编辑”值时,我可以擦除我添加到控件中的单位。我在谷歌上的一个软件中找到了这个控件,它就像一个“范围”的 numericUpDown。它就像我需要的那样工作,可以单击并仅选择数字。当你点击文本时,它应该选择要编辑的数字而不是文本:

这就是我现在拥有的:

public class RangedNumericUpDown : NumericUpDown
    {
        public int ValueMin = 0;
        public int ValueMax = 10;

        public RangedNumericUpDown()
        {

        }

        protected override void UpdateEditText()
        {
            if(this.Text != this.ValueMin + " to " + this.ValueMax)
                this.Text = this.ValueMin + " to " + this.ValueMax;
        }
    }

我找不到像这样的 Windows 窗体的任何自定义控件,我想可以创建类似图像的东西。

【问题讨论】:

    标签: c# winforms custom-controls


    【解决方案1】:

    好吧,你没有足够的代码。不太确定我会推荐这个,将“焦点”从一个值移动到另一个值并不是很直观。只需使用两个 NUD,您就会获得更好的 UI。

    Anyhoo,一些代码可以玩:

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Globalization;
    
    class RangeUpDown : NumericUpDown {
        public decimal LowValue { get; set; }
        public decimal HighValue { get; set; }
    
        private bool lowFocused = true;
        private const string separator = " to ";
    
        protected override void UpdateEditText() {
            if (base.UserEdit) ParseText();
            var lotext = FormatValue(LowValue);
            var hitext = FormatValue(HighValue);
            this.Text = lotext + separator + hitext;
            if (lowFocused) {
                EditBox.SelectionStart = 0;
                EditBox.SelectionLength = lotext.Length;
            }
            else {
                EditBox.SelectionStart = lotext.Length + separator.Length;
                EditBox.SelectionLength = hitext.Length;
            }
            EditBox.Focus();
        }
    
        public override void UpButton() {
            if (base.UserEdit) ParseText();
            if (IsLowValueFocused()) {
                LowValue = Math.Min(this.Maximum, LowValue + this.Increment);
                if (HighValue < LowValue) HighValue = LowValue;
            }
            else {
                HighValue = Math.Min(this.Maximum, HighValue + this.Increment);
            }
            this.OnValueChanged(EventArgs.Empty);
            UpdateEditText();
        }
    
        public override void DownButton() {
            if (base.UserEdit) ParseText();
            if (IsLowValueFocused()) {
                LowValue = Math.Max(this.Minimum, LowValue - this.Increment);
            }
            else {
                HighValue = Math.Max(this.Minimum, HighValue - this.Increment);
                if (LowValue > HighValue) LowValue = HighValue;
            }
            this.OnValueChanged(EventArgs.Empty);
            UpdateEditText();
        }
    
        private bool IsLowValueFocused() {
            lowFocused = EditBox.Text.Substring(EditBox.SelectionStart).Contains(separator.Trim());
            return lowFocused;
        }
    
        private string FormatValue(decimal value) {
            var fmt = (this.ThousandsSeparator ? "N" : "F");
            var frac = this.DecimalPlaces.ToString(CultureInfo.CurrentCulture);
            return value.ToString(fmt + frac, CultureInfo.CurrentCulture);
        }
    
        private TextBox EditBox { get { return (TextBox)Controls[1]; } }
    
        private void ParseText() {
            // Not implemented
            base.UserEdit = false;
        }
    }
    

    【讨论】:

    • 谢谢你,我在做这样的事情,肯定也会帮助别人,再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-10-06
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多