首页 > 解决方案 > 加载新窗体时显示消息框

问题描述

我正在开发一个列出约会并允许用户添加、更新或删除选定约会的项目。更新时,选择要更新的约会,它会打开一个新窗口进行更改。我有一个嵌套在 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();
        }

但它仍然在表单加载时显示约会更新框。

标签: c#try-catchmessagebox

解决方案


推荐阅读