items listbox to custum html mail

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I'm using mail for my program, for the body I use custom html.

I also have a listbox that contains items, my question with this is: How can I add those items to the html with numeric order.
I have tried to use "for next" but inside the html code this doesn't work.

Here is the code I have for the mail:

VB.NET:
Private Sub SendButton_Click(sender As Object, e As System.EventArgs) Handles SendButton.Click
        Dim smtpServer As New SmtpClient
        Dim mail As New MailMessage
        smtpServer.Credentials = New Net.NetworkCredential(My.Settings.EmailUser, My.Settings.EmailPW)
        smtpServer.Port = My.Settings.EmailPort
        smtpServer.Host = My.Settings.EmailHost
        'smtpServer.EnableSsl = True
        mail = New MailMessage
        mail.From = New MailAddress(My.Settings.EmailUser)
        mail.To.Add(ToTextBox.Text)
        If CCTextBox.Text > "" Then
            mail.CC.Add(CCTextBox.Text)
        End If
        mail.Subject = SubjectTextBox.Text
        mail.Body = "<html><head></head>" +
                    "<body style='background:#FFF;'>" +
                    "<table width='100%' border='0'>" +
                    "<tr><td>Songlist for: " + SongServiceEdit1.SelectedText + ", " + DateEdit1.Text + "</td></tr>" +
                    "<tr><td> </td></tr>" +
                    "<tr><td>Worshipservice: " + SongServiceEdit1.SelectedText + "</td></tr>" +
                    "<tr><td>Worshipleader(s): " + LeadersEdit1.SelectedText + "</td></tr>" +
                    "<tr><td></td></tr>" +
                    "<tr><td>Songs: <br></td></tr>" +
                    "<tr><td>[B][COLOR="#FF0000"]This is where the code for the listbox items should go![/COLOR][/B]</td></tr>" +
                    "<tr><td></td></tr>" +
                    "<tr><td>Message:</td></tr>" +
                    "<tr><td>" + RichEditControl1.HtmlText + "</td></tr>" +
                    "</table><br><br>" +
                    "Created with " + Application.Title + ": " + Application.ProductVersion +
                    "</body></html>"
        mail.IsBodyHtml = True
        Try
            smtpServer.Send(mail)
            MessageBox.Show("Songlist Send!", "App title", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Hope someone can help me.

Thanks
 
Hi their, In HTML this would be the way to do a Dropdown list box if thats what you mean, if not let me know :)
VB.NET:
  <select name="select">
    <option value="Song1 Value Here">Song 1</option>
    <option value="Song2 Value Here">Song 2</option>
    <option value="Song3 Value Here">Song 3</option>
  </select>

Hope that helps
 
Just to add to the top post here's a ListBox so you can see all items at once.

VB.NET:
<select name="select" size="3">
  <option value="Song1 Value Here">Song 1</option>
  <option value="Song2 Value Here" selected>Song 2</option>
  <option value="Song3 Value Here">Song 3</option>
</select>

Pick which one you need
 
Sorry but I forgot to mention I use vb2010, so this solution with the select option will totally not work.

Also the amount of items is not always the same.
I had found something but don't know how to implement it:
VB.NET:
dim aValues() as String    'the array 
dim i as integer 
For i = 0 To lstValues.ListCount 
lstValues.AddItem txtAddManual.text 
aValues(i) = lstValues.List(i) 
Next i
 
Bellow VB.NET Code
VB.NET:
Private Sub SendButton_Click(sender As Object, e As System.EventArgs) Handles SendButton.Click
        Dim smtpServer As New SmtpClient
        Dim mail As New MailMessage
        smtpServer.Credentials = New Net.NetworkCredential(My.Settings.EmailUser, My.Settings.EmailPW)
        smtpServer.Port = My.Settings.EmailPort
        smtpServer.Host = My.Settings.EmailHost
        'smtpServer.EnableSsl = True
        mail = New MailMessage
        mail.From = New MailAddress(My.Settings.EmailUser)
        mail.To.Add(ToTextBox.Text)
        If CCTextBox.Text > "" Then
            mail.CC.Add(CCTextBox.Text)
        End If
        mail.Subject = SubjectTextBox.Text

</CODE>
Bellow HTML Code as string
<CODE>
        mail.Body = "<html><head></head>" +
                    "<body style='background:#FFF;'>" +
                    "<table width='100%' border='0'>" +
                    "<tr><td>Songlist for: " + SongServiceEdit1.SelectedText + ", " + DateEdit1.Text + "</td></tr>" +
                    "<tr><td> </td></tr>" +
                    "<tr><td>Worshipservice: " + SongServiceEdit1.SelectedText + "</td></tr>" +
                    "<tr><td>Worshipleader(s): " + LeadersEdit1.SelectedText + "</td></tr>" +
                    "<tr><td></td></tr>" +
                    "<tr><td>Songs: <br></td></tr>" +
                    "<tr><td>This is where the code for the listbox items should go!</td></tr>" +
                    "<tr><td></td></tr>" +
                    "<tr><td>Message:</td></tr>" +
                    "<tr><td>" + RichEditControl1.HtmlText + "</td></tr>" +
                    "</table><br><br>" +
                    "Created with " + Application.Title + ": " + Application.ProductVersion +
                    "</body></html>"
        mail.IsBodyHtml = True
</CODE>
Bellow VB.NET Code
<CODE>
        Try
            smtpServer.Send(mail)
            MessageBox.Show("Songlist Send!", "App title", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

What im saying is i just thought you wanted the HTML Equivlent of a DropDown List or ListBox, thats not the case, so to clarify this ill try and explaine what you would like to do, You want to get all Listbox.Items to display on webcode Listbox right? if so you'll need to do a For Next loop, For Each, or Do while, which ever suits your needs and sent it to a String Array and assign to WebCode Dropdownlist or Listbox, if im wrong then do say so, and explaine more to me please.
 
ok formum hadn't updated as i was still in post message to here, and had to nip to out for a sec, Right yes this code is good you'll for adding items to a listbox on as Windows Form Application, but then you'll need to get the items back as String() array, which would be easier so you can use the Array value in som form ouf count method and get the strings back to place in your HTML string to add to a HTML listbox do you mean this?
VB.NET:
dim aValues() as String    'the array 
dim i as integer 
For i = 0 To lstValues.ListCount 
lstValues.AddItem txtAddManual.text 
aValues(i) = lstValues.List(i) 
Next i

This code would be better to run in the form load to initilize first then be called to the HTML sting to get items, but this code only adds to a Listbox (Windows Form only)
 
Hi look at this you'll need to make a blank form

Components used and Names used
1 x Button = "btnRun"
1 x ListBox = "lstItems"
1 x TextBox = "txtResult"
1 x WebBrowser = "webResult"

VB.NET:
Option Explicit On
Option Strict On

Public Class ListItems

    Private Sub ListItems_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim aValues() As String = {"Value1", "Value2", "Value3", "Value4"}
        lstItems.Items.AddRange(aValues)
    End Sub

    Private Sub btnRun_Click(sender As System.Object, e As System.EventArgs) Handles btnRun.Click
        Dim strHTML As String = ""
        For i = 0 To lstItems.Items.Count - 1
            txtResult.Text += (i + 1) & " " & lstItems.Items(i).ToString & vbCrLf
            strHTML += "<option value=""" & lstItems.Items(i).ToString & """>" & (i + 1) & lstItems.Items(i).ToString & "</option>" 'Stored Generated HTML and required Values in string then Passes then on to the webResult and writes then in the WebPageDocument.
      Next
        webResult.Navigate("about:blank")
        webResult.Document.Write("<select name=" & """select""" & ">" & strHTML & "</select>")
    End Sub
End Class

Hope this helps, just made it so you can try and see if the requires result is what you need
 
Back
Top