c# - 加载新窗体时显示消息框
问题描述
我正在开发一个列出约会并允许用户添加、更新或删除选定约会的项目。更新时,选择要更新的约会,它会打开一个新窗口进行更改。我有一个嵌套在 try-catch 块中的消息框,但是当表单加载时它仍然会弹出。
private void updateButton_Click(object sender, EventArgs e)
{
//updates the type, but for all customers with same
//appointment type. Work in progress.
MySqlConnection c = new MySqlConnection(SqlUpdater.conString);
MySqlCommand updateCmd = new MySqlCommand();
DateTime startTime = startTimeBox.Value.ToUniversalTime();
DateTime endTime = endTimeBox.Value.ToUniversalTime();
updateCmd.Connection = c;
c.Open();
updateCmd.CommandText = $"UPDATE appointment SET type = @type, start = @start, end = @end WHERE customerId = @customerId";
updateCmd.Parameters.AddWithValue("@type", typeBox.Text);
updateCmd.Parameters.AddWithValue("@start", (DateTime)startTimeBox.Value);
updateCmd.Parameters.AddWithValue("@end", (DateTime)endTimeBox.Value);
updateCmd.Parameters.AddWithValue("@customerId", customerIdBox.Text);
updateCmd.ExecuteNonQuery();
c.Close();
try
{
if (appHasConflict(startTime, endTime))
throw new appointmentException();
else
{
try
{
if (appIsOutsideBusinessHours(startTime, endTime))
throw new appointmentException();
else
{
MessageBox.Show("Appointment Updated");
}
}
catch (appointmentException ex) { ex.businessHours(); }
}
}
catch (appointmentException ex) { ex.appOverlap(); }
mainFormObject.updateCalendar();
}
在主窗体上单击更新按钮:
private void updateAppointmentButton_Click(object sender, EventArgs e)
{
if (appointmentCalendar.SelectedRows.Count == 0)
{
MessageBox.Show("Please select a row to update");
}
else
{
UpdateAppointment updateAppointment = new UpdateAppointment();
updateAppointment.mainFormObject = this;
updateAppointment.customerIdBox.Text = appointmentCalendar.CurrentRow.Cells[0].Value.ToString();
updateAppointment.customerNameBox.Text = appointmentCalendar.CurrentRow.Cells[1].Value.ToString();
updateAppointment.typeBox.Text = appointmentCalendar.CurrentRow.Cells[2].Value.ToString();
updateAppointment.startTimeBox.Value = Convert.ToDateTime(appointmentCalendar.CurrentRow.Cells[4].Value.ToString());
updateAppointment.endTimeBox.Value = Convert.ToDateTime(appointmentCalendar.CurrentRow.Cells[3].Value.ToString());
updateAppointment.Show();
}
}
此消息框的唯一实例出现在 try-catch 块中。
编辑:将 try-catch 更新为:
if(appIsOutsideBusinessHours(startTime, endTime))
{
MessageBox.Show("Exception occured. Please reschedule for normal business hours.");
return;
}
if(appHasConflict(startTime, endTime))
{
MessageBox.Show("Excepetion occured. Appointment time is already taken.");
return;
}
else
{
MessageBox.Show("Appointment updated");
mainFormObject.updateCalendar();
}
但它仍然在表单加载时显示约会更新框。
解决方案
推荐阅读
- echarts - eCharts - 我们可以使用 echarts 自定义图表功能创建 mekko 图表吗
- django - Django“不在”查询相关字段
- r - Geom_area 图不填充线之间的区域
- jquery - jQuery检测移动浏览器关闭事件
- laravel - Laravel - 在模型 eloquent 中获取枚举值
- mysql - MYSQL 查询 GROUP BY DESC
- rest - 如何在 C# 中刷新访问令牌
- firebase - 我应该使用哪个 API 来交换 Firebase 自定义令牌?
- flutter - 无法在构建小部件下的颤动中添加 2 个按钮
- reactjs - Expo字体加载不正确