How do you Cancel the Form closing on DialogResult.Yes click?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    How do you Cancel the Form closing on DialogResult.Yes click?

    Hello,
    I am building an application and on a button click, a new dialog form is built and has three buttons. When the user clicks on the Next button (DialogResult.Y es), I do not want the form to close. However, I am having trouble with the form not closing. Can you provide any help? Here is the method that I currently have in place.

    Code:
     private void manualEntry(object sender, EventArgs e)
            {
                System.Windows.Forms.Button next = new System.Windows.Forms.Button();
                next.Text = "Next";
                next.Location = new System.Drawing.Point(10, 70);
                next.DialogResult = DialogResult.Yes;
                
                
                System.Windows.Forms.Button okay = new System.Windows.Forms.Button();
                okay.Text = "Complete";
                okay.Location = new System.Drawing.Point(105, 70);
                okay.DialogResult = DialogResult.OK;
    
                System.Windows.Forms.Button cancel = new System.Windows.Forms.Button();
                cancel.Text = "Cancel";
                cancel.Location = new System.Drawing.Point(200, 70);
                cancel.DialogResult = DialogResult.Cancel;
    
                System.Windows.Forms.TextBox sID = new System.Windows.Forms.TextBox();
                sID.Location = new System.Drawing.Point(60, 10);
                sID.Size = new System.Drawing.Size(215, 50);
    
                System.Windows.Forms.TextBox sName = new System.Windows.Forms.TextBox();
                sName.Location = new System.Drawing.Point(60, 40);
                sName.Size = new System.Drawing.Size(215, 50);
    
                System.Windows.Forms.Label lblID = new System.Windows.Forms.Label();
                lblID.Font = new System.Drawing.Font(lblID.Font.Name, 12, lblID.Font.Style, lblID.Font.Unit);
                lblID.Location = new System.Drawing.Point(5, 10);
                lblID.Text = "ID:";
    
                System.Windows.Forms.Label llblName = new System.Windows.Forms.Label();
                llblName.Font = new System.Drawing.Font(llblName.Font.Name, 12, llblName.Font.Style, llblName.Font.Unit);
                llblName.Location = new System.Drawing.Point(5, 40);
                llblName.Text = "Name:";
    
                Form dialog = new Form();
                dialog.Size = new System.Drawing.Size(300, 140);
                dialog.MaximizeBox = false;
                dialog.MinimizeBox = false;
                dialog.FormBorderStyle = FormBorderStyle.FixedSingle;
                dialog.StartPosition = FormStartPosition.CenterScreen;
                dialog.Text = "Manual Student Entry";
                dialog.AcceptButton = okay;
                dialog.CancelButton = cancel;
                dialog.Controls.Add(next);
                dialog.Controls.Add(okay);
                dialog.Controls.Add(cancel);
                dialog.Controls.Add(sID);
                dialog.Controls.Add(sName);
                dialog.Controls.Add(lblID);
                dialog.Controls.Add(llblName);
                dialog.ShowDialog();
    
                
                if (dialog.DialogResult == DialogResult.OK)
                {
                    if (sID.Text == "" | sName.Text == "")
                    {
                        MessageBox.Show("A student id and student name must be entered to be processed,");
                        return;
                    }
                    else
                    {
                        oXL = new Microsoft.Office.Interop.Excel.Application();
                        oXL.Visible = false;
                        // oWK = oXL.Workbooks.Open(path + "\\" + fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                        oWK = oXL.Workbooks.Open(path + "\\" + fileName);
                        oWS = (Microsoft.Office.Interop.Excel.Worksheet)oWK.Worksheets[1];
                        oWS.Cells[i, 1] = sID.Text.ToString();
                        oWS.Cells[i, 2] = sName.Text.ToString();
                        i++;
                        oWK.Save();
                        oWK.Close();
                        oXL.Workbooks.Close();
    
                    }
    
    
    
                }
                if (dialog.DialogResult == DialogResult.Yes)
                {
                    dialog.DialogResult = DialogResult.None;
                }
            }
        }
  • Falkeli
    New Member
    • Feb 2012
    • 19

    #2
    By the time the code reaches your condition here, the dialog is already closed. What you need to do is edit the code of the dialog itself - and remove the DialogResult.Ye s result from the "Next" button. Or else you can simpoly re-open the dialog box with an other ShowDialog().

    Comment

    • Brian Connelly
      New Member
      • Jan 2011
      • 103

      #3
      Thank you Falkeli. You kind of pointed me in the right direction because I was headed down the wrong path. Removing the Dialog Result Yes was neccessary. At this point I just had a button which I then needed to add an event handler on the click. What was also needed was some how to store the values of the controls on the dialog form. I couln't loop and find the controls using the this.control because it would only find the controls of the main form that called the method. What I did here is declare two private variables and add event handlers on those control textboxes. The event was on the leave event where I stored the values in the private variables. Then when the button was clicked, My need logic and value checking performed what I needed. Here is the code:
      Code:
       private void manualEntry(object sender, EventArgs e)
              {
                  #region formControls
                  System.Windows.Forms.Button next = new System.Windows.Forms.Button();
                  next.Text = "Next";
                  next.Location = new System.Drawing.Point(10, 70);
      
                  System.Windows.Forms.Button okay = new System.Windows.Forms.Button();
                  okay.Text = "Complete";
                  okay.Location = new System.Drawing.Point(105, 70);
                  okay.DialogResult = DialogResult.OK;
      
                  System.Windows.Forms.Button cancel = new System.Windows.Forms.Button();
                  cancel.Text = "Cancel";
                  cancel.Location = new System.Drawing.Point(200, 70);
                  cancel.DialogResult = DialogResult.Cancel;
      
                  System.Windows.Forms.TextBox sID = new System.Windows.Forms.TextBox();
                  sID.Location = new System.Drawing.Point(60, 10);
                  sID.Size = new System.Drawing.Size(215, 50);
      
                  System.Windows.Forms.TextBox sName = new System.Windows.Forms.TextBox();
                  sName.Location = new System.Drawing.Point(60, 40);
                  sName.Size = new System.Drawing.Size(215, 50);
      
                  System.Windows.Forms.Label lblID = new System.Windows.Forms.Label();
                  lblID.Font = new System.Drawing.Font(lblID.Font.Name, 12, lblID.Font.Style, lblID.Font.Unit);
                  lblID.Location = new System.Drawing.Point(5, 10);
                  lblID.Text = "ID:";
      
                  System.Windows.Forms.Label llblName = new System.Windows.Forms.Label();
                  llblName.Font = new System.Drawing.Font(llblName.Font.Name, 12, llblName.Font.Style, llblName.Font.Unit);
                  llblName.Location = new System.Drawing.Point(5, 40);
                  llblName.Text = "Name:";
                  #endregion
      
                  Form dialog = new Form();
                  dialog.Size = new System.Drawing.Size(300, 140);
                  dialog.MaximizeBox = false;
                  dialog.MinimizeBox = false;
                  dialog.FormBorderStyle = FormBorderStyle.FixedSingle;
                  dialog.StartPosition = FormStartPosition.CenterScreen;
                  dialog.Text = "Manual Student Entry";
                  dialog.AcceptButton = okay;
                  dialog.CancelButton = cancel;
                  dialog.Controls.Add(next);
                  dialog.Controls.Add(okay);
                  dialog.Controls.Add(cancel);
                  dialog.Controls.Add(sID);
                  dialog.Controls.Add(sName);
                  dialog.Controls.Add(lblID);
                  dialog.Controls.Add(llblName);
                  next.Click += next_Click;
                  sID.Leave += sID_Leave;
                  sName.Leave += sName_Leave;
                  dialog.ShowDialog();
      
                  if (dialog.DialogResult == DialogResult.OK)
                  {
                      if (sID.Text == "" | sName.Text == "")
                      {
                          MessageBox.Show("A student id and student name must be entered to be " +
                              "processed.", "Missing Input Error");
                          return;
                      }
                      else
                      {
                          int itIsnumeric = 0;
                          bool isNumeric = int.TryParse(sID.Text, out itIsnumeric);
      
                          if (isNumeric == true)
                          {
                              oXL = new Microsoft.Office.Interop.Excel.Application();
                              oXL.Visible = false;
                              oWK = oXL.Workbooks.Open(path + "\\" + fileName);
                              oWS = (Microsoft.Office.Interop.Excel.Worksheet)oWK.Worksheets[1];
                              oWS.Cells[i, 1] = sID.Text.ToString();
                              oWS.Cells[i, 2] = sName.Text.ToString();
                              i++;
                              oWK.Save();
                              oWK.Close();
                              oXL.Workbooks.Close();
                          }
                          else
                          {
                              MessageBox.Show("A student id cannot contain a non-numeric value.\r\n" +
                                  "Please make the ID correction.", "Student ID Error");
                          }
                      }
                  }
              }
      
              void sName_Leave(object sender, EventArgs e)
              {
                  System.Windows.Forms.TextBox txt = (System.Windows.Forms.TextBox)sender;
                  manualTextboxName = txt.Text;
              }
      
              void sID_Leave(object sender, EventArgs e)
              {
                  System.Windows.Forms.TextBox txt = (System.Windows.Forms.TextBox)sender;
                  manualTextboxID = txt.Text;
              }
              void next_Click(object sender, EventArgs e)
              {
                  if (manualTextboxID == null | manualTextboxName== null)
                  {
                      MessageBox.Show("A student id and student name must be entered to be " +
                          "processed.", "Missing Input Error");
                      return;
                  }
                  else
                  {
                      int itIsnumeric = 0;
                      bool isNumeric = int.TryParse(manualTextboxID.ToString(), out itIsnumeric);
      
                      if (isNumeric == true)
                      {
                          oXL = new Microsoft.Office.Interop.Excel.Application();
                          oXL.Visible = false;
                          oWK = oXL.Workbooks.Open(path + "\\" + fileName);
                          oWS = (Microsoft.Office.Interop.Excel.Worksheet)oWK.Worksheets[1];
                          oWS.Cells[i, 1] = manualTextboxID.ToString();
                          oWS.Cells[i, 2] = manualTextboxName.ToString();
                          i++;
                          oWK.Save();
                          oWK.Close();
                          oXL.Workbooks.Close();
                      }
                      else
                      {
                          MessageBox.Show("A student id cannot contain a non-numeric value.\r\n" +
                              "Please make the ID correction.", "Student ID Error");
                      }
                  }
      
              }

      Comment

      Working...