简体   繁体   English

如何在输入文本框时保存

[英]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.

相关问题 如何将文本框内容保存到应用程序本身? - How can I save the textbox content to the application itself? 如何将文本从一个文本框保存/追加到VB中同一表单上的另一个文本框 - How can I save/append text from one textbox to another textbox on the same Form in VB 如何将多个文本框字段保存到VB.Net中的.txt文件 - How do I save multiple Textbox fields to a .txt file in VB.Net 如何使用FolderBrowsedialog将文本框数据保存到文件 - How to save textbox data to file using FolderBrowsedialog 如何在文本框中保存用户输入的值? - How to save user inputted value in TextBox? 如何在.NET中将自定义类型保存为二进制数据? - How can I save a custom type as binary data in .NET? 使文本框类型变成文本框? - Making Textbox type into a textbox? 如何将文本框文本保存到记事本并再次保存文本框文本以将其添加到同一个记事本中? - How to save textbox text to notepad and save again a textbox text to add it into the same note pad? 我想在asp.net文本框中输入140个字符,并且多行字符会随着我在文本框中键入而减少 - I want to type 140 characters in asp.net textbox with multiline characters will be decreased as i type in textbox 如何在Visual Basic中控制文本框类型加倍? - how to control textbox type to double in visual basic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM