我有一个名为日程表的类,在我的主表单中,我打开编辑器窗口: (这里还有其他的东西,但我只想谈谈与我的问题有什么关系)
using (ScheduleManager smanager = new ScheduleManager(new List<Channels>(Outputs), new List<Schedule>(Schedules)))
{
smanager.ShowDialog();
if (smanager.DialogResult == DialogResult.OK)
{
Schedules = smanager.Schedules;
Outputs = smanager.Channels;
MessageBox.Show("Results OK");
}
在打开的窗口中。 ShowDialog(),如果我在子窗体,名称,时间等中编辑项目,我会在计划中下拉项目......这一切都会更新马上回到主窗体,我使用文本框和计时器来观察值。
但是,如果我在子窗体中向该对象添加一个项目,则最新项目不会添加到主窗体中的对象中,直到我单击“确定”,这将设置 Schedules = smanager.Schedules ,所以我明白为什么会有效。
但是我无法理解的是,即使我编辑了名称,并在子窗体中更新它,为什么它会被发送回主窗体中的对象,即使我没有单击确定按钮,这里也是我正在更新它:
以子形式:
从组合框中选择时:
private void cb_Schedule_SelectedIndexChanged(object sender, EventArgs e)
{
Schedule sitem = cb_Schedule.SelectedItem as Schedule;
if (sitem != null)
{
currentSchedule = sitem;
txt_ScheduleName.Text = sitem.Name;
checkEnabled.Checked = sitem.Enabled;
cb_Outputs.Text = sitem.Channel;
dateStartTime.Value = sitem.StartTime;
dateEndTime.Value = sitem.EndTime;
dateStart.Value = sitem.StartDate;
dateStop.Value = sitem.EndDate;
checkSunday.Checked = sitem.Sunday;
checkMonday.Checked = sitem.Monday;
checkTuesday.Checked = sitem.Tuesday;
checkWednesday.Checked = sitem.Wednesday;
checkThursday.Checked = sitem.Thursday;
checkFriday.Checked = sitem.Friday;
checkSaturday.Checked = sitem.Saturday;
}
}
编辑文本框并单击保存按钮后:
//Save Button
private void button2_Click(object sender, EventArgs e)
{
if (currentChannel != null)
currentChannel.ChannelName = txt_OutputName.Text;
if (currentSchedule != null)
{
currentSchedule.Name = txt_ScheduleName.Text;
currentSchedule.Enabled = checkEnabled.Checked;
currentSchedule.Channel = currentChannel.ChannelNumber;
currentSchedule.StartTime = dateStartTime.Value;
currentSchedule.EndTime = dateEndTime.Value;
currentSchedule.StartDate = dateStart.Value;
currentSchedule.EndDate = dateStop.Value;
currentSchedule.Sunday = checkSunday.Checked;
currentSchedule.Monday = checkMonday.Checked;
currentSchedule.Tuesday = checkTuesday.Checked;
currentSchedule.Wednesday = checkWednesday.Checked;
currentSchedule.Thursday = checkThursday.Checked;
currentSchedule.Friday = checkFriday.Checked;
currentSchedule.Saturday = checkSaturday.Checked;
cb_Schedule.Items.Clear();
PopulateSchedules();
}
else
MessageBox.Show("currentSchedule item is null");
}
立即以主窗体形式更新对象,我正在用计时器和文本框观看。
但是,当我添加到子窗体中的对象时:
//New Button
private void button1_Click(object sender, EventArgs e)
{
Schedule sitem = new Schedule();
if (txt_ScheduleName.Text != null)
sitem.Name = txt_ScheduleName.Text;
else
sitem.Name = "Default";
sitem.Enabled = checkEnabled.Checked;
sitem.Channel = currentChannel.ChannelNumber;
sitem.StartTime = dateStartTime.Value;
sitem.EndTime = dateEndTime.Value;
sitem.StartDate = dateStart.Value;
sitem.EndDate = dateStop.Value;
sitem.Sunday = checkSunday.Checked;
sitem.Monday = checkMonday.Checked;
sitem.Tuesday = checkTuesday.Checked;
sitem.Wednesday = checkWednesday.Checked;
sitem.Thursday = checkThursday.Checked;
sitem.Friday = checkFriday.Checked;
sitem.Saturday = checkSaturday.Checked;
currentSchedule = sitem;
Schedules.Add(sitem);
cb_Schedule.Items.Clear();
PopulateSchedules();
}
当我单击“确定”时,新的计划项目仅显示在主表单对象中,再次,我理解为什么当我单击“确定”时它会显示。
我不明白为什么我可以更改现有项目的名称,并且当我点击保存时会立即更新回主表单对象,但是当添加新项目时,使用< strong> Schedules.Add(sitem),它不会立即添加到主窗体对象。
我希望我已经提供了足够的代码,并且已经足够清楚地证明我无法理解这里发生的事情,谢谢你的帮助!