1

我目前正在尝试制作地址簿应用程序。它会在加载时检查应用程序目录中是否有默认地址簿。如果没有,则用户可以打开选定的地址簿。我将所选文件设置为当前文件时遇到问题。在我打开将新联系人添加到文件并添加它的第二个表单之前,一切都很好。联系人添加到第一个表单中选择的文件,但是当我关闭此“添加表单”时,当前的 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

如何在将新联系人添加到先前选择的通讯录后,当添加联系人表单关闭时,通讯录表单将再次显示此通讯录。

4

0 回答 0