Home > OS >  Why when I click messagebox OK button the close code not execute?
Why when I click messagebox OK button the close code not execute?

Time:12-25

I have windows form application and I want to prevent making duplicate orders for the same patient

and I check if the patient have order today and if I try to make another order it will show message

patient have order today then when click ok button I need to close the window , but its not closing the window when click ok button ,

This is the code I used :

var patientNumber = order.GetPatientNumber(Convert.ToInt32(textPatientID.Text),Program.branch_id); // int?
            bool patientExists = patientNumber.HasValue;
            if (patientExists == true)
            {
                DialogResult dialogResult = MessageBox.Show("PATIENT HAS ORDER TODAY ", "DUPLICATE ORDER ", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                if (dialogResult == DialogResult.OK)
                {
                    this.Close();
                }

            }

I tried also to close the form without if but also not closing :

var patientNumber = order.GetPatientNumber(Convert.ToInt32(textPatientID.Text),Program.branch_id); // int?
            bool patientExists = patientNumber.HasValue;
            if (patientExists == true)
            {
                DialogResult dialogResult = MessageBox.Show("PATIENT HAS ORDER TODAY ", "DUPLICATE ORDER ", MessageBoxButtons.OK, MessageBoxIcon.Stop);
               
                    this.Close();
               

            }

why its not closing what's the wrong please your help ?

CodePudding user response:

I can see no reason in

if (dialogResult == DialogResult.OK)

check, since the dialog has the only OK button:

if (order
    .GetPatientNumber(int.Parse(textPatientID.Text), Program.branch_id)
    .HasValue) {
  MessageBox.Show(
    "PATIENT HAS ORDER TODAY", 
    "DUPLICATE ORDER", 
     MessageBoxButtons.OK, 
     MessageBoxIcon.Stop);

  Close();
}
  
  •  Tags:  
  • c#
  • Related