Resolved How can you combine/simplify redundent code

truenorth12lds

New member
Joined
May 23, 2020
Messages
4
Programming Experience
Beginner
How can the below code be simplified, so adding multiple additional objects isn't a pain?
For example, currently I will have to duplicate and modify every Timer, Textbox, and checkbox.
Full Code:
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //-----------------------------------------------------
        //-----------------  Ping Port  -----------------------
        //-----------------------------------------------------

        TcpClient tcpClient = new TcpClient();
        public static int port = 80; //<-- Can be changed to any port... default is HTTP port
        public static TcpClient client;

        //-----------------------------------------------------
        //-------------------  Timer  -------------------------
        //-----------------------------------------------------

        private void timer1_Tick(object sender, EventArgs e)
        {
            Ping ping = new Ping();
            PingReply pingresult = ping.Send((EHost1.Text));
            if (pingresult.Status.ToString() == "Success") //Add a function if it can ping the IP here...
            {
                PingStatus1.Text = "Online!";
                PingStatus1.ForeColor = Color.Green;
            }
            else
            {
                PingStatus1.Text = "Offline";
                PingStatus1.ForeColor = Color.Red;
            }
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            Ping ping = new Ping();
            PingReply pingresult = ping.Send((EHost2.Text));
            if (pingresult.Status.ToString() == "Success") //Add a function if it can ping the IP here...
            {
                PingStatus2.Text = "Online!";
                PingStatus2.ForeColor = Color.Green;
            }
            else
            {
                PingStatus2.Text = "Offline";
                PingStatus2.ForeColor = Color.Red;
            }
        }

        //-----------------------------------------------------
        //--------------------  Check Boxes  ------------------
        //-----------------------------------------------------

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                EHost1.ReadOnly = true;
                Regex rgx = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");
                if (rgx.IsMatch(EHost1.Text))
                {
                    timer1.Start();
                    timer1.Interval = int.Parse((Pingtime1.Text) + 999);
                    //EHost1.Click = MessageBox.Show("Please disable first");
                }
                else
                {
                    checkBox1.Checked = false;
                    MessageBox.Show("Invalid IP Address");
                }
            }
            else
            {
                EHost1.ReadOnly = false;
                PingStatus1.Text = "DISABLED";
                PingStatus1.ForeColor = Color.Orange;
                timer1.Stop();
            }
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                EHost2.ReadOnly = true;
                Regex rgx = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");
                if (rgx.IsMatch(EHost2.Text))
                {
                    timer2.Start();
                    timer2.Interval = int.Parse((Pingtime1.Text) + 999);
                    //EHost1.Click = MessageBox.Show("Please disable first");
                }
                else
                {
                    checkBox2.Checked = false;
                    MessageBox.Show("Invalid IP Address");
                }
            }
            else
            {
                EHost2.ReadOnly = false;
                PingStatus2.Text = "DISABLED";
                PingStatus2.ForeColor = Color.Orange;
                timer2.Stop();
            }
        }

        //-----------------------------------------------------
        //--------------------  TextBox's  --------------------
        //-----------------------------------------------------
        private void EHost1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                MessageBox.Show("Please Disable In order To Change");
            }
        }

        private void EHost2_Click(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                MessageBox.Show("Please Disable In order To Change");
            }
        }
    }
}
 
Perhaps you could explain what you're trying to achieve, rather than expecting us to work it out for ourselves from the code.

That said, if you want to treat multiple controls and components as a unit, you might consider creating a user control. You add a user control to your project and design it in pretty much the same way as you do a form. Once you build, the user control is added to the Toolbox automatically and you treat it like any other control. You can add the group of controls/components to your form in the designer or in code and you interact with it in code like you would any other control. It's up to you to add the appropriate properties, methods and events to the control. Note that you should not be able to interact with the child controls from outside the user control. If you had a Button and a TextBox and you wanted to know when the Button was clicked and be able to get and set the Text of the TextBox then you should provide your own event and property for that, e.g.
C#:
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public string TextBoxText
    {
        get => textBox1.Text;
        set => textBox1.Text = value;
    }

    public event EventHandler ButtonClick;

    private void button1_Click(object sender, EventArgs e)
    {
        ButtonClick?.Invoke(this, EventArgs.Empty);
    }
}
 
Sorry jmc. My intention was not to have you work it out. I simply don’t know how to simply the code. Maybe I should post each section as a separate question.

I’m trying to make it so I don’t have to duplicate the checkbox, timer, and textbox (EHost1), every time I want to create more. They all link to each other which is why I posted the full code.

Sorry if it was confusing. I’m new to this forum thing and C#. I started learning code about 4 days ago. Thank you though!
 
They all link to each other which is why I posted the full code.
I am only seeing the textbox, checkbox, and timer for each suffix are linked together. I am not seeing controls with suffix 1 talking to controls of suffix 2. Perhaps I am just missing it. If in fact I am not missing anything, then the suggestion of lumping the controls that talk to each other into a user control is an excellent suggestion.
 
Back
Top Bottom