Jump to content

Visual Basic

natan.p

Hello i have little code that checks if "textbox1/2/3/4/5.text" is empty, so make a test and run the program, i leave the text boxes empty that show me the "MsgBox" but is keep to the next action/script. how to block that?

 

I want it like this: User most fill the text boxes to keep to next step, if the boxes not fillen so he stilll on this step.

 

The code:

Private Sub Button1_Clivk(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickIf TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" ThenMsgBox("Most fill the form to connect.", MsgBoxStyle.OkOnly, "ERROR")End IfMsgBox("Connected")End Sub
Link to comment
Share on other sites

Link to post
Share on other sites

You might want to put more effort in your english, bad english makes your question harder to answer, nonetheless I shall attempt.

what I would do instead:

for i = 1 to 6 step 1    if(me.controls("Textbox" & i).text = string.empty )        msgbox("you still need to enter dat in textbox" & i)    end ifnext

my VB.NET is a little rusty, but basically my code checks each textbox for content, if at least one of your textboxes contains nothing, it will message it to the user.
I might not have written it 100% correctly, I don't really have time right now, lemme know if it works.

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

You might want to put more effort in your english, bad english makes your question harder to answer, nonetheless I shall attempt.

what I would do instead:

for i = 1 to 6 step 1    if(me.controls("Textbox" & i).text = string.empty )        msgbox("you still need to enter dat in textbox" & i)    end ifnext

my VB.NET is a little rusty, but basically my code checks each textbox for content, if at least one of your textboxes contains nothing, it will message it to the user.

I might not have written it 100% correctly, I don't really have time right now, lemme know if it works.

Thanks you and sorry for my bad english, "for i = 1 to 6 step 1" change the six to my last textbox number(5) and paste it like this?

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks you and sorry for my bad english, "for i = 1 to 6 step 1" change the six to my last textbox number(5) and paste it like this?

oh yeah, if your Textboxes go form TextBox1 to TextBox5 then you should change to loop from 1 to 5.

I'm noticing you're a beginner so I'll rewrite the whole funtion for you like you had it in the original question.

what that makes:

Private Sub Button1_Clivk(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clickfor i = 1 to 5 step 1If(me.controls("Textbox" & i).text = string.empty ) Thenmsgbox("Must fill the form to connect.", MsgBoxStyle.OkOnly, "ERROR")End IfnextMsgBox("Connected")End Sub

However, this code will message everythime 1 textbox is empty(so 2 empty textboxes will generate 2 messages)

to fix this, try:

 

Private Sub Button1_Clivk(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clickdim EmptyBox as boolean = falsefor i = 1 to 5 step 1If(me.controls("Textbox" & i).text = string.empty ) ThenEmptyBox = trueEnd IfnextIf(EmptyBox = true) Thenmsgbox("Must fill the form to connect.", MsgBoxStyle.OkOnly, "ERROR")elseMsgBox("Connected")End IFEnd Sub

Happy Coding!  :P

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

That still keep to the next message, how to block that

Link to comment
Share on other sites

Link to post
Share on other sites

@natan.p All you had to do was add a 'Return' statement to his code to stop it from running the rest of the method

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        For i = 1 To 5 Step 1            If (Me.Controls("Textbox" & i).Text = String.Empty) Then                MsgBox("Must fill the form to connect.", MsgBoxStyle.OkOnly, "ERROR")                Return            End If        Next        MsgBox("Connected")End Sub

OR your original code with an 'Else' added

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Then            MsgBox("Most fill the form to connect.", MsgBoxStyle.OkOnly, "ERROR")        Else            MsgBox("Connected")        End If    End Sub

Read this

http://msdn.microsoft.com/en-us/library/752y8abs.aspx

 



Link to comment
Share on other sites

Link to post
Share on other sites

Alternatively you could also use a do-until loop with a boolean variable to regualte input, with a nested for loop handling the rest.

Folding@Home | BOINC

CPU: AMD Ryzen 9 3900X + Celsius S36 GPU: ASUS TUF RTX3080 

MB: ASRock x470 Taichi RAM: Corsair Vengeance RGB DDR4-2666 32GB 

CASE: Fractal Design Define 7 Panda STORAGE: WD Black SN770 2TB + WD Red Pro 6TB

Link to comment
Share on other sites

Link to post
Share on other sites

i have no idea why everyone suggest loops, if you don't want to make things overly complicated you can just keep you code, it was fine

the only change you should do is to put the MsgBox in the "else" block , so that the program only executes it if the tested conditon is false

Private Sub Button1_Clivk(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click          If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Then                    MsgBox("Most fill the form to connect.", MsgBoxStyle.OkOnly, "ERROR")          else                    MsgBox("Connected")          End IfEnd Sub

edit: well i didn't see this has already been suggested

anyway, bear in mind that

- handling the execution with "if...then... else...end if" is generally a better idea than using wild "return"s or "exit sub"s that force the program to exit, it's just easier to read the code

- as long as the number of textboxes is constant, known and small, using this method should be a better idea than doing a loop

Link to comment
Share on other sites

Link to post
Share on other sites

Just to build on what other people have said, do not use returns or exit subs. There should only be one way to enter a function, and one way to leave. It's poor coding practice to do otherwise. 
If you want to be able to get out of a loop, you do a conditional loop and set it so a certain key word would make the loops exit, such as:
 

Do While [variable 1] [parameter] [value 1] and [variable 2] <> [value 2]	{code}	If [variable 1] = [exit value]		[variable 2] = [value 2]	End ifloop

Other than that, it looks good. I'd suggest against using a loop, because you're making it overly complicated (a loop on top of the already existing discussion structure), this ends up being less efficient coding.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×