0

我有一个文本框(textbox1),当加载 WPF 窗口时,它使用 UserProfile 变量在 textbox1.text 中显示当前用户目录

   Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    Dim defaultpath As String = Environment.CurrentDirectory
    Environment.CurrentDirectory = Environment.GetEnvironmentVariable("UserProfile")
    TextBox1.Text = defaultpath

    End Sub

我还有一个按钮,单击该按钮时,使用 FolderBrowserDialog 浏览文件夹,然后在 textbox1.text 中显示新文件夹路径。

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim fldDialog As New FolderBrowserDialog()
    fldDialog.RootFolder = Environment.SpecialFolder.Desktop
    fldDialog.ShowDialog()
    Dim filepathstore As String = fldDialog.SelectedPath

    TextBox1.Text = filepathstore
     End Sub

该值现在显示使用 FolderBrowserDialog 选择的路径。

我将如何存储这个新值,当 WPF 窗口关闭/重新打开时,显示这个新值而不是默认值。(替换不删除默认值)

文件夹路径的这个新值可以根据需要多次更改。但是,当单击重置按钮时,WPF 窗口将恢复为默认值。

4

1 回答 1

0

听起来最简单的选择是将值保存到可以随时调用的文件中。下面的代码假定您要保存的值在调用 FormClosing 事件时位于 textbox1.text 中,然后在打开表单时将其加载回 textbox1.text 中。

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim s As String
    s = TextBox1.Text
    Dim loc As String
    loc = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/testfile.txt"
    My.Computer.FileSystem.WriteAllText(loc, TextBox1.Text, False)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim loc As String
    loc = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/testfile.txt"
    Dim s As String
    s = My.Computer.FileSystem.ReadAllText(loc)
    TextBox1.Text = s
End Sub

如果您对此有任何疑问,请告诉我:)

于 2012-11-22T05:32:46.243 回答