How to check if all Textbox is empty in on click and input thier errorProvider msg?












1















I want to check if any textbox is empty on btn click and then display their corresponding SetError msg on their side



bool isIncomplete = false;
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox tb = control as TextBox;
if (string.IsNullOrWhiteSpace(tb.Text))
{
isIncomplete = true;
break;
}
}
} // I think this.controls does not work properly..

if (isIncomplete)
{
errorProvider1.SetError(firstname_txtbox, "First Name is required.");
errorProvider2.SetError(lastname_txtbox, "Last Name is required.");

MessageBox.Show("Please fill all the textbox correctly!");
return;

} else if(firstname_txtbox.Text.Length < 2)
{
errorProvider1.SetError(firstname_txtbox, "First Name need to be at least 2 characters"); //this error message does appear through...
}else if() { etc..


The errorProvider message arent being dispalyed on click.
My textboxes are inside a Panel...










share|improve this question




















  • 1





    you need to iterate through the controls of the panel and not of the form. One shortcut: this.panel1.Controls.OfType<TextBox>() will return only the textboxes

    – Mong Zhu
    Nov 27 '18 at 9:26











  • thanks, i think its working

    – Emanula Sohn
    Nov 27 '18 at 9:41











  • stackoverflow.com/a/33080822/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:44











  • stackoverflow.com/a/46129052/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:45
















1















I want to check if any textbox is empty on btn click and then display their corresponding SetError msg on their side



bool isIncomplete = false;
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox tb = control as TextBox;
if (string.IsNullOrWhiteSpace(tb.Text))
{
isIncomplete = true;
break;
}
}
} // I think this.controls does not work properly..

if (isIncomplete)
{
errorProvider1.SetError(firstname_txtbox, "First Name is required.");
errorProvider2.SetError(lastname_txtbox, "Last Name is required.");

MessageBox.Show("Please fill all the textbox correctly!");
return;

} else if(firstname_txtbox.Text.Length < 2)
{
errorProvider1.SetError(firstname_txtbox, "First Name need to be at least 2 characters"); //this error message does appear through...
}else if() { etc..


The errorProvider message arent being dispalyed on click.
My textboxes are inside a Panel...










share|improve this question




















  • 1





    you need to iterate through the controls of the panel and not of the form. One shortcut: this.panel1.Controls.OfType<TextBox>() will return only the textboxes

    – Mong Zhu
    Nov 27 '18 at 9:26











  • thanks, i think its working

    – Emanula Sohn
    Nov 27 '18 at 9:41











  • stackoverflow.com/a/33080822/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:44











  • stackoverflow.com/a/46129052/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:45














1












1








1








I want to check if any textbox is empty on btn click and then display their corresponding SetError msg on their side



bool isIncomplete = false;
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox tb = control as TextBox;
if (string.IsNullOrWhiteSpace(tb.Text))
{
isIncomplete = true;
break;
}
}
} // I think this.controls does not work properly..

if (isIncomplete)
{
errorProvider1.SetError(firstname_txtbox, "First Name is required.");
errorProvider2.SetError(lastname_txtbox, "Last Name is required.");

MessageBox.Show("Please fill all the textbox correctly!");
return;

} else if(firstname_txtbox.Text.Length < 2)
{
errorProvider1.SetError(firstname_txtbox, "First Name need to be at least 2 characters"); //this error message does appear through...
}else if() { etc..


The errorProvider message arent being dispalyed on click.
My textboxes are inside a Panel...










share|improve this question
















I want to check if any textbox is empty on btn click and then display their corresponding SetError msg on their side



bool isIncomplete = false;
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox tb = control as TextBox;
if (string.IsNullOrWhiteSpace(tb.Text))
{
isIncomplete = true;
break;
}
}
} // I think this.controls does not work properly..

