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. Checking required fields in a second Windows Form

Checking required fields in a second Windows Form

Scheduled Pinned Locked Moved C#
csharphelpquestion
38 Posts 5 Posters 9 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.
  • _ _Madmatt

    Maybe you can create your whole "Form2" by codig? That will be much easier ;) Form f = new Form(); f.Location = new Point(100, 100); f.Size = new Size(300, 300); f.Text = "Form 2"; And so on for each property. After that you'll need to check your textbox: if (TextBox.Text == null) { //Tell user he has to fill it. } if (TextBox.Text != null) { //Do something with the text. } Hope this helps!

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #4

    if (TextBox.Text == null)
    {
    //Tell user he has to fill it.
    }

    if (TextBox.Text != null)
    {
    //Do something with the text.
    }

    Wouldn't an if/else be more appropriate? Control validators eliminate this kind of clutter.

    _Madmatt wrote:

    Maybe you can create your whole "Form2" by codig

    And how does this solve the issue?

    _Madmatt wrote:

    That will be much easier

    Really? How so?


    only two letters away from being an asset

    _ 1 Reply Last reply
    0
    • N Not Active

      The validation should be done in the form that contains the fields and the form should not be dismissed until valid data has been entered

      Form2 qForm = new Form2();
      if( qForm.ShowDialog() == DialogResults.OK )
      {
      // Valid, now do something
      }


      only two letters away from being an asset

      J Offline
      J Offline
      JTRizos
      wrote on last edited by
      #5

      Thank you all for your responses. Here is the code that I have in Form1 public void ShowMyDialog() { Form2 qForm = new Form2(); qForm.ShowDialog(); if (qForm.ShowDialog ()== DialogResult.OK) { CaseNo = qForm.CaseNo; Reason = qForm.Reason; MessageBox.Show("Form2 Exists" + qForm.CaseNo + qForm.Reason); if (textBox1.Text == "" || textBox2.Text == "") { MessageBox.Show("Both Case No and Reason are required"); ShowMyDialog(); } } } Here's what is in Form2 public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string CaseNo { get { return textBox1.Text; } } public string Reason { get { return textBox2.Text; } } public void button1_Click(object sender, System.EventArgs e) { } } CaseNo and Reason flow back to Form1 but I am having a problem knowing how and where to check for the these two fields being present. These code sees if the fields are present but then just passes through. Mark, is this what you were suggesting? Thanx again!!!

      N 1 Reply Last reply
      0
      • J JTRizos

        Thank you all for your responses. Here is the code that I have in Form1 public void ShowMyDialog() { Form2 qForm = new Form2(); qForm.ShowDialog(); if (qForm.ShowDialog ()== DialogResult.OK) { CaseNo = qForm.CaseNo; Reason = qForm.Reason; MessageBox.Show("Form2 Exists" + qForm.CaseNo + qForm.Reason); if (textBox1.Text == "" || textBox2.Text == "") { MessageBox.Show("Both Case No and Reason are required"); ShowMyDialog(); } } } Here's what is in Form2 public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string CaseNo { get { return textBox1.Text; } } public string Reason { get { return textBox2.Text; } } public void button1_Click(object sender, System.EventArgs e) { } } CaseNo and Reason flow back to Form1 but I am having a problem knowing how and where to check for the these two fields being present. These code sees if the fields are present but then just passes through. Mark, is this what you were suggesting? Thanx again!!!

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #6

        Close but quite there yet. Either use RequiredField validators for the CaseNo and Reason textboxes on Form2 or validate them in the click event.

        public void button1_Click(object sender, System.EventArgs e)
        {
        if(string.IsNullOrEmpty(CaseNo) || string.IsNullOrEmpty(Reason) )
        {
        MessageBox(...);
        }
        else
        {
        DialogResult = DialogResult.OK;
        Close();
        }
        }

        Now Form2 will not be dismissed until the fields are valid. Also check to make sure button1 is not set as the default button for form2 or it will close the form automatically.


        only two letters away from being an asset

        J 1 Reply Last reply
        0
        • N Not Active

          Close but quite there yet. Either use RequiredField validators for the CaseNo and Reason textboxes on Form2 or validate them in the click event.

          public void button1_Click(object sender, System.EventArgs e)
          {
          if(string.IsNullOrEmpty(CaseNo) || string.IsNullOrEmpty(Reason) )
          {
          MessageBox(...);
          }
          else
          {
          DialogResult = DialogResult.OK;
          Close();
          }
          }

          Now Form2 will not be dismissed until the fields are valid. Also check to make sure button1 is not set as the default button for form2 or it will close the form automatically.


          only two letters away from being an asset

          J Offline
          J Offline
          JTRizos
          wrote on last edited by
          #7

          That helped a lot. It is much closer to what it should do. OK was set as default. Thank you Mark!! However, what would cause Form2 to be redisplayed after pressing OK or Cancel? Form2 pops up again and I have to press either OK or Cancel for it to continue. Thanx again for your help. It's these little gotchas that make this so exciting. ;P

          N 1 Reply Last reply
          0
          • J JTRizos

            That helped a lot. It is much closer to what it should do. OK was set as default. Thank you Mark!! However, what would cause Form2 to be redisplayed after pressing OK or Cancel? Form2 pops up again and I have to press either OK or Cancel for it to continue. Thanx again for your help. It's these little gotchas that make this so exciting. ;P

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #8

            JTRizos wrote:

            what would cause Form2 to be redisplayed after pressing OK or Cancel?

            Something in the code, it doesn't just happen. Are you calling the Show method again?


            only two letters away from being an asset

            J 1 Reply Last reply
            0
            • N Not Active

              JTRizos wrote:

              what would cause Form2 to be redisplayed after pressing OK or Cancel?

              Something in the code, it doesn't just happen. Are you calling the Show method again?


              only two letters away from being an asset

              J Offline
              J Offline
              JTRizos
              wrote on last edited by
              #9

              No, I don't but it seems I need to look closer at the code. The required field code you suggested in Form2 is processed twice (that's for submittals with an empty field) and then drops back to the next step in the code back in Form1 even if both fields are not filled. Cannot figure out why but it seems to want to process Form2 just twice or pop up Form2 again if the fields are filled. Thanx for your help. If you can think of something or anyone else who offered an answer earlier, I would be much appreciative. :~

              C 1 Reply Last reply
              0
              • N Not Active

                if (TextBox.Text == null)
                {
                //Tell user he has to fill it.
                }

                if (TextBox.Text != null)
                {
                //Do something with the text.
                }

                Wouldn't an if/else be more appropriate? Control validators eliminate this kind of clutter.

                _Madmatt wrote:

                Maybe you can create your whole "Form2" by codig

                And how does this solve the issue?

                _Madmatt wrote:

                That will be much easier

                Really? How so?


                only two letters away from being an asset

                _ Offline
                _ Offline
                _Madmatt
                wrote on last edited by
                #10

                Mark Nischalke wrote:

                Really? How so?

                Like I described ;) If Form1 creates Form2, Form1 can access Form2 and take inf out of it...

                N 1 Reply Last reply
                0
                • _ _Madmatt

                  Mark Nischalke wrote:

                  Really? How so?

                  Like I described ;) If Form1 creates Form2, Form1 can access Form2 and take inf out of it...

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #11

                  _Madmatt wrote:

                  Maybe you can create your whole "Form2" by codig? That will be much easier

                  How is "create your whole "Form2" by coding" easier?


                  only two letters away from being an asset

                  _ 1 Reply Last reply
                  0
                  • N Not Active

                    _Madmatt wrote:

                    Maybe you can create your whole "Form2" by codig? That will be much easier

                    How is "create your whole "Form2" by coding" easier?


                    only two letters away from being an asset

                    _ Offline
                    _ Offline
                    _Madmatt
                    wrote on last edited by
                    #12

                    Are you going to tell me you don't know how to make a control in the codebehind?

                    N 2 Replies Last reply
                    0
                    • _ _Madmatt

                      Are you going to tell me you don't know how to make a control in the codebehind?

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #13

                      As you are having difficulties understanding, I'll try to make this very clear, again. You stated it was easier to create the form in code. My question, once again, is to have you explain why you think this is easier?

                      _Madmatt wrote:

                      Are you going to tell me you don't know how to make a control in the codebehind?

                      Take a better look around before making statements like this.


                      only two letters away from being an asset

                      J _ 2 Replies Last reply
                      0
                      • N Not Active

                        As you are having difficulties understanding, I'll try to make this very clear, again. You stated it was easier to create the form in code. My question, once again, is to have you explain why you think this is easier?

                        _Madmatt wrote:

                        Are you going to tell me you don't know how to make a control in the codebehind?

                        Take a better look around before making statements like this.


                        only two letters away from being an asset

                        J Offline
                        J Offline
                        JTRizos
                        wrote on last edited by
                        #14

                        I am guessing this recent interchange of messages must be part of another thread. But since you've responded in this one. I still have an issue that I cannot for the life of me figure out. Your input has been helpful but I cannot get around the problem of the code in Form2 checking for empty textboxes twice and then falling through to the rest of the code in Form1. I run Debug and cannot find the cause. A similar If/Else in Form1 returns to Form1 focused on field 1 if the conditions find that a required field is empty. It continues to do this until I fill all the required fields or hit Exit. Similar code in Form2 only checks twice, finds/reports the correct empty textboxes and then drops down back to Form1 regardless whether the textboxes are empty or not. The returns in Form2 do not seem to affect anything. Here's some of the code: Form1.cs --------

                                {
                                MessageBox.Show("The Last Name and First Name are required!!");
                                return;
                                }
                                //  Above code handles Form1 textboxes
                        
                        
                                //Call Form2 for CaseNo and Reason data
                                GetForm2();                           
                                ...
                                ... 
                        
                          // Opens second Diaolog form (Form2) to get Case Number and Reason
                            public void GetForm2()
                            {
                                Form2 qForm = new Form2();
                                qForm.ShowDialog();
                        
                                if (qForm.ShowDialog()== DialogResult.OK)
                                {
                                    CaseNo = qForm.CaseNo;
                                    Reason = qForm.Reason;
                                    MessageBox.Show("Form2 Exists" + " " + CaseNo + "  " + Reason);
                                }
                                qForm.Dispose();
                            }
                        

                        Form2.cs --------

                        using System;
                        using System.Collections.Generic;
                        using System.ComponentModel;
                        using System.Data;
                        using System.Drawing;
                        using System.Text;
                        using System.Windows.Forms;

                        namespace DMV_Test_1
                        {
                        public partial class Form2 : Form
                        {
                        public Form2()
                        {
                        InitializeComponent();
                        }

                            public string CaseNo
                            {
                                get { return textBox1.Text; }
                            }
                            public string Reason
                            {
                                get { return textBox2.Text; }
                            }
                            
                        
                            private void button1\_Click(object sender, System.EventArgs e)
                            {
                                if (CaseNo == "")
                                {
                                    MessageBox.Show("The Case No is required!!");
                                    this.textBox1.Focus();
                                    return;
                                }
                        
                        N 1 Reply Last reply
                        0
                        • J JTRizos

                          I am guessing this recent interchange of messages must be part of another thread. But since you've responded in this one. I still have an issue that I cannot for the life of me figure out. Your input has been helpful but I cannot get around the problem of the code in Form2 checking for empty textboxes twice and then falling through to the rest of the code in Form1. I run Debug and cannot find the cause. A similar If/Else in Form1 returns to Form1 focused on field 1 if the conditions find that a required field is empty. It continues to do this until I fill all the required fields or hit Exit. Similar code in Form2 only checks twice, finds/reports the correct empty textboxes and then drops down back to Form1 regardless whether the textboxes are empty or not. The returns in Form2 do not seem to affect anything. Here's some of the code: Form1.cs --------

                                  {
                                  MessageBox.Show("The Last Name and First Name are required!!");
                                  return;
                                  }
                                  //  Above code handles Form1 textboxes
                          
                          
                                  //Call Form2 for CaseNo and Reason data
                                  GetForm2();                           
                                  ...
                                  ... 
                          
                            // Opens second Diaolog form (Form2) to get Case Number and Reason
                              public void GetForm2()
                              {
                                  Form2 qForm = new Form2();
                                  qForm.ShowDialog();
                          
                                  if (qForm.ShowDialog()== DialogResult.OK)
                                  {
                                      CaseNo = qForm.CaseNo;
                                      Reason = qForm.Reason;
                                      MessageBox.Show("Form2 Exists" + " " + CaseNo + "  " + Reason);
                                  }
                                  qForm.Dispose();
                              }
                          

                          Form2.cs --------

                          using System;
                          using System.Collections.Generic;
                          using System.ComponentModel;
                          using System.Data;
                          using System.Drawing;
                          using System.Text;
                          using System.Windows.Forms;

                          namespace DMV_Test_1
                          {
                          public partial class Form2 : Form
                          {
                          public Form2()
                          {
                          InitializeComponent();
                          }

                              public string CaseNo
                              {
                                  get { return textBox1.Text; }
                              }
                              public string Reason
                              {
                                  get { return textBox2.Text; }
                              }
                              
                          
                              private void button1\_Click(object sender, System.EventArgs e)
                              {
                                  if (CaseNo == "")
                                  {
                                      MessageBox.Show("The Case No is required!!");
                                      this.textBox1.Focus();
                                      return;
                                  }
                          
                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #15

                          The application is behaving exactly as planned. You are calling ShowDialog twice.

                          public void GetForm2()
                          {
                          Form2 qForm = new Form2();
                          qForm.ShowDialog(); <---- Form2 opened. Not needed

                          if (qForm.ShowDialog()== DialogResult.OK) <------ Form 2 opened again
                          {
                          CaseNo = qForm.CaseNo;
                          Reason = qForm.Reason;
                          MessageBox.Show("Form2 Exists" + " " + CaseNo + " " + Reason);
                          }
                          qForm.Dispose(); <----- Not needed
                          }


                          only two letters away from being an asset

                          J 1 Reply Last reply
                          0
                          • _ _Madmatt

                            Are you going to tell me you don't know how to make a control in the codebehind?

                            N Offline
                            N Offline
                            Not Active
                            wrote on last edited by
                            #16

                            I'm still waiting for your explanation.


                            only two letters away from being an asset

                            _ 1 Reply Last reply
                            0
                            • N Not Active

                              The application is behaving exactly as planned. You are calling ShowDialog twice.

                              public void GetForm2()
                              {
                              Form2 qForm = new Form2();
                              qForm.ShowDialog(); <---- Form2 opened. Not needed

                              if (qForm.ShowDialog()== DialogResult.OK) <------ Form 2 opened again
                              {
                              CaseNo = qForm.CaseNo;
                              Reason = qForm.Reason;
                              MessageBox.Show("Form2 Exists" + " " + CaseNo + " " + Reason);
                              }
                              qForm.Dispose(); <----- Not needed
                              }


                              only two letters away from being an asset

                              J Offline
                              J Offline
                              JTRizos
                              wrote on last edited by
                              #17

                              Thank you ... that handled Form2 popping up twice and I learned something. I thought the first one opened the form and the second was just a check. However, the check for an empty textbox now just drops through even if a textbox is empty and the message appears. Am I missing something on how Form2 is processed versus how Form1 is processed? Maybe which form is the primary form? I would expect for the process to stay within the if/else until the textboxes are no longer empty. That is what similar code in Form1 does. Almost there. Thanx again

                              N 1 Reply Last reply
                              0
                              • N Not Active

                                As you are having difficulties understanding, I'll try to make this very clear, again. You stated it was easier to create the form in code. My question, once again, is to have you explain why you think this is easier?

                                _Madmatt wrote:

                                Are you going to tell me you don't know how to make a control in the codebehind?

                                Take a better look around before making statements like this.


                                only two letters away from being an asset

                                _ Offline
                                _ Offline
                                _Madmatt
                                wrote on last edited by
                                #18

                                Mark Nischalke wrote:

                                As you are having difficulties understanding, I'll try to make this very clear, again.

                                I'm so sorry, my English isn't that good. If Form1 creates Form2, Form2 can check its own textboxes. When those boxes are empty, you just display a message. If the textboxes are valid, Form2 closes itself and Form1 can still acces the textbox.Text properties. You may not Dispose Form2 for that ;) I'm not yet studying c# so you may conclude I'm beginner :)

                                J N 2 Replies Last reply
                                0
                                • N Not Active

                                  I'm still waiting for your explanation.


                                  only two letters away from being an asset

                                  _ Offline
                                  _ Offline
                                  _Madmatt
                                  wrote on last edited by
                                  #19

                                  I'm not online all day long ;)

                                  L 1 Reply Last reply
                                  0
                                  • _ _Madmatt

                                    Mark Nischalke wrote:

                                    As you are having difficulties understanding, I'll try to make this very clear, again.

                                    I'm so sorry, my English isn't that good. If Form1 creates Form2, Form2 can check its own textboxes. When those boxes are empty, you just display a message. If the textboxes are valid, Form2 closes itself and Form1 can still acces the textbox.Text properties. You may not Dispose Form2 for that ;) I'm not yet studying c# so you may conclude I'm beginner :)

                                    J Offline
                                    J Offline
                                    JTRizos
                                    wrote on last edited by
                                    #20

                                    Thanx _Madmatt, your English is good, no problem. I understand what you are saying but am confused on the HOW. I am also new to C# and am getting used to the gotchas. I am sure the problem is with how the Dialogresult is handled, just not clear on how I need to handle it. Thanx again for your suggestions.

                                    _ 1 Reply Last reply
                                    0
                                    • J JTRizos

                                      Thanx _Madmatt, your English is good, no problem. I understand what you are saying but am confused on the HOW. I am also new to C# and am getting used to the gotchas. I am sure the problem is with how the Dialogresult is handled, just not clear on how I need to handle it. Thanx again for your suggestions.

                                      _ Offline
                                      _ Offline
                                      _Madmatt
                                      wrote on last edited by
                                      #21

                                      You're welcome! Maybe you can show your Form2 just in a form and not in a Dialog. Then you just check if your textboxes are empty like this: if (txt1.Text == null) { // TextBox1 is empty } else if(txt1.Text != null) { // TextBox is not empty do something with the text ;) }

                                      1 Reply Last reply
                                      0
                                      • J JTRizos

                                        Thank you ... that handled Form2 popping up twice and I learned something. I thought the first one opened the form and the second was just a check. However, the check for an empty textbox now just drops through even if a textbox is empty and the message appears. Am I missing something on how Form2 is processed versus how Form1 is processed? Maybe which form is the primary form? I would expect for the process to stay within the if/else until the textboxes are no longer empty. That is what similar code in Form1 does. Almost there. Thanx again

                                        N Offline
                                        N Offline
                                        Not Active
                                        wrote on last edited by
                                        #22

                                        I would suggest you maybe email me what you have. That way I can take a look at the whole problem and maybe be better able to help you at this point.


                                        only two letters away from being an asset

                                        J 1 Reply Last reply
                                        0
                                        • _ _Madmatt

                                          Mark Nischalke wrote:

                                          As you are having difficulties understanding, I'll try to make this very clear, again.

                                          I'm so sorry, my English isn't that good. If Form1 creates Form2, Form2 can check its own textboxes. When those boxes are empty, you just display a message. If the textboxes are valid, Form2 closes itself and Form1 can still acces the textbox.Text properties. You may not Dispose Form2 for that ;) I'm not yet studying c# so you may conclude I'm beginner :)

                                          N Offline
                                          N Offline
                                          Not Active
                                          wrote on last edited by
                                          #23

                                          _Madmatt wrote:

                                          I'm so sorry, my English isn't that good.

                                          I don't buy this excuse You still have not answered the question, why is doing it in code better? But no matter, not worth continuing based on below.

                                          _Madmatt wrote:

                                          I'm not yet studying c#

                                          Then you absolutely should not have answered in the first place :mad::mad:


                                          only two letters away from being an asset

                                          _ 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