|
-
Aug 23rd, 2009, 03:39 PM
#1
Thread Starter
Lively Member
Listbox try catch
I have some items in a listbox and a try catch. I need it where if the program catches an error with the current listbox item to move on to the next one.
Does any one now if this is possible
-
Aug 23rd, 2009, 03:41 PM
#2
Re: Listbox try catch
what do you mean by catches an error?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 23rd, 2009, 03:46 PM
#3
Re: Listbox try catch
Increase the SelectedIndex property by 1 in the Catch clause of the Try-Catch.
Be careful not to go beyond the max index.
-
Aug 23rd, 2009, 03:47 PM
#4
Thread Starter
Lively Member
Re: Listbox try catch
Ok so my program uses email and I have 2 or 3 email accounts in a listbox
if one does not work it moves on to the next
-
Aug 23rd, 2009, 04:18 PM
#5
Thread Starter
Lively Member
Re: Listbox try catch
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strSplit() As String
Dim strResult() As String
'Sets server and port for smtp server
Dim Smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", "587")
'Enables SS1 Encryption.
Smtp.EnableSsl = True
Me.ListBox1.SelectedIndex = 0
Try
Try
strSplit = Split(ListBox1.SelectedIndex, ":")
'Sets username and password for account
Smtp.Credentials = New System.Net.NetworkCredential(strSplit(0), strSplit(1))
MsgBox("worked")
Catch ex As Exception
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
End Try
Catch ex As Exception
End
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ListBox1.Items.Add(Me.TextBox1.Text)
End Sub
End Class
here is the code but i cant get it to work
-
Aug 23rd, 2009, 04:21 PM
#6
Re: Listbox try catch
What exactly, is not working? And why do you have two nested Try-Catch blocks?
-
Aug 23rd, 2009, 05:16 PM
#7
Thread Starter
Lively Member
Re: Listbox try catch
ok i got it working but i have to click the button each time how do i get it to cycle through them?
Code:
Dim strSplit() As String
Dim strResult() As String
'Sets server and port for smtp server
Dim Smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", "587")
Dim Message = New System.Net.Mail.MailMessage("[email protected]", "[email protected]", "worked", "worked")
'Enables SS1 Encryption.
Smtp.EnableSsl = True
Me.ListBox1.SelectedItem = 0
Try
Try
strSplit = Split(ListBox1.SelectedItem, ":")
'Sets username and password for account
Smtp.Credentials = New System.Net.NetworkCredential(strSplit(0), strSplit(1))
Smtp.Send(Message)
Catch ex As Exception
MsgBox("invalid")
ListBox1.SelectedIndex = ListBox1.SelectedItem + 1
End Try
Catch ex As Exception
End
End Try
-
Aug 23rd, 2009, 05:17 PM
#8
-
Aug 23rd, 2009, 05:21 PM
#9
Thread Starter
Lively Member
Re: Listbox try catch
 Originally Posted by Atheist
You'd use a loop.
yea i know i tried to do
for each Item in listbox1.items
next
but i get
"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."
-
Aug 23rd, 2009, 05:38 PM
#10
Re: Listbox try catch
try this:
vb Code:
'Sets server and port for smtp server
Dim Smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
'Enables SS1 Encryption.
Smtp.EnableSsl = True
For Each Item As String In ListBox1.Items
Try
Dim strSplit() As String = Split(Item, ":")
'Sets username and password for account
Smtp.Credentials = New System.Net.NetworkCredential(strSplit(0), strSplit(1))
Smtp.Send(Message)
Catch ex As Exception
MsgBox("invalid")
End Try
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 23rd, 2009, 05:47 PM
#11
Thread Starter
Lively Member
Re: Listbox try catch
that works, but doesnt solve the original problem which is. The program uses the current email until it catches an error and when it catches an error it moves on to the next, I've searched for a long time trying to find this solution
-
Aug 23rd, 2009, 05:53 PM
#12
Re: Listbox try catch
 Originally Posted by Tddupre
The program uses the current email until it catches an error and when it catches an error it moves on to the next
Just to clarify, I have a couple of questions. Do you want to catch the error and then examine it later, or just catch it and move on so that your application doesn't lock up?
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Aug 23rd, 2009, 05:56 PM
#13
Thread Starter
Lively Member
Re: Listbox try catch
Just to move on so like:
Program Is Sending Email and If for some reason the next email it tries to send the email/password it is using doesn't work it uses the next email/password in the list.
-
Aug 23rd, 2009, 06:18 PM
#14
Re: Listbox try catch
 Originally Posted by Tddupre
Just to move on so like:
Program Is Sending Email and If for some reason the next email it tries to send the email/password it is using doesn't work it uses the next email/password in the list.
Oh. Okay. Well, if you just wanted it to move on, then you wouldn't need to put anything after the catch line. The application would catch the error, throw it away and then move on to the next section of code.
In your case, I thought moving to the next line in the ListBox would work. Though, if you go to the next line in the catch part of the try statement, it shouldn't work.
Doing it that way, says for the application to do the loop, if it errors, move the focus to the next line, but not continue the loop. If I'm not mistaken, that is.
You'd probably have to resubmit the loop on error. Or, if there is a "fails" method to System.Net.Mail namespace, then you could use conditional logic to check the state of the message that way.
I'll look it up in MSDN and see what it says.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Aug 23rd, 2009, 06:25 PM
#15
Thread Starter
Lively Member
Re: Listbox try catch
OK, I understand that, but I know no other way of moving to the next email/password then continue sending the email. I have searched everything that i could think of
-
Aug 23rd, 2009, 06:41 PM
#16
Re: Listbox try catch
I can't imagine that there wouldn't be something in the System.Net.Mail namespace that verifies if a mail message has been sent.
I haven't got a chance to research and verify it, but from a quick search on MSDN, I found a SmtpFailedRecipientException Class.
MSDN explains the class in this way:
 Originally Posted by MSDN
Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation to a particular recipient.
This sounds exactly like what you're looking for. I would suggest you do some research into this class and how to utilize it.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Aug 23rd, 2009, 06:42 PM
#17
Re: Listbox try catch
i still don't understand
the code i posted will loop through each item in the listbox, + try to send your message. if theres an error it pops up a msgbox, then moves on to the next listbox item.
i can't see how that isn't doing what you requested?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 23rd, 2009, 07:04 PM
#18
Thread Starter
Lively Member
Re: Listbox try catch
The problem with that code paul is that it will send the email from each email/password in the list
-
Aug 23rd, 2009, 08:53 PM
#19
Re: Listbox try catch
i found this on msdn:
vb Code:
Imports System
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel
Namespace Examples.SmptExamples.Async
Public Class SimpleAsynchronousExample
Shared mailSent As Boolean = False
Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
' Get the unique identifier for this asynchronous operation.
Dim token As String = DirectCast(e.UserState, String)
If e.Cancelled Then
Console.WriteLine("[{0}] Send canceled.", token)
End If
If e.Error IsNot Nothing Then
Console.WriteLine("[{0}] {1}", token, e.Error.ToString())
Else
Console.WriteLine("Message sent.")
End If
mailSent = True
End Sub
Public Shared Sub Main(ByVal args As String())
' Command line argument must the the SMTP host.
Dim client As New SmtpClient(args(0))
' Specify the e-mail sender.
' Create a mailing address that includes a UTF8 character
' in the display name.
Dim from As New MailAddress(" [email protected]", "Jane " & CChar(&Hd8) & " Clayton", System.Text.Encoding.UTF8)
' Set destinations for the e-mail message.
' Specify the message content.
Dim message As New MailMessage(from, to)
message.Body = "This is a test e-mail message sent by an application. "
' Include some non-ASCII characters in body and subject.
Dim someArrows As New String(New Char() {"←"c, "↑"c, "→"c, "↓"c})
message.Body += Environment.NewLine + someArrows
message.BodyEncoding = System.Text.Encoding.UTF8
message.Subject = "test message 1" & someArrows
message.SubjectEncoding = System.Text.Encoding.UTF8
' Set the method that is called back when the send operation ends.
AddHandler client.SendCompleted, AddressOf SendCompletedCallback
' The userState can be any object that allows your callback
' method to identify this send operation.
' For this example, the userToken is a string constant.
Dim userState As String = "test message1"
client.SendAsync(message, userState)
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.")
Dim answer As String = Console.ReadLine()
' If the user canceled the send, and mail hasn't been sent yet,
' then cancel the pending operation.
If answer.StartsWith("c") AndAlso mailSent = False Then
client.SendAsyncCancel()
End If
' Clean up.
message.Dispose()
Console.WriteLine("Goodbye.")
End Sub
End Class
End Namespace
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|