简体   繁体   English

c# - 如何在已被覆盖的自定义控件上创建事件和更改属性?

[英]c# - how do I create events and change properties on custom controls that have been overridden?

My goal is to create 2 Numeric Up Down controls for keeping score in Tennis.我的目标是创建 2 个 Numeric Up Down 控件来保持网球得分。 So I have to override the Numeric Up Down control class, BUT I'm not sure how to create events or reference the properties of these custom controls from other control's events.所以我必须覆盖 Numeric Up Down 控件类,但我不确定如何创建事件或从其他控件的事件中引用这些自定义控件的属性。

I created the 2 custom controls by this technique ( https://stackoverflow.com/a/5921599/10886205 ), which allowed me to jump from 0, 15, 30 to 40 and place text values in it after it exceeded 40.我通过这种技术( https://stackoverflow.com/a/5921599/10886205 )创建了 2 个自定义控件,它允许我从 0、15、30 跳到 40,并在超过 40 后将文本值放入其中。

However, the control doesn't exist in the Designer Interface of Visual Studio, so (because I'm kind of an “amateur programmer”) I have no idea how to create an event, such as ValueUp, ValueDown, ValueChanged, OR how to change any of the control's properties, such as Value or Text, from any other event except by use of “this” keyword in the override events.但是,Visual Studio 的设计器界面中不存在该控件,因此(因为我是一个“业余程序员”)我不知道如何创建事件,例如 ValueUp、ValueDown、ValueChanged 或如何创建从任何其他事件更改任何控件的属性,例如值或文本,除非在覆盖事件中使用“this”关键字。 However, this doesn't really help me to link the controls programmatically, so that when 1 player is in “Adv”, the other player is in “-“, or when 1 player evens up to “Deuce” then the other player is also in “Deuce.”但是,这并不能真正帮助我以编程方式链接控件,因此当 1 名玩家处于“Adv”时,另一名玩家处于“-”,或者当 1 名玩家达到“Deuce”时,则另一名玩家处于也在“Deuce”中。 (I was trying to use Tags and variables, but I think this is a dead-end.) (我试图使用标签和变量,但我认为这是一个死胡同。)

public partial class Form1 : Form
{
    public class NumericUpDownEx : NumericUpDown
    {
        string tennisAdv = "none";
        string whichNumUpDown = "unknown";

        public void Form1() { }

        public override void UpButton()
        {
            if (Value == 0) Value = 15;
            else if (Value == 15) Value = 30;
            else if (Value == 30) Value = 40;
            else base.UpButton();
        }

        public override void DownButton()
        {
            if (Value == 40) Value = 30;
            else if (Value == 30) Value = 15;
            else if (Value == 15) Value = 0;
            else base.DownButton();
        }

        protected override void UpdateEditText()
        {
            if (Value > 40 & Value % 2 == 0)
                this.Text = "Deuce";
            else if (Value > 40 & Value % 2 != 0)
                this.Text = "Adv";
            else this.Text = this.Value.ToString();
        }
      }

    public Form1()
    {
        //create a custom UpDown for Tennis Points
        //Tennis Away Points
        NumericUpDown mynumTennisAwayScorePoints = new NumericUpDownEx
        {
            Location = new Point(249, 33),
            Size     = new Size(52, 20),
            Minimum  = 0,
            Maximum  = 1000
        };

        //Tennis Home POints
        NumericUpDown mynumTennisHomeScorePoints = new NumericUpDownEx
        {
            Location = new Point(249, 32),
            Size     = new Size(52, 20),
            Minimum  = 0,
            Maximum  = 1000
        };

        //create all other standard componets for the form
        InitializeComponent();

        //place the two custom UpDowns 
        ((System.ComponentModel.ISupportInitialize)(mynumTennisAwayScorePoints)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(mynumTennisHomeScorePoints)).BeginInit();

        panel19.Controls.Add(mynumTennisAwayScorePoints);
        mynumTennisAwayScorePoints.Tag = "away";
        panel20.Controls.Add(mynumTennisHomeScorePoints);
        mynumTennisHomeScorePoints.Tag = "home";
    }
}

At this point, I have 2 custom controls that give values (0, 15, 30, 40, Adv, Deuce, Adv, Deuce, etc.) in that order, but I'm kind of stuck there.在这一点上,我有 2 个自定义控件,它们按顺序给出值(0、15、30、40、Adv、Deuce、Adv、Deuce 等),但我有点卡在那里。

Put your code into its own file (not embedded inside your existing Form code).将您的代码放入其自己的文件中(未嵌入到您现有的表单代码中)。 Click on Project --> Add Class --> Type in "NumericUpDownEx" --> Add.单击项目 --> 添加类 --> 键入“NumericUpDownEx” --> 添加。 Add a using System.Windows.Forms;添加一个using System.Windows.Forms; line at the top of the class so it knows how to inherit from the normal NumericUpDown.行在类的顶部,因此它知道如何从正常的 NumericUpDown 继承。 Then move all the code relating to NumericUpDownEx so it looks like below (your namespace will be different, of course):然后移动与 NumericUpDownEx 相关的所有代码,如下所示(当然,您的命名空间会有所不同):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_Scratch_WindowsFormsApp1
{

    public class NumericUpDownEx : NumericUpDown
    {
        string tennisAdv = "none";
        string whichNumUpDown = "unknown";

        public NumericUpDownEx()
        {

        }

        public override void UpButton()
        {
            if (Value == 0)
                Value = 15;
            else if (Value == 15)
                Value = 30;
            else if (Value == 30)
                Value = 40;
            else
                base.UpButton();
        }

        public override void DownButton()
        {
            if (Value == 40)
                Value = 30;
            else if (Value == 30)
                Value = 15;
            else if (Value == 15)
                Value = 0;
            else
                base.DownButton();
        }

        protected override void UpdateEditText()
        {
            if (Value > 40 & Value % 2 == 0)
            {
                this.Text = "Deuce";

            }
            else if (Value > 40 & Value % 2 != 0)
            {
                this.Text = "Adv";
            }
            else
            {
                this.Text = this.Value.ToString();
            }

        }

    }

}

Now, after rebuilding, you should get a new section at the top of your ToolBox with your new control in it:现在,重建后,您应该在 ToolBox 顶部获得一个新部分,其中包含您的新控件:

在此处输入图片说明

So instead of creating instances through code, you can place them on the form like other controls and work their properties and/or events as you'd expect.因此,不是通过代码创建实例,您可以将它们像其他控件一样放置在表单上,​​并按照您的预期处理它们的属性和/或事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM