Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
textbox can't be blank and there are 12 textbox and not even a single textbox can be blank so how can i show error message and show focus of cursor on that textbox.

i wrote that..

VB
Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeOf (ctl) Is TextBox Then
                If ctl.Text = "" Then

                    MsgBox("Please Enter the text ", MsgBoxStyle.Information, "Note")
                Else
                    ctl.Focus()
                End If
            End If
        Next



but this is not working...so help me plz
Posted
Updated 12-Oct-11 10:35am
v2

try this

VB
Dim ctl As Control
 For Each ctl In Me.Controls
 If TypeOf (ctl) Is TextBox Then
 If ctl.Text.Length = 0 Then

 MsgBox("Please Enter the text ", MsgBoxStyle.Information, "Note")
 Else
 ctl.Focus()
 End If
 End If
 Next
 
Share this answer
 
How about this:
VB
Dim ctl As Control
For Each ctl In Me.Controls
    If TypeOf (ctl) Is TextBox Then
        'In this next line, I added a .Trim to get rid of spaces and used theanil's method of using the .Length and comparing it to zero instead of checking for an empty string.
        If ctl.Text.Trim.Length = 0 Then
            MsgBox("Please Enter the text ", MsgBoxStyle.Information, "Note")
            'I removed the else because I figured you wanted to set focus when the box was empty
            ctl.Focus()
            'I added the Exit For because once you've found an empty box you want to stop your checking
            Exit For
        End If
    End If
Next
 
Share this answer
 
Comments
shweta89 13-Oct-11 10:01am    
still this is not working ....i m pasting my code ok
see whats d problem...this is cramer's rule to solve sytem of linear equation.


form1..

Public Class Form1
'to clear the element
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ctl As Control
For Each ctl In Me.Controls

If TypeOf (ctl) Is TextBox Then
ctl.Text = ""
lblres.Visible = False

p.Label10.Text = "" : p.Label11.Text = "" : p.Label12.Text = ""
px.Label10.Text = "" : px.Label11.Text = "" : px.Label12.Text = ""
py.Label10.Text = "" : py.Label11.Text = "" : py.Label12.Text = ""
pz.Label10.Text = "" : pz.Label11.Text = "" : pz.Label12.Text = ""


p.a11 = "0" : p.a12 = "0" : p.a13 = "0"
p.a21 = "0" : p.a22 = "0" : p.a23 = "0"
p.a31 = "0" : p.a32 = "0" : p.a33 = "0"

px.a11 = "0" : px.a12 = "0" : px.a13 = "0"
px.a21 = "0" : px.a22 = "0" : px.a23 = "0"
px.a31 = "0" : px.a32 = "0" : px.a33 = "0"

py.a11 = "0" : py.a12 = "0" : py.a13 = "0"
py.a21 = "0" : py.a22 = "0" : py.a23 = "0"
py.a31 = "0" : py.a32 = "0" : py.a33 = "0"

pz.a11 = "0" : pz.a12 = "0" : pz.a13 = "0"
pz.a21 = "0" : pz.a22 = "0" : pz.a23 = "0"
pz.a31 = "0" : pz.a32 = "0" : pz.a33 = "0"
End If
Next
End Sub

Private Sub btnsolve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsolve.Click
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf (ctl) Is TextBox Then
If ctl.Text.Length = 0 Then
' ErrorProvider1.SetError(ctl, "Please Enter the text")
MsgBox("Please Enter the text ", MsgBoxStyle.Information, "Note")
ctl.Focus()
Exit For
End If
End If
Next

Dim ctl2 As Control
For Each ctl2 In Me.Controls
If TypeOf (ctl2) Is TextBox Then
If Not ctl2.Text = "" Then

lblres.Visible = True
p.a11 = txtx1.Text : p.a12 = txty1.Text : p.a13 = txtz1.Text
p.a21 = txtx2.Text : p.a22 = txty2.Text : p.a23 = txtz2.Text
p.a31 = txtx3.Text : p.a32 = txty3.Text : p.a33 = txtz3.Text
p.calculate = True

px.a11 = txtres1.Text : px.a12 = txty1.Text : px.a13 = txtz1.Text
px.a21 = txtres2.Text : px.a22 = txty2.Text : px.a23 = txtz2.Text
px.a31 = txtres3.Text : px.a32 = txty3.Text : px.a33 = txtz3.Text
px.calculate = True

py.a11 = txtx1.Text : py.a12 = txtres1.Text : py.a13 = txtz1.Text
py.a21 = txtx2.Text : py.a22 = txtres2.Text : py.a23 = txtz2.Text
py.a31 = txtx3.Text : py.a32 = txtres3.Text : py.a33 = txtz3.Text
py.calculate = True

pz.a11 = txtx1.Text : pz.a12 = txty1.Text : pz.a13 = txtres1.Text
pz.a21 = txtx2.Text : pz.a22 = txty2.Text : pz.a23 = txtres2.Text
pz.a31 = txtx3.Text : pz.a32 = txty3.Text : pz.a33 = txtres3.Text
pz.calculate = True

'Result
lblres.Text = "x = " & px.results / p.results & " y = " & py.results / p.results & " z = " & pz.results / p.results

End If
End If
Next
End Sub
End Class


det...

Public Class det

Dim b As Boolean = False
Dim result As String

Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
Dim step2, step3, step4 As String


step2
Kschuler 13-Oct-11 10:03am    
Why don't you click Improve Question, and paste that code in your question where it can be formatted nicely...no one can read it like that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900