简体   繁体   中英

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. 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.

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.

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. 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.” (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.

Put your code into its own file (not embedded inside your existing Form code). Click on Project --> Add Class --> Type in "NumericUpDownEx" --> Add. Add a using System.Windows.Forms; line at the top of the class so it knows how to inherit from the normal NumericUpDown. Then move all the code relating to NumericUpDownEx so it looks like below (your namespace will be different, of course):

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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