我开发了一个sendmail程序以获得支持,效果很好。我现在要做的是添加一个历史记录页面,它会向我的用户显示他们之前发送的支持请求。我打算使用My.Settings来保存从我的程序发送的每封电子邮件。
以下是我发送邮件的SendMail按钮的代码:
If TextBox1.Text = "" Then
MsgBox("Please describe the issue you're having Bender, I'm not a mindreader!")
Exit Sub
Else
lblpleasewait.Visible = True
delay(2000)
Dim Recipients As New List(Of String)
Recipients.Add("johndoe@yahoo.com")
Dim FromEmailAddress As String = Recipients(0)
Dim Subject As String = "IT Help!"
Dim Body As String = TextBox1.Text
Dim UserName As String = My.Settings.txtboxUN
Dim Password As String = My.Settings.txtboxPW
Dim Port As Integer = My.Settings.txtboxSMTPPort
Dim Server As String = My.Settings.txtboxSMTP
Dim Attachments As New List(Of String)
MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))
lblpleasewait.Visible = False
TextBox1.Text = ""
TextBox1.Focus()
'This is where the beginning of my code is to send it to the history form.
Dim str(2) As String
Dim itm As ListViewItem
str(0) = Today + " - " + TimeOfDay
str(1) = Body
itm = New ListViewItem(str)
GTSMailHistory.ListView1.Items.Add(itm)
My.Settings.logDate = str(0)
My.Settings.logIssue = str(1)
My.Settings.Save()
End If
从上面的代码中可以看出,最后几行是我添加电子邮件正文和一天中的时间,并将其添加到我的另一个表单(GTSMailHistory)列表视图中。
我的问题是,上面将代码发送到另一个表单的代码正在保存,但是每封新邮件都会覆盖。它基本上没有将新的电子邮件附加到列表中,只是写了第一个。
我在历史表单上的唯一代码是LOAD函数,如下所示:
Private Sub GTSMailHistory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str(2) As String
Dim itm As ListViewItem
str(0) = My.Settings.logDate
str(1) = My.Settings.logIssue
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
End Sub
答案 0 :(得分:0)
我还不能发表评论,所以如果有人能够提出这个意见,那就有所帮助。
您是在每次通过时重新创建GTSMailHistory对象而不是仅在启动时重新创建一次?
另外,当您将日期/正文保存到My.Settings时,您正在覆盖现有值,那么您不希望这些是列表吗?
回应你的评论:
Joiner,我确实希望它成为一个清单。我可以将listview保存到my.settings,以便在下次程序启动时保持持久性吗?
您可以使用My.Settings存储不同类型的变量,包括列表。
在Visual Studio中转到:
项目 - >属性 - >设置(标签) - >添加设置及其类型(浏览更多)。
有关示例,请参阅here。
答案 1 :(得分:0)
找到答案。不是我想要的,但现在就足够了。我可以将其写入文本文件并使用streamreader将其加载到listview。
以下代码将正文和日期添加到listview:
Static i As Integer = 0
Dim newItem As New ListViewItem(Today + " - " + TimeOfDay) '// add text Item.
newItem.SubItems.Add(TextBox2.Text) '// add SubItem.
history.ListView1.Items.Add(newItem) '// add Item to ListView.
i += 1
以下代码加载保存的文本文件
ListView1.View = View.Details : ListView1.Columns.Add("Date") : ListView1.Columns.Add("Issue")
If IO.File.Exists(myCoolFile) Then '// check if file exists.
Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your file as a string array.
For Each line As String In myCoolFileLines '// loop thru array list.
Dim lineArray() As String = line.Split("#") '// separate by "#" character.
Dim newItem As New ListViewItem(lineArray(0)) '// add text Item.
newItem.SubItems.Add(lineArray(1)) '// add SubItem.
ListView1.Items.Add(newItem) '// add Item to ListView.
Next
End If
在表单关闭时,它会保存我的条目:
Dim myWriter As New IO.StreamWriter(myCoolFile)
For Each myItem As ListViewItem In ListView1.Items
myWriter.WriteLine(myItem.Text & "#" & myItem.SubItems(1).Text) '// write Item and SubItem.
Next
myWriter.Close()