I created a program that when i click the button, all the Label text will be reset in Empty.

I have 31 labels, and 1 button..

When I click the button, i want that the output will be the labels are empty.

Hadi

32.1k 9 gold badges 54 silver badges 114 bronze badges

asked Apr 2 '17 at 9:35

5

  • I just dont know what the code to clear all text in 31 labels in just 1 click. :) THank you!!

    Apr 2 '17 at 9:36

  • Did you have other labels that you don't want to clear? Are you talking about WinForms, WPF, ASP.NET? Why do you tag this question with SQL?

    Apr 2 '17 at 9:47

  • Did you tried anything? Writing all labels by name and setting .Text = string.Empty? Or maybe you added all labels to the collection and you need only loop through them and reset text?

    Apr 2 '17 at 10:00

  • all done sir :) thank you!

    Apr 2 '17 at 12:53

  • @Im.Lean. if an answer solved your issue you have to accept it , else you should write ur own answer.

    Apr 2 '17 at 14:21

1 Answer 1

First method

Loop over label using this code:

                  For each Lbl as Label in Me.Controls.OfType(Of Label)()     Lbl.Text = ""  Next                                  

Second method

Or you have to list all labels and change .Text property

                  Label1.Text = "" Label2.Text = "" Label3.Text = "" ...                                  

answered Apr 2 '17 at 9:48

4

  • This will not compile with Option Strict On

    Apr 2 '17 at 10:01

  • @Fabio you're right, but Vb.net option strict is set to OFF by default. I added a note to my answer

    Apr 2 '17 at 10:19

  • You can simply use Form1.Controls.OfType(Of Label)() and inside loop lbl.Text = "" which satisfy both options of Option Strict :)

    Apr 2 '17 at 10:23

  • And make sure you are not using Option Strict On is somehow bad advise - because you want get full help from compiler. That's why you should advise to use Option Strict On

    Apr 2 '17 at 10:25

Not the answer you're looking for? Browse other questions tagged vb.net or ask your own question.