我有16个不同的切换按钮。他们需要切换秒表。当按钮打开时,秒表启动,当按钮关闭时,秒表停止并重置。当有16个按钮时,这会变得复杂,每个按钮都需要控制它们各自的秒表。用户可以随时打开所有或部分按钮,打开各自的秒表。关于如何实现这一点的任何线索?我正在尝试线程,但如果不进入跨线程通信,这似乎不起作用。有任何想法吗?如果你愿意,我可以发布我拥有的代码,但它的概念很重要。
见鬼,以防万一,代码:
public partial class Main : Form
{
//declares global varaibles for easy use accross functions
bool[] btnstat = new bool[16];
int UnitNumber = 0, LineNumber = 0;
String IPaddress = "", message = "";
bool[] X = new bool[17];
String[] Y = new String[17];
bool[] Z = new bool[17];
double[] time = new double[16];
bool run = false;
String serial = Generator.GetMotherBoardID();
public Main()
{
InitializeComponent();
}//end Main()
private void Main_Load(object sender, EventArgs e)
{
//checks for initial setup
System.IO.StreamReader file = new System.IO.StreamReader("Welcome.config");
string x = file.ReadLine();
file.Close();
if (!String.Equals(x, "1px7ngme9dny"))
{
Welcome M = new Welcome();
M.ShowDialog();//pauses program until Welcome page has been closed
}
//Console.WriteLine("Serieal : " + serial);//For making sure the serial is pulled correctly
//Resets all buttons to disabled, invisible, and hidden off page.
Btn1.Enabled = false; Btn1.Visible = false; Btn1.Top = -30;
btn2.Enabled = false; btn2.Visible = false; btn2.Top = -30;
btn3.Enabled = false; btn3.Visible = false; btn3.Top = -30;
btn4.Enabled = false; btn4.Visible = false; btn4.Top = -30;
btn5.Enabled = false; btn5.Visible = false; btn5.Top = -30;
btn6.Enabled = false; btn6.Visible = false; btn6.Top = -30;
btn7.Enabled = false; btn7.Visible = false; btn7.Top = -30;
btn8.Enabled = false; btn8.Visible = false; btn8.Top = -30;
btn9.Enabled = false; btn9.Visible = false; btn9.Top = -30;
btn10.Enabled = false; btn10.Visible = false; btn10.Top = -30;
btn11.Enabled = false; btn11.Visible = false; btn11.Top = -30;
btn12.Enabled = false; btn12.Visible = false; btn12.Top = -30;
btn13.Enabled = false; btn13.Visible = false; btn13.Top = -30;
btn14.Enabled = false; btn14.Visible = false; btn14.Top = -30;
btn15.Enabled = false; btn15.Visible = false; btn15.Top = -30;
btn16.Enabled = false; btn16.Visible = false; btn16.Top = -30;
FileReader();
Updater(); //calls updater to update the program with configuration and calculations
}//end Main_Load
public void plusplus()
{
for (int i = 0; i < 16; i++)
{
if (btnstat[i])
time[i]++;
else
time[i] = 0;
}
Updater();
}
public void FileReader()
{
//declaration
System.IO.StreamReader file2 = new System.IO.StreamReader("ConfigurationSettings.config");
//loads all saved data into memory
for (int i = 1; i <= 53; i++)
{
if (i == 1)
{
if (String.Equals(file2.ReadLine(), "Yes"))
{
}
}
else if (i == 2)
{
String z = file2.ReadLine();
if (!String.Equals(null, z))
CompName.Text = z;
else
CompName.Text = "Company Name";
}
else if (i == 3)
{
String z = file2.ReadLine();
if (!String.Equals(null, z))
CompLoc.Text = z;
else
CompLoc.Text = "Company Location";
}
else if (i == 4)
{
String z = file2.ReadLine();
if (!String.Equals(null, z))
{
LineNum.Text = "Line: " + z; LineNumber = Convert.ToInt32(z);
}
else
LineNum.Text = "Line Number: ";
}
else if (i == 5)
{
String z = file2.ReadLine();
if (!String.Equals(null, z))
{
UnitNum.Text = "Unit: " + z; UnitNumber = Convert.ToInt32(z);
}
else
UnitNum.Text = "Unit Number: ";
}
else if ((i >= 6) && (i <= 21))
X[i - 6] = Convert.ToBoolean(file2.ReadLine());
else if ((i >= 22) && (i <= 37))
Y[i - 22] = file2.ReadLine();
else if ((i >= 38) && (i <= 53))
Z[i - 38] = Convert.ToBoolean(file2.ReadLine());
}
IPaddress = file2.ReadLine();
file2.Close();
}
public void Updater()//This reloads everything. From status to what buttons are enabled.
{
//Sets all buttons again
int j = 0;
if (X[0])
{
Btn1.Enabled = true; Btn1.Visible = true; Btn1.Top = (80 + j); Btn1.Text = Y[0]; j += 46;
}
if (X[1])
{
btn2.Enabled = true; btn2.Visible = true; btn2.Top = (80 + j); btn2.Text = Y[1]; j += 46;
}
if (X[2])
{
btn3.Enabled = true; btn3.Visible = true; btn3.Top = (80 + j); btn3.Text = Y[2]; j += 46;
}
if (X[3])
{
btn4.Enabled = true; btn4.Visible = true; btn4.Top = (80 + j); btn4.Text = Y[3]; j += 46;
}
if (X[4])
{
btn5.Enabled = true; btn5.Visible = true; btn5.Top = (80 + j); btn5.Text = Y[4]; j += 46;
}
if (X[5])
{
btn6.Enabled = true; btn6.Visible = true; btn6.Top = (80 + j); btn6.Text = Y[5]; j += 46;
}
if (X[6])
{
btn7.Enabled = true; btn7.Visible = true; btn7.Top = (80 + j); btn7.Text = Y[6]; j += 46;
}
if (X[7])
{
btn8.Enabled = true; btn8.Visible = true; btn8.Top = (80 + j); btn8.Text = Y[7]; j += 46;
}
if (X[8])
{
btn9.Enabled = true; btn9.Visible = true; btn9.Top = (80 + j); btn9.Text = Y[8]; j += 46;
}
if (X[9])
{
btn10.Enabled = true; btn10.Visible = true; btn10.Top = (80 + j); btn10.Text = Y[9]; j += 46;
}
if (X[10])
{
btn11.Enabled = true; btn11.Visible = true; btn11.Top = (80 + j); btn11.Text = Y[10]; j += 46;
}
if (X[11])
{
btn12.Enabled = true; btn12.Visible = true; btn12.Top = (80 + j); btn12.Text = Y[11]; j += 46;
}
if (X[12])
{
btn13.Enabled = true; btn13.Visible = true; btn13.Top = (80 + j); btn13.Text = Y[12]; j += 46;
}
if (X[13])
{
btn14.Enabled = true; btn14.Visible = true; btn14.Top = (80 + j); btn14.Text = Y[13]; j += 46;
}
if (X[14])
{
btn15.Enabled = true; btn15.Visible = true; btn15.Top = (80 + j); btn15.Text = Y[14]; j += 46;
}
if (X[15])
{
btn16.Enabled = true; btn16.Visible = true; btn16.Top = (80 + j); btn16.Text = Y[15]; j += 46;
}
this.Size = new System.Drawing.Size(175, (120 + j));
//sets correct 'On if all others are off'
bool allareoff = true;
for (int i = 0; i < 16; i++)
{
if (btnstat[i] == true && Z[i] == false)
allareoff = false;
}
if (allareoff)
{
for (int i = 0; i < 16; i++)
{
if (Z[i] == true)
btnstat[i] = true;
}
}
else
{
for (int i = 0; i < 16; i++)
{
if (Z[i] == true)
btnstat[i] = false;
}
}
//updates buttons to current status
if (btnstat[0])
{
Btn1.FlatStyle = FlatStyle.Flat; Btn1.Image = ((System.Drawing.Image)(Properties.Resources.Rl)); Btn1.Text = Y[0] + "\r\n" + time[0];
}
else
{
Btn1.FlatStyle = FlatStyle.Popup; Btn1.Image = ((System.Drawing.Image)(Properties.Resources.Rd));Btn1.Text = Y[0];time[0] = 0;
}
if (btnstat[1])
{
btn2.FlatStyle = FlatStyle.Flat; btn2.Image = ((System.Drawing.Image)(Properties.Resources.Rl)); btn2.Text = Y[1] + "\r\n" + time[1];
}
else
{
btn2.FlatStyle = FlatStyle.Popup; btn2.Image = ((System.Drawing.Image)(Properties.Resources.Rd));btn2.Text = Y[1];time[1] = 0;
}
if (btnstat[2])
{
btn3.FlatStyle = FlatStyle.Flat; btn3.Image = ((System.Drawing.Image)(Properties.Resources.Yl)); btn3.Text = Y[2] + "\r\n" + time[2];
}
else
{
btn3.FlatStyle = FlatStyle.Popup; btn3.Image = ((System.Drawing.Image)(Properties.Resources.Yd));btn3.Text = Y[2];time[2] = 0;
}
if (btnstat[3])
{
btn4.FlatStyle = FlatStyle.Flat; btn4.Image = ((System.Drawing.Image)(Properties.Resources.Yl)); btn4.Text = Y[3] + "\r\n" + time[3];
}
else
{
btn4.FlatStyle = FlatStyle.Popup; btn4.Image = ((System.Drawing.Image)(Properties.Resources.Yd));btn4.Text = Y[3];time[3] = 0;
}
if (btnstat[4])
{
btn5.FlatStyle = FlatStyle.Flat; btn5.Image = ((System.Drawing.Image)(Properties.Resources.Gl)); btn5.Text = Y[4] + "\r\n" + time[4];
}
else
{
btn5.FlatStyle = FlatStyle.Popup; btn5.Image = ((System.Drawing.Image)(Properties.Resources.Gd));btn5.Text = Y[4];time[4] = 0;
}
if (btnstat[5])
{
btn6.FlatStyle = FlatStyle.Flat; btn6.Image = ((System.Drawing.Image)(Properties.Resources.Gl)); btn6.Text = Y[5] + "\r\n" + time[5];
}
else
{
btn6.FlatStyle = FlatStyle.Popup; btn6.Image = ((System.Drawing.Image)(Properties.Resources.Gd));btn6.Text = Y[5];time[5] = 0;
}
if (btnstat[6])
{
btn7.FlatStyle = FlatStyle.Flat; btn7.Image = ((System.Drawing.Image)(Properties.Resources.Bl)); btn7.Text = Y[6] + "\r\n" + time[6];
}
else
{
btn7.FlatStyle = FlatStyle.Popup; btn7.Image = ((System.Drawing.Image)(Properties.Resources.Bd));btn7.Text = Y[6];time[6] = 0;
}
if (btnstat[7])
{
btn8.FlatStyle = FlatStyle.Flat; btn8.Image = ((System.Drawing.Image)(Properties.Resources.Bl)); btn8.Text = Y[7] + "\r\n" + time[7];
}
else
{
btn8.FlatStyle = FlatStyle.Popup; btn8.Image = ((System.Drawing.Image)(Properties.Resources.Bd));btn8.Text = Y[7];time[7] = 0;
}
if (btnstat[8])
{
btn9.FlatStyle = FlatStyle.Flat; btn9.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn9.Text = Y[8] + "\r\n" + time[8];
}
else
{
btn9.FlatStyle = FlatStyle.Popup; btn9.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn9.Text = Y[8]; time[8] = 0;
}
if (btnstat[9])
{
btn10.FlatStyle = FlatStyle.Flat; btn10.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn10.Text = Y[9] + "\r\n" + time[9];
}
else
{
btn10.FlatStyle = FlatStyle.Popup; btn10.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn10.Text = Y[9]; time[9] = 0;
}
if (btnstat[10])
{
btn11.FlatStyle = FlatStyle.Flat; btn11.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn11.Text = Y[10] + "\r\n" + time[10];
}
else
{
btn11.FlatStyle = FlatStyle.Popup; btn11.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn11.Text = Y[10]; time[10] = 0;
}
if (btnstat[11])
{
btn12.FlatStyle = FlatStyle.Flat; btn12.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn12.Text = Y[11] + "\r\n" + time[11];
}
else
{
btn12.FlatStyle = FlatStyle.Popup; btn12.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn12.Text = Y[11]; time[11] = 0;
}
if (btnstat[12])
{
btn13.FlatStyle = FlatStyle.Flat; btn13.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn13.Text = Y[12] + "\r\n" + time[12];
}
else
{
btn13.FlatStyle = FlatStyle.Popup; btn13.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn13.Text = Y[12]; time[12] = 0;
}
if (btnstat[13])
{
btn14.FlatStyle = FlatStyle.Flat; btn14.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn14.Text = Y[13] + "\r\n" + time[13];
}
else
{
btn14.FlatStyle = FlatStyle.Popup; btn14.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn14.Text = Y[13]; time[13] = 0;
}
if (btnstat[14])
{
btn15.FlatStyle = FlatStyle.Flat; btn15.Image = ((System.Drawing.Image)(Properties.Resources.Wl));btn15.Text = Y[14] + "\r\n" + time[14];
}
else
{
btn15.FlatStyle = FlatStyle.Popup; btn15.Image = ((System.Drawing.Image)(Properties.Resources.Wd)); btn15.Text = Y[14]; time[14] = 0;
}
if (btnstat[15])
{
btn16.FlatStyle = FlatStyle.Flat; btn16.Image = ((System.Drawing.Image)(Properties.Resources.Wl));btn16.Text = Y[15] + "\r\n" + time[15];
}
else
{
btn16.FlatStyle = FlatStyle.Popup; btn16.Image = ((System.Drawing.Image)(Properties.Resources.Wl)); btn16.Text = Y[15]; time[15] = 0;
}
//sends all the data;
String[] intA = new String[16];
String ln,un = "";
for (int i = 0; i < 16; i++)
{
if (btnstat[i] == true)
intA[i] = "0";
else
intA[i] = "1";
}
if (LineNumber < 10)
ln = "0" + LineNumber;
else
ln = Convert.ToString(LineNumber);
if (UnitNumber < 10)
un = "0" + UnitNumber;
else
un = Convert.ToString(UnitNumber);
//creates the string to send.
message ="" + ln + " " + un + " " +intA[7]+intA[6]+intA[5]+intA[4]+intA[3]+intA[2]+intA[1]+intA[0]+" "+intA[15]+intA[14]+intA[13]+intA[12]+intA[11]+intA[10]+intA[9]+intA[8]+" "+serial;
IPLabel.Text = "IP: " + IPaddress;
}//end updater
public void Sender()
{
Console.WriteLine(IPaddress + ": " + message);//for referancing
Send(IPaddress, 8000, message);//sends message
}
public void Send(string server, int port, string message)
{
try
{
// Create a TcpClient.
TcpClient client = new TcpClient(server, port);
// Translate
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream
NetworkStream stream = client.GetStream();
// Send the message
stream.Write(data, 0, data.Length);
// Close
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
// trap error
}
catch (SocketException e)
{
// trap error
}
} //end send
private void IALogo_Click(object sender, EventArgs e)
{
Config config = new Config();
config.ShowDialog(); // code here STOPS until "config" is closed
FileReader();
Updater();
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void toolTip1_Popup(object sender, PopupEventArgs e){}
private void button1_Click(object sender, EventArgs e){}
private void CompLoc_Click(object sender, EventArgs e){}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e){}
private void button5_Click(object sender, EventArgs e)
{
btnstat[5] = !btnstat[5];
var stopwatch = new Stopwatch();
if(btnstat[4])
{
stopwatch.Start();
time[4] = stopwatch.ElapsedMilliseconds / 1000;
}
else
{
stopwatch.Stop();
time[4] = 0;
}
Updater();
Sender();
}
private void Btn1_Click(object sender, EventArgs e)
{
btnstat[0] = !btnstat[0];
Updater();
Sender();
}
private void btn2_Click(object sender, EventArgs e)
{
btnstat[1] = !btnstat[1];
Updater();
Sender();
}
private void btn3_Click(object sender, EventArgs e)
{
btnstat[2] = !btnstat[2];
Updater();
Sender();
}
private void btn4_Click(object sender, EventArgs e)
{
btnstat[3] = !btnstat[3];
Updater();
Sender();
}
private void btn5_Click(object sender, EventArgs e)
{
btnstat[4] = !btnstat[4];
Updater();
Sender();
}
private void btn7_Click(object sender, EventArgs e)
{
btnstat[6] = !btnstat[6];
Updater();
Sender();
}
private void btn8_Click(object sender, EventArgs e)
{
btnstat[7] = !btnstat[7];
Updater();
Sender();
}
private void btn9_Click(object sender, EventArgs e)
{
btnstat[8] = !btnstat[8];
Updater();
Sender();
}
private void btn10_Click(object sender, EventArgs e)
{
btnstat[9] = !btnstat[9];
Updater();
Sender();
}
private void btn11_Click(object sender, EventArgs e)
{
btnstat[10] = !btnstat[10];
Updater();
Sender();
}
private void btn12_Click(object sender, EventArgs e)
{
btnstat[11] = !btnstat[11];
Updater();
Sender();
}
private void btn13_Click(object sender, EventArgs e)
{
btnstat[12] = !btnstat[12];
Updater();
Sender();
}
private void btn14_Click(object sender, EventArgs e)
{
btnstat[13] = !btnstat[13];
Updater();
Sender();
}
private void btn15_Click(object sender, EventArgs e)
{
btnstat[14] = !btnstat[14];
Updater();
Sender();
}
private void btn16_Click(object sender, EventArgs e)
{
btnstat[15] = !btnstat[15];
Updater();
Sender();
}
感谢您的帮助!