0

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.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
KeLorean
  • 3
  • 5

1 Answers1

0

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:

enter image description here

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.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • ahh! that sounds like an essential thing for readability that I haven't been doing. now, should i just ADD this into the "...design.cs"/namespace that is already apart of my project? – KeLorean Jun 13 '19 at 15:18
  • Yes, same namespace. – Idle_Mind Jun 13 '19 at 15:26
  • this solution turned out pretty dope! now, I can see my custom Numeric Up Downs in "design mode", and I can create events for these controls, adjust their properties in "design mode", and reference these controls to adjust properties programmatically. the community here on stackoverflow is absolutely priceless! search a problem, ask a question, and it is amazing how fast you can make some pretty awesome stuff! thanks @Idle_Mind – KeLorean Jun 13 '19 at 18:32