For each new line do something - vb.net

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).

Related

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 create something similar to google style currency converter with VB.NET

Ok I have a bit of a weird question here. I have a program similar to a currency converter (it performs a mathematical function in order to produce a value to go in another textbox). What I want it to be able to do is identify the last textbox that you edited (there are 4) and then update the rest based on what you have inputted, the user then must be able to change a different textbox to change all of them.
If anyone can get me started on how to do it or even some sample code that would be much appreciated, thanks!
Sorry if I'm not making sense, just have a look at the google currency converter and think that with two more editable boxes.
This might be what you want if I understand you correctly.
In the form class, you have a variable called lastTextBoxChangedName which keeps track of which text box was the last to be edited.
Next there is an event handler which will fire when any of the four TextBoxes are changed. This merely updates lastTextBoxChangedName.
When you have finihed editing a textbox, and tab to the next one or click on something that causes a TextBox to lose input focus, the next event handler executes. This looks at lastTextBoxChangedName to see which was the last edited TextBox and you can insert your update code to replace the comments in the Select Case block.
Public Class Form1
Dim lastTextBoxChangedName As String
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
lastTextBoxChangedName = sender.name
End Sub
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus, TextBox4.LostFocus
updateTextBoxes()
End Sub
Private Sub updateTextBoxes()
Select Case lastTextBoxChangedName
Case "TextBox1"
'do updates appropriate to textbox1 changed
Case "TextBox2"
'do updates appropriate to textbox2 changed
Case "TextBox3"
'do updates appropriate to textbox3 changed
Case "TextBox4"
'do updates appropriate to textbox4 changed
End Select
End Sub
End Class
However, if you already have separate event handlers for each TextBox, don't add that first event handler for TextBox_TextChanged, just add the line ..
lastTextBoxChangedName = sender.name
into each handler.

how to set minimum limit of a textbox vb.net

I need to set a minimum length for a password that can be used in a textbox and then set a label which will say if it meets the minimum number of characters required. I know how to set the limit of characters, what I can't do is the part where it will show in a label as soon as I leave the textbox. I was thinking I need to use an event, like maybe Leave or LostFocus, but it's not working. Please help :(
Ok, There are plenty of ways to do what you want to achieve. I personally like to a separate subroutine; if you need to change one thing, you wont have to edit every single event that has the same codeFrom what I can understand, something like this should help get you on your way. Basically, we just setup a subroutine that will check to see if textbox1.text's length is more than five and we trigger the subroutine by using events such as a button click of if the textbox is clicked off.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''save button
checkPassword(TextBox1.Text)
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
checkPassword(TextBox1.Text)
End Sub
Private Sub checkPassword(password As String)
If Not password.Length > 5 Then
Label1.Text = "The password must be more than 5 charcharacters"
TextBox1.Clear()
Else
Label1.Text = "Password accepted"
End If
End Sub

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. 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

validate a textbox in vb.net

How could i validate a textbox in vb.net, so that it gives error message if i enter anything apart from alphabets
You can check the text string, i.e., textbox1.text, to make sure it has nothing besides alphabet characters in the .Leave event. This will catch an error when the user tabs to the next control, for example. You can do this using a regular expression (import System.Text.RegularExpressions for this example), or you can check the text "manually."
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
If Not Regex.Match(TextBox1.Text, "^[a-z]*$", RegexOptions.IgnoreCase).Success Then
MsgBox("Please enter alpha text only.")
TextBox1.Focus()
End If
End Sub
If you want to stop the user as soon as a non-alpha key is pressed, you can use the TextChanged event instead of the .Leave event.
CustomFieldValidator with a regex.
If it's a standard textbox in a WinForms app you can validate every typed character by handling the KeyPressed event and have the following code in the event handler:
e.Handled = Not Char.IsLetter(e.KeyChar)
The user could still use the mouse to paste something in there though so you might need to handle that as well.
Another option is to handle the Validating event and if the textbox contains any non alphabetic characters you set e.Cancel to true.