【问题标题】:Only show prime numbers in a NumericUpDown control [duplicate]仅在 NumericUpDown 控件中显示素数 [重复]
【发布时间】:2017-11-13 21:31:09
【问题描述】:
    private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
    {
        //my numeric up down var name is numrsa1//
    }

    static bool IsPrimeNumber(int value)
    {
        bool result = true;

        for (int i = 2; i < value; i++)
        {
            if (value % i == 0)
            {
                result = false;
                break;
            }
        }

        if (value <= 1)
            result = false;

        return result;
    }`

我想知道是否有办法在 Numeric Up Down 中仅显示素数。我正在尝试为大学做密码学工作,我只需要为用户提供素数选项。

【问题讨论】:

  • 您好,欢迎您。请在此处阅读如何提问。您应该说明您做了什么以及缺少什么,以便其他人可以帮助您。 stackoverflow.com/help/how-to-ask
  • Ashkan 我什么都没做,因为我不知道我需要使用什么类型的逻辑。
  • up down 你的意思是降序吗?
  • 你可以将前一个值存储在tag属性中,然后检查它是更低还是更高,然后计算下一个素数并将数字的值设置为up down
  • 改用 DomainUpDown 控件。它类似于 NumericUpDown,但您可以使用自己的集合进行填充,在您的情况下,这只是质数。

标签: c# winforms numbers primes


【解决方案1】:

因为您要求的功能与NumericUpDown 控件相似,但行为不同,所以我建议您创建一个新控件PrimeNumberUpDown扩展 NumericUpDown 控件。

这是我一起浏览的快速版本。我测试了它是否适用于使用鼠标或箭头键向上/向下,并在文本框中输入一个数字。如果它涉及任何关键问题,您可能需要对其进行更详尽的测试。

将此代码粘贴到一个新文件中(相应地更新命名空间),编译,然后您应该会看到一个 PrimeNumberUpDown 控件出现在您的工具箱中,因此您可以像使用任何其他 Windows 窗体控件一样使用它。

using System;
using System.Globalization;
using System.Windows.Forms;

namespace YourNameSpace
{
    public class PrimeNumberUpDown : NumericUpDown
    {
        private int _value;

        public PrimeNumberUpDown()
        {
            // Make sure default value is prime
            if (!IsPrime((int)Value))
                SetNextPrimeValue();
            _value = (int)Value;
        }

        public override void DownButton()
        {
            SetNextPrimeValue();
        }

        public override void UpButton()
        {
            SetPreviousPrimeValue();
        }

        private void SetNextPrimeValue()
        {
            int newValue = (int)Value;
            while (newValue <= Maximum)
            {
                if (IsPrime(++newValue))
                {
                    if (newValue <= Maximum)
                    {
                        Value = newValue;
                        _value = newValue;
                    }
                    return;
                }
            }
        }

        private void SetPreviousPrimeValue()
        {
            int newValue = (int)Value;
            while (newValue >= Minimum)
            {
                if (IsPrime(--newValue))
                {
                    if (newValue >= Minimum)
                    {
                        Value = newValue;
                        _value = newValue;
                    }
                    return;
                }
            }
        }

        protected override void ValidateEditText()
        {
            if (_value == 0)
            {
                base.ValidateEditText();
                return;
            }

            int newValue;
            if (int.TryParse(Text, out newValue) && IsPrime(newValue) && newValue >= Minimum && newValue <= Maximum)
            {
                _value = newValue;
                base.ValidateEditText();
            }
            else
            {
                ChangingText = true;
                Text = _value.ToString(CultureInfo.InvariantCulture);
            }
        }

        private static bool IsPrime(int number)
        {
            if (number == 1) return false;
            if (number == 2) return true;
            if (number % 2 == 0) return false;

            int boundary = (int)Math.Floor(Math.Sqrt(number));

            for (int i = 3; i <= boundary; i += 2)
            {
                if (number % i == 0) return false;
            }

            return true;
        }
    }
}

【讨论】:

    【解决方案2】:

    给你:)它只会显示素数

    private void numericUpDown2_ValueChanged(object sender, EventArgs e)
    {
        //suposing you have this code
        int numrsa1 = (int)this.numericUpDown2.Value;
    
        //or add the rest 8 lines to your code
        if (IsPrimeNumber(numrsa1))
        {
            numericUpDown2.Value = numrsa1;
        }
        else
        {
            numericUpDown2.UpButton();
        }
    }
    

    //the rest is your code without changes, I am using you function declaration IsPrimeNumber() 以验证数字并在那里打印结果。它将暂时上升,并通过按升序增加控件的值仅显示素数。

    这是一个非常简单的解决方案,我可以快速测试以尽快发布 :)

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多