我目前正在尝试制作地址簿应用程序。如果应用程序目录中有默认地址簿,它会检查负载。如果没有则用户可以打开选定的地址簿。将选定文件设置为当前文件时出现问题。任何工作都很好,直到我打开第二个表单,它将新的联系人添加到文件并添加它。联系人添加到以第一种形式选择的文件但是当我关闭这个“添加表单”时,当前的xml路径重置并且程序要求地址簿再次打开而不是打开我在“添加联系人表单”中修改的xml文件。即使我在“添加联系人表单”中设置xmlCurrentPath,它仍然会在联系人表单再次显示后清除。这是我的代码:
Public Class Form2
Private myTable As New DataTable()
Dim path = My.Application.Info.DirectoryPath
Dim xmlDefaultPath As String = path + "\address.xml"
Dim xmlCurrentPath As String
Dim fileDlg As New OpenFileDialog
Public isXml As Boolean = False
'Sets current xml path
Public Sub setCurrentPath(ByVal path As String)
xmlCurrentPath = path
End Sub
Public Sub XmlExistence(ByVal xmlpath As String)
'Checks if current xml path exists and if not then checks if there is a default address book file in root directory
If IsNothing(xmlCurrentPath) Then
If System.IO.File.Exists(xmlpath) Then
xmlCurrentPath = xmlDefaultPath
Else
xmlCurrentPath = ""
End If
End If
End Sub
Public Sub LoadForm(sender As Object, e As EventArgs) Handles Me.Shown
'checks if there is current xml path and if not asks if you want to choose from file
If IsNothing(xmlCurrentPath) Then
XmlExistence(xmlDefaultPath)
If Not xmlCurrentPath = "" Then
CreateTable()
LoadXml(xmlCurrentPath)
Else
CreateTable()
Dim addressfile = MessageBox.Show("Brak pliku adresów w katalogu głównym programu. Czy chcesz wybrać plik z dysku ? Jeśli wybierzesz nie zostanie utworzony nowy plik.", "Pytanie", MessageBoxButtons.YesNo)
If addressfile = Windows.Forms.DialogResult.Yes Then
fileDlg.Title = "Otwórz plik adresów"
fileDlg.Filter = "XML Files|*.xml"
fileDlg.ShowDialog()
xmlCurrentPath = fileDlg.FileName
LoadXml(xmlCurrentPath)
End If
End If
End If
End Sub
如何在将新联系人添加到之前选择的地址簿后,当添加联系表单关闭时,地址簿表格将再次显示此地址簿。