How to save the as I type in a textbox - vb.net

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. 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 :(
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?

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

Related

How can I save the content of a rich text box even when the form closes?

This is my first post here, so please don't judge me if I write something wrong ^^
Anyways, I've recently run into an issue with richtextboxes in Visual Basic .NET and WinForms.
Let's say I have a Main form and a Log form. The log form contains a richtextbox which functions as a log. From the main form I'm writing text to the log and I also format the lines, so they have different colors (blue for information, red for error).
Unfortunately, whenever I close and reopen the log form all text that has been written to it is lost.
I've tried saving it to a text file, but that doesn't save the colors of the text.
Is there any way I can save the text and the colors of the lines even when the form closes?
Here's what the excellent suggestion from Shrotter might look like:
Public Class Form1
Private RtfPath As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim folder As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
RtfPath = System.IO.Path.Combine(folder, "RtbData.rtf")
If System.IO.File.Exists(RtfPath) Then
RichTextBox1.LoadFile(RtfPath)
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
RichTextBox1.SaveFile(RtfPath)
End Sub
End Class
Of course, you should always wrap the loading/saving of the file in a Try/Catch block in case anything goes wrong.

Not allow the creation of new lines but textbox is still multiline with wordwarp

I wrote some code to check when the user inputs a new line and then deletes the line. All of that is happening on the TextChanged event on a Textbox control and the WordWarp property set to True.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = TextBox1.Text.Replace(vbCr, "")
TextBox1.Text = TextBox1.Text.Replace(vbLf, "")
End Sub
The code kinda works, problem is that when the user inputs a new line on my Textbox control the text cursor gets sent back to the first character.
Is there any solution to this?

How to enable overwrite text in a textbox?

I'm making a program which requires a login system. What I want to do is have a textbox which contains the word 'Username' and when the user clicks it, the 'Username' text is overwritten. For example, Spotify use it on their login screen:
My question is, how do I do this?
Set the Text property of the TextBox that you are using for the username to the string "Username". Then, in the TextBox's Click event, change it to a blank string. Like so:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
TextBox1.Text = ""
End Sub
Edit:
As #LarsTech mentioned, this does not address if the user tabs into the TextBox. If you wanted to account for that too, use the TextBox's Enter event instead:
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
TextBox1.Text = ""
End Sub
I agree, using Textbox1.Enter is the easiest solution. On top you can also catch the case of no text entered via TextBox1.Leave like that
Private Sub TextBoxLeaveHandle() Handles TextBox1.Leave
If TextBox1.Text = "" Then
TextBox1.Text = "Username"
End If
End Sub
Sometimes it can also be useful to use the TextBox.SelectAll() function as it not immediately remove the entire text but (obviously from the name) select the entire text so you can overwrite it with your first keypress.

For each new line do something

i have a textbox called txtChat and i want that for every new line the textbox2 and textbox3 gets refreshed.. i dont know how to make thits "new line thing" or which commmand is the right one. Im looking for something like
For Each NewLine refresh textbox2&3
If you wish to monitor the input into a textbox as the user types you could use the keypress event. The parameter e.KeyChar then corresponds to the keyboard key pressed by the user. If it is equal to vbCr you can conclude the user pressed enter. Your code should probably look something like this:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = vbCr Then
'Refresh textboxes 2 and 3
End If
End Sub
You would get into trouble though if, for example, the user copy-pasted something in, as the user will then not have entered a newline character.
As Dries says, you could use the textchange event, in which case you would have to check what the last character entered into the textbox is. Slightly confusingly, in this case, you would have to check if the text ends with a vbCrLf.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.EndsWith(vbCrLf) Then
'Refresh textboxes 2 and 3
End If
End Sub
Take a look at the TextChange Event.
(https://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged(v=vs.110).aspx)
It should point out what you're looking for (as far as I understand).

What are some good options for providing help on a form with minimal controls?

I have a few textboxes that will require user input, and I want to add something that the user can revert to in case they forgot the proper syntax required for input.
For example, if in textbox1 the input MUST ALWAYS be something like "bSAMPLE" or "bSAMPLE2" I want to show the user (i.e., bSAMPLE) so that they may see the proper syntax required.
I know I can add a button and show a messagebox, but that just seems too much for something this simple, as for a tooltip, I'm not sure if the user might hover long enough to see the example. Any tips?
Did a quick test on some code for the tooltip method, and this works for me:
'In your form's general declarations:
Dim tt As New ToolTip
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter 'list out all your text boxes here
Dim txtbx As TextBox = sender, dispText As String
Select Case txtbx.Name
Case TextBox1.Name
dispText = "How to use text box 1"
Case TextBox2.Name
dispText = "How to use text box 2"
'flesh out the text for each text box
End Select
tt.Show(dispText, txtbx)
End Sub
Private Sub TextBox_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave, TextBox2.Leave 'finish the list as above
tt.Hide()
End Sub