if (isIncomplete)
{
errorProvider1.SetError(firstname_txtbox, "First Name is required.");
errorProvider2.SetError(lastname_txtbox, "Last Name is required.");

MessageBox.Show("Please fill all the textbox correctly!");
return;

} else if(firstname_txtbox.Text.Length < 2)
{
errorProvider1.SetError(firstname_txtbox, "First Name need to be at least 2 characters"); //this error message does appear through...
}else if() { etc..


The errorProvider message arent being dispalyed on click.
My textboxes are inside a Panel...







c# winforms textbox errorprovider






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 9:28









Mong Zhu

15.3k52348




15.3k52348










asked Nov 27 '18 at 9:00









Emanula SohnEmanula Sohn

275




275








  • 1





    you need to iterate through the controls of the panel and not of the form. One shortcut: this.panel1.Controls.OfType<TextBox>() will return only the textboxes

    – Mong Zhu
    Nov 27 '18 at 9:26











  • thanks, i think its working

    – Emanula Sohn
    Nov 27 '18 at 9:41











  • stackoverflow.com/a/33080822/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:44











  • stackoverflow.com/a/46129052/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:45














  • 1





    you need to iterate through the controls of the panel and not of the form. One shortcut: this.panel1.Controls.OfType<TextBox>() will return only the textboxes

    – Mong Zhu
    Nov 27 '18 at 9:26











  • thanks, i think its working

    – Emanula Sohn
    Nov 27 '18 at 9:41











  • stackoverflow.com/a/33080822/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:44











  • stackoverflow.com/a/46129052/3110834

    – Reza Aghaei
    Nov 27 '18 at 9:45








1




1





you need to iterate through the controls of the panel and not of the form. One shortcut: this.panel1.Controls.OfType<TextBox>() will return only the textboxes

– Mong Zhu
Nov 27 '18 at 9:26





you need to iterate through the controls of the panel and not of the form. One shortcut: this.panel1.Controls.OfType<TextBox>() will return only the textboxes

– Mong Zhu
Nov 27 '18 at 9:26













thanks, i think its working

– Emanula Sohn
Nov 27 '18 at 9:41





thanks, i think its working

– Emanula Sohn
Nov 27 '18 at 9:41













stackoverflow.com/a/33080822/3110834

– Reza Aghaei
Nov 27 '18 at 9:44





stackoverflow.com/a/33080822/3110834

– Reza Aghaei
Nov 27 '18 at 9:44













stackoverflow.com/a/46129052/3110834

– Reza Aghaei
Nov 27 '18 at 9:45





stackoverflow.com/a/46129052/3110834

– Reza Aghaei
Nov 27 '18 at 9:45












1 Answer
1






active

oldest

votes


















0














use String.IsNullOrEmpty(String)
to check if a control is empty.



private void button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Please fill all the textbox correctly!");
}
else
{
MessageBox.Show("Not empty");
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495959%2fhow-to-check-if-all-textbox-is-empty-in-on-click-and-input-thier-errorprovider-m%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    use String.IsNullOrEmpty(String)
    to check if a control is empty.



    private void button1_Click(object sender, EventArgs e)
    {
    if (String.IsNullOrEmpty(textBox1.Text))
    {
    MessageBox.Show("Please fill all the textbox correctly!");
    }
    else
    {
    MessageBox.Show("Not empty");
    }
    }





    share|improve this answer




























      0














      use String.IsNullOrEmpty(String)
      to check if a control is empty.



      private void button1_Click(object sender, EventArgs e)
      {
      if (String.IsNullOrEmpty(textBox1.Text))
      {
      MessageBox.Show("Please fill all the textbox correctly!");
      }
      else
      {
      MessageBox.Show("Not empty");
      }
      }





      share|improve this answer


























        0












        0








        0







        use String.IsNullOrEmpty(String)
        to check if a control is empty.



        private void button1_Click(object sender, EventArgs e)
        {
        if (String.IsNullOrEmpty(textBox1.Text))
        {
        MessageBox.Show("Please fill all the textbox correctly!");
        }
        else
        {
        MessageBox.Show("Not empty");
        }
        }





        share|improve this answer













        use String.IsNullOrEmpty(String)
        to check if a control is empty.



        private void button1_Click(object sender, EventArgs e)
        {
        if (String.IsNullOrEmpty(textBox1.Text))
        {
        MessageBox.Show("Please fill all the textbox correctly!");
        }
        else
        {
        MessageBox.Show("Not empty");
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 13:02









        JosephJoseph

        14




        14
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495959%2fhow-to-check-if-all-textbox-is-empty-in-on-click-and-input-thier-errorprovider-m%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)