mario.deghetto

Visual Basic & .NET
posts - 332, comments - 79, trackbacks - 0

#29: Svuotare tutte le TextBox di un form

Per svuotare tutte le TextBox di un form, è possibile utilizzare questo codice:

Dim ctrl As New Form.ControlCollection(Me)
ctrl = System.Windows.Forms.Form.ActiveForm.Controls
For Each c As Control In ctrl
   If TypeOf (c) Is System.Windows.Forms.TextBox Then
      c.Text = ""
   End If
Next

Se vogliamo cancellare solo determinate TextBox possiamo impostare la proprietà “Tag” dei controlli TextBox interessati e poi di testare questa proprietà.
Se per esempio mettiamo “svuota” come proprietà Tag di alcuni controlli, il codice per svuotare solo le TextBox che hanno questa proprietà impostata a “svuota” è questo:

ctrl = CType(System.Windows.Forms.Form.ActiveForm.Controls, ControlCollection)
For Each c As Control In ctrl
   If TypeOf (c) Is System.Windows.Forms.TextBox Then
      Try
         If c.Tag.ToString = “svuota” Then
            c.Text = “”
         End If
      Catch ex As Exception
         ‘ non fa nulla
      End Try
   End If
Next

Ho inserito il test della proprietà Tag all’interno di un blocco Try … Catch per prevedere il caso in cui per una TextBox non fosse stata impostata la proprietà Tag (= valore nullo). Infatti in questo caso verrebbe sollevata un’eccezione.

Print | posted on venerdì 15 gennaio 2010 5.31 |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 4 and type the answer here:

Powered by: