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.
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");
}
}
}
}