简体   繁体   中英

Populate Listbox from XML Class with multiple values

I am attempting to populate a Listbox from a class read in from XML.

I have the following code:

Imports System.IO
Imports System.Windows.Forms
Imports System.Xml.Serialization

Public Class TruckToOfficeMessage_Email

  public class EmailMessage
    public Property EmailSubject As String
    Public Property EmailBody As String
    Public Property Description As string
  End class

  Private _emailMessages  as List(of EmailMessage)

  Private Sub TruckToOfficeMessage_Email_Load(sender As Object, e As EventArgs) Handles Me.Load
    LoadEmailMessages()
  End Sub

  private sub LoadEmailMessages()
    ReadFromXmlFile()
    ListBox_EmailTemplates.DataSource = _emailMessages
    ListBox_EmailTemplates.DisplayMember = "Description"
    ListBox_EmailTemplates.ValueMember = "EmailBody"
  End sub

  Private sub SendMail()

    Dim hasErrors As Boolean
    If String.IsNullOrEmpty(ComboBox_EmailAddresses.Text) Then
      hasErrors = True
    End If

  End sub

  Public ReadOnly Property ListBoxEmailTemplates As ListBox
    Get
      Return _ListBox_EmailTemplates
    End Get
  End Property


  Private Sub Button_Send_Click(sender As Object, e As EventArgs) Handles Button_Send.Click
    SendMail()
  End Sub



  Private sub WriteToXmlFile()

    Dim serializer As New XmlSerializer(GetType(List(Of EmailMessage)), New XmlRootAttribute("EmailMessage"))
    Using file As System.IO.FileStream = System.IO.File.Open("d:\temp\obj.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
      serializer.Serialize(file, _emailMessages)
    End Using

  End sub

  private sub ReadFromXmlFile()

    dim xmlfile = "d:\temp\obj.xml"
    If Not File.Exists(xmlfile) then
      _emailMessages = New List(Of EmailMessage) 
      return
    End If

    Dim serializer As New XmlSerializer(GetType(List(Of EmailMessage)), New XmlRootAttribute("EmailMessage"))
    Using file = System.IO.File.OpenRead(xmlfile)
      _emailMessages = DirectCast(serializer.Deserialize(file), List(Of EmailMessage))
    End Using

    if _emailMessages Is nothing then
      _emailMessages = New List(Of EmailMessage) 
    End If

  End sub

  Private Sub Button_SaveTemplate_Click(sender As Object, e As EventArgs) Handles Button_SaveTemplate.Click
    UpdateXmlWithChanges()
    WriteToXmlFile()
    LoadEmailMessages()
  End Sub

  Private sub UpdateXmlWithChanges()

    if _emailMessages Is nothing then
      _emailMessages = New List(Of EmailMessage) 
      dim em = new EmailMessage()
      em.EmailSubject = TextBox_EmailSubject.Text
      em.EmailBody = RichTextBox_EmailBody.Text
      em.Description = TextBox_CurrentTemplateName.Text
      _emailMessages.Add(em)
      return
    End If

    For Each emailMessage As EmailMessage In _emailMessages
      if  String.Compare(emailMessage.Description, TextBox_EmailSubject.Text,StringComparison.OrdinalIgnoreCase) = 0 Then
        emailMessage.EmailBody = RichTextBox_EmailBody.Text
        emailMessage.EmailSubject = TextBox_EmailSubject.Text
        return
      End If
    Next

    dim em1 = new EmailMessage()
    em1.EmailSubject = TextBox_EmailSubject.Text
    em1.EmailBody = RichTextBox_EmailBody.Text
    em1.Description = TextBox_CurrentTemplateName.Text
    _emailMessages.Add(em1)

  End sub

  Private Sub ListBox_EmailTemplates_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListBox_EmailTemplates.MouseDoubleClick
    if not ListBox_EmailTemplates.SelectedItem Is Nothing then
      RichTextBox_EmailBody.Text = ListBox_EmailTemplates.SelectedValue.ToString()
      TextBox_EmailSubject.Text = ListBox_EmailTemplates.GetItemText(ListBox_EmailTemplates.SelectedItem)
    End If
  End Sub
End Class

I have 3 values I am reading from the xml file:

public Property EmailSubject As String
Public Property EmailBody As String
Public Property Description As string

I have set the data source for the Listbox as such:

ListBox_EmailTemplates.DataSource = _emailMessages
ListBox_EmailTemplates.DisplayMember = "Description"
ListBox_EmailTemplates.ValueMember = "EmailBody"

My question is, how do I store 3 values in the listbox instead of just two? I need the EmailSubject also but I cannot figure out to reference it once the customer clicks on the row.

Thanks!

Each item in ListBox_EmailTemplates is a EmailMessage. Although the item is type Object you can cast to the underlying type EmailMessage and then access its properties.

Dim itm = ListBox_EmailTemplates.SelectedItem
Dim EM = DirectCast(itm, EmailMessage)
Dim Subject = EM.EmailSubject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM