我正在尝试从XML读取的类中填充列表框。
我有以下代码:
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
我有3个从xml文件读取的值:
public Property EmailSubject As String
Public Property EmailBody As String
Public Property Description As string
我已经这样设置了列表框的数据源:
ListBox_EmailTemplates.DataSource = _emailMessages
ListBox_EmailTemplates.DisplayMember = "Description"
ListBox_EmailTemplates.ValueMember = "EmailBody"
我的问题是,如何在列表框中存储3个值,而不是两个?我也需要EmailSubject,但是一旦客户单击该行,我就无法确定要引用它。
谢谢!
答案 0 :(得分:2)
ListBox_EmailTemplates中的每个项目都是一个EmailMessage。尽管该项是Object类型,但是您可以将其转换为基础类型EmailMessage,然后访问其属性。
Dim itm = ListBox_EmailTemplates.SelectedItem
Dim EM = DirectCast(itm, EmailMessage)
Dim Subject = EM.EmailSubject