Results 1 to 21 of 21

Thread: [RESOLVED] Forcing a messagebox to appear more than once if needed ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Forcing a messagebox to appear more than once if needed ?

    sorry

    This line : mail.To.Add(TextBox4.Text)

    error : argument exception was unhandled

  4. #4
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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

  6. #6
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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"

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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"

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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

  9. #9
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         If TextBox1.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox6.Text = "" Or TextBox2.Text = "" Then
    3.             MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation)
    4. Else
    5.         i = 0
    6.         strmessage = "Your e-mail has been sent !"
    7.         Timer2.Enabled = True
    8.         Label4.Text = ""
    9.    
    10.  
    11.         Dim mail As New MailMessage()
    12.         Dim SmtpServer As New SmtpClient
    13.         SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
    14.         SmtpServer.Port = 587
    15.         SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
    16.         SmtpServer.EnableSsl = False
    17.         mail.To.Add(TextBox4.Text)
    18.         mail.From = New MailAddress(TextBox1.Text)
    19.         mail.Subject = TextBox6.Text
    20.         mail.Body = TextBox2.Text
    21.        
    22.  
    23.         If Not TextBox8.Text = "" Then
    24.             Dim attach As New Attachment(TextBox8.Text)
    25.             mail.Attachments.Add(attach)
    26.         End If
    27.  
    28.         SmtpServer.Send(mail)
    29.         MsgBox("E-mail Sent Successfully - " + TextBox4.Text + "", MsgBoxStyle.Information)
    30.         End if
    31.     End Sub

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. If TextBox1.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" OrElse TextBox6.Text = "" OrElse TextBox2.Text = "" Then
    2.     MsgBox("You Didn't Fill Up The Form Correctly, Please Check Again", MsgBoxStyle.Exclamation)
    3.     Exit Sub
    4. End If
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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 ?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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

  14. #14
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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:
    1. If CheckBox1.Checked = True Then
    2.         SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
    3.         SmtpServer.Port = *Whatever the Port is for the new smtp
    4.         SmtpServer.Host = "WhateverTheHostisForNewSMTP"
    5.         SmtpServer.EnableSsl = False
    6. Else
    7.         SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
    8.         SmtpServer.Port = 587
    9.         SmtpServer.Host = "Smtp.mail.yahoo.co.uk"
    10.         SmtpServer.EnableSsl = False
    11. End If

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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 ?

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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

  18. #18
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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 ?

  20. #20
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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)

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    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
  •  



Click Here to Expand Forum to Full Width