如何在记事本文件中编辑文本框?

问题描述 投票:0回答:2

[我正在VB.net(Visual Studio)中构建一个小型办公桌应用程序,但我为此感到挣扎。

我想在记事本文件中编辑文本框。该文件将打开并保存在我的PC桌面上,但是当我在文本框中写入内容时,该文件不会发送到记事本。记事本保持空白...到目前为止,在下面的代码中。

Private Sub BtnEditQuestion_Click_1(sender As Object, e As EventArgs) Handles BtnEdiPla.Click
    MsgBox("Now, a notepad will be openned. Edit the question on the file.")
    Dim desktop_path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim writer As TextWriter = New StreamWriter(ruta_escritorio + "\planteamiento.txt")
    writer.Write(TxtPla.Text)
    Process.Start(desktop_path + "\planteamiento.txt")
End Sub

((我还有另一个按钮,将要求用户上传文本文件,以便文本框填充其内容...如果有人知道在一个按钮中同时完成两项工作,也很好)

vb.net textbox edit notepad
2个回答
0
投票

尝试一下:txtBox.Text = = File.ReadAllText(path)


0
投票

我不认为您真的想一次在程序和记事本中显示文件。如果用户想在您的程序中对其进行编辑,则需要2个单独的事件。

我使用了File中的System.IO对象。流需要关闭和处理。 .ReadAllText.WriteAllText将为您打开和关闭文件。

Private PathToFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\planteamiento.txt"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = File.ReadAllText(PathToFile) 'File must exist
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    MsgBox("Now, a notepad will be openned. Edit the question on the file.")
    File.WriteAllText(PathToFile, TextBox1.Text)
    Process.Start(PathToFile)
End Sub

如果要查看在记事本中所做的更改,则必须将文件保存在记事本中,并在程序中再次单击Button1以查看更改。

© www.soinside.com 2019 - 2024. All rights reserved.