Not allow the creation of new lines but textbox is still multiline with wordwarp - vb.net

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?

Related

vb.net ComboBox Text Changing When Left

I'm having an issue with my ComboBoxes whereby if I type into it to get a value & then tab out the Text changes to the first item in the list with the first letter typed.
I have:
AutoCompleteMode set to SuggestAppend
AutoCompleteSource set to ListItems
DropDownStyle set to DropDownList
I add the items for the ComboBox in the Load event of the Form the ComboBox is on.
e.g. the below is code from a Load event where I populate a ComboBox that I have set up as below.
`Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")`
After running the code, if I select the ComboBox1 & type in common - common is selected in ComboBox1 but if I leave ComboBox1 the Text reverts to combo.
It gets a bit stranger as if I user the below code in the ComboBox1_Leave event procedure it throws common:
MsgBox(ComboBox1.Text)
I've also tried assigning the value of Text to a string in the ComboBox1_KeyUp event procedure & then assign that to ComboBox1.Text in the ComboBox1_Leave event procedure but that doesn't do anything.
If I put a the above MsgBox code before assigning the strings value to ComboBox1.Text then the Text value does revert to Common but this is isn't a practical solution.
I've also noticed that if I hit Enter before hitting tab it retains the correct value but again I'm don't think this is a particularly practical solution.
Does anyone have any idea what's going on here & how I can fix it?
It is absolutely necessary to have the DropDownStyle set to DropDownList?
Because if you set DropDownStyle to DropDown the selected value will be retained when you press tab or lose the focus.
If it's absolutely necessary to have it that way, you could try this.
Public Class Form2
Dim selectedTextForCombo As String = ""
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")
End Sub
Private Sub ComboBox1_LostFocus(sender As Object, e As System.EventArgs) Handles ComboBox1.LostFocus
ComboBox1.SelectedItem = selectedTextForCombo
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
selectedTextForCombo = ComboBox1.Text
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
End Class
Warning:
This example works with the tab action.
If the users writes something that doesn't exist like "commun" the
selected value will end up being the visually selected value, in this
case: "common"

mandatory field in VB and save Button

if somebody can help me in VB coding to show required field to be mandatory when I click save Button and the function of saving should not be work till all required field to be filled
(please visual basic codes not other )
You can use a simple If function that occurs when the button is pressed.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
Msgbox("A field is missing")
Exit Sub 'To avoid saving, escape the procedure
Else
' code to save goes here
End If
End Sub
Here, I assume that TextBox1 and TextBox2 are the fields you want and Button1 is the save button.

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.

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

Toggle the masking and unmasking of a TextBox using a CheckBox

I have a TextBox, which has a CheckBox operation to mask the containing text. This works with the following code:
Private Sub CheckBox2_Checked(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox2.CheckedChanged
TextBox14.PasswordChar = "*"
End Sub
It works well, but I want to also be able to uncheck theCheckBox and then have the recognizable text return. How can I achieve this?
The docos actually state:
The character used to mask characters entered in a single-line TextBox
control. Set the value of this property to 0 (character value) if you
do not want the control to mask characters as they are typed. Equals 0
(character value) by default.
Found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.passwordchar(v=vs.110).aspx
In VB.NET, that would be easiest done by setting PasswordChar to vbNullChar.
You can do so by simply setting the PasswordChar property back to a null character, like this:
Private Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
TextBox14.PasswordChar = "*"c
Else
TextBox14.PasswordChar = ControlChars.NullChar
End If
End Sub
The CheckedChanged event occurs every time the Checked property changes. So, when the user unchecks the CheckBox, it will raise that event too, so you need to check to see whether or not the control is currently checked.
I found just toggling the password character wasn't enough. In my case I was masking a connection string. With the lack of spaces in my text I had an issue going back and forth. My text would be cut off and wasn't wrapping properly.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
Dim beforeText As String = TextBox1.Text
TextBox1.Text = ""
TextBox1.PasswordChar = IIf(CheckBox1.Checked, Convert.ToChar(0), "*"c)
TextBox1.Text = beforeText
End Sub
I imagine if you used a font like Console this would not be a problem as all character widths are constant.