Siccome il giovanotto dice:
My problem is that sometimes there are textboxes that cannot be left blank and there is a final button on the form to record that when it goes to record everything fails because of this damn blank textbox.
Allora, senza aver approfondito troppo il discorso, propongo questo:
Private $aTextBoxNames As New String[]
Private $hScroll As ScrollView
Public Sub Form_Open()
Dim hTextBox As TextBox
Dim hPan As HBox
Dim hSprig As Spring
Dim hButton As Button
With Me
.Arrangement = Arrange.Vertical
.W = 300
.H = 496
.Margin = True
End With
With $hScroll = New ScrollView(Me)
.Expand = True
.Arrangement = Arrange.Vertical
.ScrollBar = Scroll.Vertical
End With
For i As Byte = 1 To 20
With hTextBox = New TextBox($hScroll)
.H = 32
.Name = "TextBox" & i
.Text = "A correct value"
End With
If i Mod 2 = 0 Then
' Assumes as containing required values
$aTextBoxNames.Push("TextBox" & i)
Endif
If i Mod 4 = 0 Then
' Clear some (required) value
hTextBox.Clear
Endif
Next
With hPan = New HBox(Me)
.H = 32
End With
hSprig = New Spring(hPan)
With hButton = New Button(hPan) As "btnValidate"
.Text = "Validate value"
.AutoResize = True
End With
End
Public Sub btnValidate_Click()
If AllSet() Then Print "ALL VALIDATE"
End
Private Sub AllSet() As Boolean
Dim hControl As Control
Dim hObj As Object
For Each hControl In Me.Controls
If hControl.Name Like "TextBox*" Then
hObj = Me[hControl.Name]
If hObj.Text = "" Then
If $aTextBoxNames.Find(hControl.Name) >= 0 Then
Print hControl.Name
$hScroll.Scroll(hControl.X, hControl.Y)
Balloon.Info("Absent data", hControl)
hControl.SetFocus
Return False
Endif
Endif
Endif
Next
Return True
End
Perché secondo me devi agire solo là dove il valore va scritto
:ciao:
Se uno volesse davvero usare questo codice di convalida, gli suggerisco di aggiungere un Trim qui:
If Trim(hObj.Text) = "" Then
:ciao:
Ieri avevo poco tempo e qualcuno di voi può aver pensato che il codice proposto non è molto efficiente e comunque esula dalla richiesta di aiuto.
Si potrebbe essere vero, ad esempio in questo caso che icontrolli sono tutti in una scrollview il codice poteva essere scritto anche così (vedi sotto) e anche in altri modi.
Ma io comunque rispondevo alla frase quotata dove si parla di una finestra già finita e pertanto non è detto che tutti i controlli da verificare si trovino nello stesso contenitore ho proposto un codice semplice da capire ed efficace in qualunque situazione (almeno credo).
Sempre partendo dalla frase quotata, la richiesta di aiuto risulta inefficace per il problema, pertanto credo occorra dare una risposta efficace e sensata, ma forse qui sono in errore :-\
Private Sub AllSet() As Boolean
Dim hObj As Object
For Each hObj In $hScroll.Children
If Object.Type(hObj) = "TextBox" And If Trim(hObj.Text) = "" Then
Print hObj.Name
If $aTextBoxNames.Find(hObj.Name) >= 0 Then
$hScroll.Scroll(hObj.X, hObj.Y)
Balloon.Info("Absent data", hObj)
hObj.SetFocus
Return False
Endif
Endif
Next
Return True
End
:ciao: