-
Oct 24th, 2009, 07:13 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Forcing a messagebox to appear more than once if needed ?
Hi guys ,
On my emailer program ive created a messagebox to appear if any of the textboxes are empty when i clcik send, its works fine (first time) but if i leave a textbox empty for the second time and click send i get an error.
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" And TextBox3.Text = "" And TextBox4.Text = "" And TextBox6.Text = "" And TextBox2.Text = "" Then
MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation)
End If
i = 0
strmessage = "Your e-mail has been sent !"
Timer2.Enabled = True
Label4.Text = ""
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
SmtpServer.EnableSsl = False
mail.To.Add(TextBox4.Text)
mail.From = New MailAddress(TextBox1.Text)
mail.Subject = TextBox6.Text
mail.Body = TextBox2.Text
If Not TextBox8.Text = "" Then
Dim attach As New Attachment(TextBox8.Text)
mail.Attachments.Add(attach)
End If
SmtpServer.Send(mail)
MsgBox("E-mail Sent Successfully - " + TextBox4.Text + "", MsgBoxStyle.Information)
End Sub
-
Oct 24th, 2009, 07:19 AM
#2
Re: Forcing a messagebox to appear more than once if needed ?
I wish I had a $ for every time I've had to post this. If you get an error then you get an error message, to help you diagnose the issue. If you want us to diagnose the issue then you need to give us the error message and tell us where in the code it occurs.
-
Oct 24th, 2009, 07:21 AM
#3
Thread Starter
Fanatic Member
Re: Forcing a messagebox to appear more than once if needed ?
sorry
This line : mail.To.Add(TextBox4.Text)
error : argument exception was unhandled
-
Oct 24th, 2009, 08:00 AM
#4
Re: Forcing a messagebox to appear more than once if needed ?
looks like you're testing EVERY textbox every time you click the button, where you really want to test them individually.
Change all of you "And"s to "Or"s
It will test the textboxes individually. The reason it wouldn't call the msgbox the second time is because you probably typed something in one of the textboxes and left the rest blank to test. The condition tests whether they area ALL blank, so it fails and does not call the msgbox.
-
Oct 24th, 2009, 08:11 AM
#5
Thread Starter
Fanatic Member
Re: Forcing a messagebox to appear more than once if needed ?
Ok ive tried that , left them all blank , clciked it got the messagebox , clicked again(still all blank) same error again
-
Oct 24th, 2009, 08:14 AM
#6
Re: Forcing a messagebox to appear more than once if needed ?
ok.. well after it calls the msgbox, it will continue to do the rest of the code. so if textbox4 is blank, it is going to add nothing to the "To" field.
you need to call the msgbox, then line after do a "Exit Sub"
-
Oct 24th, 2009, 08:17 AM
#7
Thread Starter
Fanatic Member
Re: Forcing a messagebox to appear more than once if needed ?
not sure what u mean here >>
Code:
you need to call the msgbox, then line after do a "Exit Sub"
-
Oct 24th, 2009, 08:18 AM
#8
Thread Starter
Fanatic Member
Re: Forcing a messagebox to appear more than once if needed ?
oh got you i think , 1 sec lol
yep thats it m8 thanks very much , great help, ty
-
Oct 24th, 2009, 08:24 AM
#9
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
actually, don't exit sub, just put all the code after the msgbox in an else clause.
vb.net Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox6.Text = "" Or TextBox2.Text = "" Then
MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation)
Else
i = 0
strmessage = "Your e-mail has been sent !"
Timer2.Enabled = True
Label4.Text = ""
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
SmtpServer.EnableSsl = False
mail.To.Add(TextBox4.Text)
mail.From = New MailAddress(TextBox1.Text)
mail.Subject = TextBox6.Text
mail.Body = TextBox2.Text
If Not TextBox8.Text = "" Then
Dim attach As New Attachment(TextBox8.Text)
mail.Attachments.Add(attach)
End If
SmtpServer.Send(mail)
MsgBox("E-mail Sent Successfully - " + TextBox4.Text + "", MsgBoxStyle.Information)
End if
End Sub
-
Oct 24th, 2009, 08:26 AM
#10
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
I would also suggest you to use OrElse insted of Or. This short-circuits the conditions and bypasses unnecessary checks in case the first (or any previous) condition returns True.
vb.net Code:
If TextBox1.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" OrElse TextBox6.Text = "" OrElse TextBox2.Text = "" Then MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation) Exit Sub End If
-
Oct 24th, 2009, 08:26 AM
#11
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
Oh , ok will it make any difference if i exit sub or use else ?
-
Oct 24th, 2009, 08:28 AM
#12
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
yea , "else" still gives the error , needs to be "exit sub" , thanks guys
-
Oct 24th, 2009, 08:30 AM
#13
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
guys now that your here lol ,
How can i implememt a checkbox as an option to send emails to other smtp servers if the checkbox is checked , currently its set to send emails from @yahoo.co.uk accounts
-
Oct 24th, 2009, 08:35 AM
#14
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
the "else" should work just fine. it'll look at each textbox and if one is empty, it'll call the msgbox, if they are all filled, it will execute the else portion.
@Pradeep1210: good call on the OrElse, forgot about that
to implement the other smtp, you would basically test to see if Checkbox1.Checked = True, then set your smtp properties accordingly. if not checked, then keep your yahoo settings
vb.net Code:
If CheckBox1.Checked = True Then
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = *Whatever the Port is for the new smtp
SmtpServer.Host = "WhateverTheHostisForNewSMTP"
SmtpServer.EnableSsl = False
Else
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
SmtpServer.EnableSsl = False
End If
-
Oct 24th, 2009, 08:56 AM
#15
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
Ok its throwing an error up on this line >>>> SmtpServer.Send(mail)
Error : Smtp Exception Unhandled
My current code:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" OrElse TextBox6.Text = "" OrElse TextBox2.Text = "" Then
MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation)
Exit Sub
End If
i = 0
strmessage = "Your e-mail has been sent !"
Timer2.Enabled = True
Label4.Text = ""
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient
If CheckBox1.Checked = True Then
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.live.com"
SmtpServer.EnableSsl = True
Else
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
SmtpServer.EnableSsl = False
End If
mail.To.Add(TextBox4.Text)
mail.From = New MailAddress(TextBox1.Text)
mail.Subject = TextBox6.Text
mail.Body = TextBox2.Text
If Not TextBox8.Text = "" Then
Dim attach As New Attachment(TextBox8.Text)
mail.Attachments.Add(attach)
End If
SmtpServer.Send(mail)
MsgBox("E-mail Sent Successfully - " + TextBox4.Text + "", MsgBoxStyle.Information)
End Sub
-
Oct 24th, 2009, 09:03 AM
#16
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
Ok i sorted that, forgot to add the checkbox , oh man how dumb an i lmao.
So how do i display a message box to let the user know that they are using the wrong email address if the check box is not ticked and matching the the address ?
-
Oct 24th, 2009, 09:07 AM
#17
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
also can i add a third smtp server to this ?
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" OrElse TextBox6.Text = "" OrElse TextBox2.Text = "" Then
MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation)
Exit Sub
End If
i = 0
strmessage = "Your e-mail has been sent !"
Timer2.Enabled = True
Label4.Text = ""
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient
If CheckBox1.Checked = True Then
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.live.com"
SmtpServer.EnableSsl = True
Else
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
SmtpServer.Port = 587
SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
SmtpServer.EnableSsl = False
End If
-
Oct 24th, 2009, 09:10 AM
#18
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
i think a better way to do it rather than inundating the user with msgbox's would be to have 2 radio buttons, one for yahoo, and one for windows live.
then when they click the button, test for which radio button is checked and set the settings accordingly.
*make one of them checked by default*
or you could use a regex (dont ask me how, im terrible at regex) to test the email address they enter and select the smtp settings based on that so that they don't even have to select anything.
-
Oct 24th, 2009, 09:12 AM
#19
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
ok so if lets say im using 3 smtp servers and 2 checkboxes , how do i code that correctly , and also how do i implement the radio buttons ?
-
Oct 24th, 2009, 10:19 AM
#20
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
just add the smtp servers to a combo box and when they click the button, do an If/Else to find out which smtp was selected from the box. supply the appropriate settings for that smtp.
if the only thing that changes between them is the host, then make the combobox entry the exact text of the host name.
then in the code set the smtp host = combobox.selectedindex.text (or whatever it is)
this is the easiest way to do it as it also prohibits more than one selection as well (like a radio button would, but less cluttered)
-
Oct 24th, 2009, 12:14 PM
#21
Thread Starter
Fanatic Member
Re: [RESOLVED] Forcing a messagebox to appear more than once if needed ?
ok ty , got all that sorted m8 thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|