[C ×] - clear (judge) multiple or all TextBox text

Posted by lakilevi on Mon, 30 Dec 2019 16:19:33 +0100

Preface

                      . In the face of numerous text boxes, excellent program apes will not judge one by one. Program apes have the routine of program apes. Here are a few tips to use!

Clear all TextBox text boxes

Idea: these textboxes are all on one form. At this time, we can use a loop to traverse all TextBox controls on the form. (code as follows)

                    foreach (Control i in Controls)          //Clear all text boxes
                    {
                        if (i is TextBox)
                        {
                            i.Text = "";
                        }
                    }

Empty multiple TextBox text boxes (not all)

Idea: sometimes we don't need to operate all textboxes, but judge some textboxes. On the basis of the above ideas. We can put the text to be operated in a GroupBox control, and then traverse the TextBox control on this control. (code as follows)

	        foreach (Control i in groupBox1.Controls)               //Traverse TextBox controls on GroupBox controls
                    {
                        if (i is TextBox)
                        {
                            i.Text = "";
                        }
                    }

Judge whether all TextBox text boxes are empty

Idea: Based on the above idea, traverse the TextBox on the form. (code as follows)

	   foreach (Control cur in Controls)           //Judge whether the text is empty
            {
                if (cur is TextBox && cur.Text == string.Empty)             //If it is empty
                {                
                    MessageBox.Show("Please complete the information!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else                                    //If not empty
                {
                
                }
            }

Determine whether multiple (not all) textboxes are empty

Idea: Based on the above idea, traverse the TextBox control on GroupBox control. (code as follows)

	           foreach (Control i in groupBox3.Controls)            //Traverse all TextBox controls on GroupBox
                    {
                        if (i is TextBox)
                        {
                            i.Text = "";
                        }
                    }

summary

                     !