[英]How to save the as I type in a textbox
I'm trying to make a textbox where the user inputs a string like "Joe was here" and that same string is "written" on the text file at the same time. 我正在尝试创建一个文本框,用户在其中输入类似“ Joe was here”的字符串,并且该字符串同时被“写入”文本文件。 Most of the questions asked around is using a button that helps save the string to the text file. 询问的大多数问题都使用一个按钮来帮助将字符串保存到文本文件中。
It works great, however for some unknown reason the text file can't register the last key pressed. 效果很好,但是由于某些未知原因,文本文件无法注册最后按下的键。 In other words if I wrote "Joe was here" in my textbox, the text file has "Joe was her" where the "e" is missing.It's always the last key :( 换句话说,如果我在文本框中输入“ Joe was here”,则文本文件中将缺少“ e”的“ Joe was her”。它始终是最后一个键:(
This is a general view of the code I have that makes it work 这是使它起作用的代码的一般视图
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
Dim File_name As String = "path where text file is saved at"
If System.IO.File.Exists(File_name) Then
Dim objWriter As New System.IO.StreamWriter(File_name)
objWriter.Write(TextBox1.Text)
objWriter.Close()
End If
End Sub
Maybe I'm missing something? 也许我缺少什么? Perhaps I'm using the wrong type of event as I'm using keypress instead of keydown or something else? 也许我使用的是错误类型的事件,而不是使用按键而不是按下keydown或其他事件?
i just did a quick sample with your code and gives the exact issue, so i changed to the event keyup and it worked. 我只是用您的代码做了一个简单的示例,并给出了确切的问题,所以我更改为事件键盘,并且它起作用了。
reason could be the time that keypress event processes or receives the key code at least in keyup first receive all the text and then executes the call to the process. 原因可能是按键事件至少在键盘输入中处理或接收按键代码的时间,该时间首先接收所有文本,然后执行对流程的调用。
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
My.Computer.FileSystem.WriteAllText("C:\test.txt", TextBox1.Text, False)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.