Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A simple Form OK button question

A simple Form OK button question

Scheduled Pinned Locked Moved C#
helpc++question
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DionChen
    wrote on last edited by
    #1

    Hi, In my WinForm App, I have to show another form2. but I do have to know if the user click on OK or Cancel. therefore I assigned DialogResult property of OK and Cancel button on Form2 to OK and Cancel, respectively. It works fine and I can use Dialogresult to figure out whether the OK or Cancel button is clicked to close the Form2. The problem comes in when I add a errorProvider1 to form2, I use it to make sure a couple of textBoxes are not empty and then SetError on ErrorProvider. Because the OK button has DialogResult.OK property set, the dialog closes before I got chance to view the error. Is there anyway to prevent the dialog form close if I have any errors. It is so easy to do in old MFC days. :-(. Dion

    G L 2 Replies Last reply
    0
    • D DionChen

      Hi, In my WinForm App, I have to show another form2. but I do have to know if the user click on OK or Cancel. therefore I assigned DialogResult property of OK and Cancel button on Form2 to OK and Cancel, respectively. It works fine and I can use Dialogresult to figure out whether the OK or Cancel button is clicked to close the Form2. The problem comes in when I add a errorProvider1 to form2, I use it to make sure a couple of textBoxes are not empty and then SetError on ErrorProvider. Because the OK button has DialogResult.OK property set, the dialog closes before I got chance to view the error. Is there anyway to prevent the dialog form close if I have any errors. It is so easy to do in old MFC days. :-(. Dion

      G Offline
      G Offline
      Gogou
      wrote on last edited by
      #2

      Why not just catch the yourbutton.OnClick event? private void button1_Click(object sender, System.EventArgs e) { if(textBox1.Text.Length == 0 || textBox2.Text.Length == 0)//etc. { //Your error handling code MessageBox.Show("Please, enter some text"); return; } this.Close(); } Hope being helpful Cheers, Gogou GAtanasov

      D 1 Reply Last reply
      0
      • G Gogou

        Why not just catch the yourbutton.OnClick event? private void button1_Click(object sender, System.EventArgs e) { if(textBox1.Text.Length == 0 || textBox2.Text.Length == 0)//etc. { //Your error handling code MessageBox.Show("Please, enter some text"); return; } this.Close(); } Hope being helpful Cheers, Gogou GAtanasov

        D Offline
        D Offline
        DionChen
        wrote on last edited by
        #3

        Thanks for your help. I definitely can do my validation on btnOK_click event, but how do I know from my main form whether an user clicked on OK button or Cancel button if I do not set the DialogResult property. But if I set DialogResult property, the OK button closes the form before I can see the validation errors. I'd like to achieve BOTH: 1. TextBox Validation when OK is clicked, if there is error, do not close the form. 2. If there is no error, the Form2 is closed. From the caller, I want to know whether an user click on OK or Cancel. How do I achieve this? Dion

        J Z 2 Replies Last reply
        0
        • D DionChen

          Thanks for your help. I definitely can do my validation on btnOK_click event, but how do I know from my main form whether an user clicked on OK button or Cancel button if I do not set the DialogResult property. But if I set DialogResult property, the OK button closes the form before I can see the validation errors. I'd like to achieve BOTH: 1. TextBox Validation when OK is clicked, if there is error, do not close the form. 2. If there is no error, the Form2 is closed. From the caller, I want to know whether an user click on OK or Cancel. How do I achieve this? Dion

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          DionChen wrote: TextBox Validation when OK is clicked Handle the Validating and Validated events on your textboxes. If you look up the Control.Validating event in MSDN you'll see an example using the events with the ErrorProvider class. Just remember to set the CausesValidation property to false on at least the cancel button, or you can't click it without providing valid data. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

          1 Reply Last reply
          0
          • D DionChen

            Thanks for your help. I definitely can do my validation on btnOK_click event, but how do I know from my main form whether an user clicked on OK button or Cancel button if I do not set the DialogResult property. But if I set DialogResult property, the OK button closes the form before I can see the validation errors. I'd like to achieve BOTH: 1. TextBox Validation when OK is clicked, if there is error, do not close the form. 2. If there is no error, the Form2 is closed. From the caller, I want to know whether an user click on OK or Cancel. How do I achieve this? Dion

            Z Offline
            Z Offline
            Zeke Le
            wrote on last edited by
            #5

            You can achieve this by setting the form2 DialogResult to None. EX: if (txtName == "") { //Show error message this.DialogResult = DialogResult.None }  No time to breathe

            1 Reply Last reply
            0
            • D DionChen

              Hi, In my WinForm App, I have to show another form2. but I do have to know if the user click on OK or Cancel. therefore I assigned DialogResult property of OK and Cancel button on Form2 to OK and Cancel, respectively. It works fine and I can use Dialogresult to figure out whether the OK or Cancel button is clicked to close the Form2. The problem comes in when I add a errorProvider1 to form2, I use it to make sure a couple of textBoxes are not empty and then SetError on ErrorProvider. Because the OK button has DialogResult.OK property set, the dialog closes before I got chance to view the error. Is there anyway to prevent the dialog form close if I have any errors. It is so easy to do in old MFC days. :-(. Dion

              L Offline
              L Offline
              LongRange Shooter
              wrote on last edited by
              #6

              In the parent form: GetMyData diagInfo = new GetMyData(); if (diagInfo.ShowDialog() == DialogResult.Cancel) { cancel logic } else { doit } The dialog form: public GeMyData { } public string GiveMeInfoOne { get { return info1.Text; } } public string GiveMeInfoTwo { get { return info2.Text; } } private void button1_Click(Object sender, EventArgs e) { if (info1.Text == "" or info2.Text == "") { MessageBox.Show("You really must enter data in my text boxes or I will never go away and I will haunt you the rest of your days"); } else { this.DialogResult = DialogResult.OK; this.Close(); return; } } Voila -- your dialog never closes until you have a good bit of data in each box and you actually execute a return. :cool: _____________________________________________ The world is a dangerous place.
              Not because of those that do evil,
                  but because of those who look on and do nothing.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups