|
I am Creating a Client that uses email to send and recieve text messages. I only have to problems when i recieve the mail from the server it goes into my listbox but how do i make that listbox string, when selected, open the body of the message in a richtextbox. Here is my code so far.
Also here is a picture of the GUI so you can see the problem a little more clearly.
Click Here For GUI Picture
The messages are put into the Inbox i just cant display the body of the message in the richtext box when selected from the listbox. Any Help will be greatly appreciated! Thanks!
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim strMailServer, strUsername, strPassword, strFrom, strSubject, strToo, strBody, strMailContent As String
Dim popConn As Text_Me_Now.POP3
Dim mailMess As Text_Me_Now.EmailMessage
Dim intMessCnt, i As Integer
strMailServer = "pop.gmx.com"
strUsername = "xxxxxxx@gmx.com"
strPassword = "xxxxxxx"
Button2.Enabled = False
popConn = New Text_Me_Now.POP3
mailMess = New Text_Me_Now.EmailMessage
popConn.POPConnect(strMailServer, strUsername, strPassword)
intMessCnt = popConn.GetMailStat()
If intMessCnt > 0 Then
ListBox1.Items.Clear()
For i = 1 To intMessCnt
strMailContent = popConn.GetMailMessage(i)
strFrom = mailMess.ParseEmail(strMailContent, "From:")
strSubject = mailMess.ParseEmail(strMailContent, "Subject:")
strToo = mailMess.ParseEmail(strMailContent, "To:")
strBody = mailMess.ParseBody()
ListBox1.Items.Add(strFrom & ", " & strSubject)
Next i
End If
popConn.CloseConn()
Button2.Enabled = True
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object,
End Sub
|
|
|
|
|
|
You appear to have extracted all the relevant information for the message, but the only thing you are storing in the ListBox items collection is the from and and subject parts as a single string.
ListBox1.Items.Add(strFrom & ", " & strSubject)
I would suggest that instead of storing this string, that you instead define a class to hold the relevant information.
Something like this:
Public Class MessageItem
Public From As String
Public Body As String
Public Subject As String
Public Sub New(ByVal From As String, ByVal Subject As String, ByVal Body As String)
Me.From = From
Me.Body = Body
Me.Subject = Subject
End Sub
Public Overrides Function ToString() As String
Return From & ", " & Subject
End Function
End Class
Instead of adding items directly to the ListBox, you bind the ListBox to a List(of MessageItem). Define this list at the form level like this:
Private Messages As New List(Of MessageItem)
In the FormLoad event handler, place this statement:
ListBox1.DataSource = Messages
The ToString Function defined in the class will provide the text displayed for the item in the ListBox.
Now in your button_click handler, replace this code:
'call the functions to get the various parts out of the email
strFrom = mailMess.ParseEmail(strMailContent, "From:")
strSubject = mailMess.ParseEmail(strMailContent, "Subject:")
strToo = mailMess.ParseEmail(strMailContent, "To:")
strBody = mailMess.ParseBody()
'add email details to the list box
ListBox1.Items.Add(strFrom & ", " & strSubject)
with this:
Dim msg As New MessageItem(mailMess.ParseEmail(strMailContent, "From:"), _
mailMess.ParseEmail(strMailContent, "Subject:"), _
mailMess.ParseBody())
Messages.Add(msg)
Also replace ListBox1.Items.Clear() with Messages.Clear()
Now in your ListBox1_SelectedIndexChanged method you can set the textbox text with something like this:
RichTextBox1.Text = CType(ListBox1.SelectedItem, MessageItem).Body
|
|
|
|