我有一个应用程序,其中表单类DataEntryForm.Show.Dialog();在每小时的57分钟每小时执行一次。我展示的表单的构造函数有一些代码来初始化一些变量,这些变量用于设置表单中组合框的索引。启动应用程序后,显示的第一个表单是完美的。用户填充并单击按钮以记录数据并关闭表单。表格下一个小时再次显示,但变量没有更新,我希望它们增加一个小时。我已经尝试了很多东西:处理,移动变量以将构造函数初始化为另一个方法,并从Form_Shown事件处理程序调用该方法。我想我错过了一些简单的东西...我是新手。看起来即使表单关闭,下一个show.dialog也是原始表单的相同实例。
//Launching Data Entry Form Code
private void button2_Click(object sender, EventArgs e) // note the DataEntryTimer.Elapsed event raises the button2_click event in addition to the form button.
{
worker1 = textBox1.Text;
worker2 = textBox2.Text;
worker3 = textBox3.Text;
worker4 = textBox4.Text;
// keep this timer interval such that it triggers the next button event at 57 minutes past the hour.
DataEntryTimer.Stop();
if (57 - DateTime.Now.Minute == 0)
{ DataEntryTimer.Interval = 60 * 60 * 1000; }
if (57 - DateTime.Now.Minute == -1)
{ DataEntryTimer.Interval = 59 * 60 * 1000; }
if (57 - DateTime.Now.Minute == -2)
{ DataEntryTimer.Interval = (58) * 60 * 1000; }
else if (57 - DateTime.Now.Minute > 0)
{ DataEntryTimer.Interval = (57 - DateTime.Now.Minute) * 60 * 1000; }
DataEntryTimer.Start();
if (string.IsNullOrEmpty(comboBox1.Text) | string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Please Select a Work Center and at least 1 worker first.\n\nIf your name is not in the check box list, \nyou can type it into the text field above the list. The field labeled 'Worker 1' must contain a name to record data.", "Select Work Center & Worker(s) First!");
}
else
{
LaunchDataEntryForm();
}
}
public void LaunchDataEntryForm()
{
Form form = null;
//search all opened forms for one with name "DataEntryForm"
foreach (Form frm in Application.OpenForms)
if (frm.Name == "DataEntryForm") //this requires Form2 to be named "DataEntryForm"
{
frm.Dispose();
break;
}
DataEntryForm DataEntryForm = new DataEntryForm();
DataEntryForm.StartPosition = FormStartPosition.CenterParent;
DataEntryForm.ShowDialog();
这是Form2的代码:
public DataEntryForm() // initialize
{
InitializeComponent();
DataEntryFormInitialize();
}
public void DataEntryFormInitialize()
{
// production hr----------------------
if (clockHr >= 0 && clockHr < 8)
{ productionHr = clockHr + 1; }
else if (clockHr >= 8 && clockHr < 16)
{ productionHr = clockHr - 7; }
else
{ productionHr = clockHr - 15; }
label17.Text = Form1.globalWorkCenter;
comboBox1.Items.Add(new CrewItem("A", 1));
comboBox1.Items.Add(new CrewItem("B", 2));
comboBox1.Items.Add(new CrewItem("C", 3));
comboBox2.Items.Add(new ShiftItem("midnight", 1));
comboBox2.Items.Add(new ShiftItem("day", 2));
comboBox2.Items.Add(new ShiftItem("afternoon", 3));
// shift---------------------
if (clockHr >= 0 && clockHr < 8)
{ shift = "midnight"; }
else if (clockHr >= 8 && clockHr < 16)
{ shift = "day"; }
else
{ shift = "afternoon"; }
if (shift == "midnight")
{
comboBox3.Items.Add(new ProdHrItem("12:00am - 1:00am, Hour 1", 0));
comboBox3.Items.Add(new ProdHrItem("1:00am - 2:00am, Hour 2", 1));
comboBox3.Items.Add(new ProdHrItem("2:00am - 3:00am, Hour 3", 2));
comboBox3.Items.Add(new ProdHrItem("3:00am - 4:00am, Hour 4", 3));
comboBox3.Items.Add(new ProdHrItem("4:00am - 5:00am, Hour 5", 4));
comboBox3.Items.Add(new ProdHrItem("5:00am - 6:00am, Hour 6", 5));
comboBox3.Items.Add(new ProdHrItem("6:00am - 7:00am, Hour 7", 6));
comboBox3.Items.Add(new ProdHrItem("7:00am - 8:00am, Hour 8", 7));
}
if (shift == "day")
{
comboBox3.Items.Add(new ProdHrItem("8:00am - 9:00am, Hour 1", 8));
comboBox3.Items.Add(new ProdHrItem("9:00am - 10:00am, Hour 2", 9));
comboBox3.Items.Add(new ProdHrItem("10:00am - 11:00am, Hour 3", 10));
comboBox3.Items.Add(new ProdHrItem("11:00am - 12:00pm, Hour 4", 11));
comboBox3.Items.Add(new ProdHrItem("12:00pm - 1:00pm, Hour 5", 12));
comboBox3.Items.Add(new ProdHrItem("1:00pm - 2:00pm, Hour 6", 13));
comboBox3.Items.Add(new ProdHrItem("2:00pm - 3:00pm, Hour 7", 14));
comboBox3.Items.Add(new ProdHrItem("3:00pm - 4:00pm, Hour 8", 15));
}
if (shift == "afternoon")
{
comboBox3.Items.Add(new ProdHrItem("4:00pm - 5:00pm, Hour 1", 16));
comboBox3.Items.Add(new ProdHrItem("5:00pm - 6:00pm, Hour 2", 17));
comboBox3.Items.Add(new ProdHrItem("6:00pm - 7:00pm, Hour 3", 18));
comboBox3.Items.Add(new ProdHrItem("7:00pm - 8:00pm, Hour 4", 19));
comboBox3.Items.Add(new ProdHrItem("8:00pm - 9:00pm, Hour 5", 20));
comboBox3.Items.Add(new ProdHrItem("9:00pm - 10:00pm, Hour 6", 21));
comboBox3.Items.Add(new ProdHrItem("10:00pm - 11:00pm, Hour 7", 22));
comboBox3.Items.Add(new ProdHrItem("11:00pm - 12:00pm, Hour 8", 23));
}
// select the default production hour in ComboBox3.
Update_Hour();
}
// ---- production hour default index setting
private void Update_Hour()
{
int indexoffset = 0;
if (clockHr == 0 | clockHr < 8)
{ indexoffset = 0; }
else if (clockHr >= 8 && clockHr < 16)
{ indexoffset = 8; }
else if (clockHr >= 16 && clockHr <= 23)
{ indexoffset = 16; }
comboBox3.SelectedIndex = clockHr - indexoffset; //valid indeces are 0 to 7... so hour 0,8,16 = index 0
private void button1_Click(object sender, EventArgs e) /// ---------INITIATE RECORD DATA TO DATABASE-----------
{
if (textBox2.Text == "0")
{
if (MessageBox.Show("No cuts, or red time were recorded.\n\nAre you sure you want to record this data?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ RecordRedGreenData(); }
}
else
{ RecordRedGreenData(); }
}
private void RecordRedGreenData()
{
int selectedIndex = comboBox3.SelectedIndex;
if (!recordOperatorData.IsBusy)
{ recordOperatorData.RunWorkerAsync(); }
}
}
private void recordOperatorData_DoWork(object sender, DoWorkEventArgs e)
{
SqlUpdate_Red_Green_Records myRecordInsert = new SqlUpdate_Red_Green_Records(); // instantiate an sql insert updateobject
myRecordInsert.UpdateRedGreenNow(); // call the method in the class to initiate the connection and data record insert. Pass the record set as the argument.
}
// close the form after the sql insert/update is complete
private void recordOperatorData_RunWorkerCompleted(object sender, unWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("Operator hourly product record Background Task Canceled!");
}
else if (e.Error != null)
{
MessageBox.Show("Operarator hourly product record Task Error: " + e.Error.Message);
}
Close();
}
public static DateTime productionDateTimeobj = DateTime.Now.AddMinutes(-30);
public static string productionDateTime = productionDateTimeobj.ToString();
public static int yearMo = int.Parse(productionDateTimeobj.ToString("yyyyMM"));
public int clockHr = Convert.ToInt32(productionDateTimeobj.ToString("HH"));
public static int productionHr;
答案 0 :(得分:0)
您正在创建一个新的对话框表单实例,因此它无法使用已处理的表单。
DataEntryForm DataEntryForm = new DataEntryForm();
<强> DataEntryForm.ShowDialog(); 强>
你能详细说明以下句子吗?
&#34;但变量没有更新,我期待它们 增加一小时&#34;
也许问题是逻辑而不是窗口。我强烈建议将逻辑分离到专用方法,并测试它,无论表单如何。通过这样做,您可以隔离问题。 我希望它能带领你。
答案 1 :(得分:0)
Problem solved. clockHr was declared and a value was defined at the class level. The value should have been defined in the constructor. Working perfect now.