Textbox: missing first character typed on the TextBox - vb.net

I have 2 text boxes to accept info but only one of them is need at the same time (One is for selection of the first letter and the other is for selection of any part of the name)
So I need to delete the text of the other textbox when the user start typing on the textbox
Something like this work:
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox1.Text = ""
End Sub
But there's a problem, the first character the user type is not displayed, I think is because the first character typed is used to launch the event.
Example:
TextBox1.Text = "A"
when the user click on TextBox2 and type michael the "m" is lost
I don´t want to use the "GotFocus" event because a simple TAB pulsed to jump from TextBox1 to another control can delete the text on TextBox1 when the user stop on TextBox2, even if he don´t want to type anything there
How can I manage this mistake?

Right answer:
From #Plutonix
"No user is going to expect this behavior and you will go mad trying to divine when to execute and when not to. A better thing might be to select the text on the Enter event - at least that has been seen before by users"

Related

Determine initial text selection in DataGridView Cell

A little question on use of DataGridView.
When I enter a Cell, text is all selected (in blue in the example). But I want to set the caret position at the right side of the text, for my user can delete the last character without clicking twice to place caret.
Have you some idea, because I don't find the good property...
I try to change behaviour when I enter in cell (edit with F2, in simple click), but test is always all select
Use DataGridView1.CellClick event and BeginEdit to allow casting Cell in textbox. After that set cursor position using textbox.select (from,Length) :
Private Sub cellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1.BeginEdit(False)
Dim tbCell As TextBox = CType(DataGridView1.EditingControl, TextBox)
tbCell.Select(tbCell.Text.Length, 0)
End Sub
The functioning of DataGridView has always been a problem for me.
tuyau2poil, your answer was perfect for my first question. I integred the code in DataGridView_CellDoubleClick and it's OK. Thousand of thanks
For my second question, I think the way is on Jimy anwser:
Now I search, when I select the cell with 1 click and press "Del", to delete the last character. That works, but when I type a new character + Enter, that delete all the cell for write only the new charactere.
This is for my users have to modify only the last number of the serial number enter in cell...
"21000_001" to become "21000_002" with "Del" + key press "2" + Enter .
Actually I've got "21000_001" then "2"
I think solution is arount of DataGridView_EditingControlShowing
Thanks

How can I make a textbox perform a backspace when I'm Not typing in it VB.NET

I'm making a hex keyboard converter with two TextBoxes: TextBox1 for typing in the key you want to convert, and TextBox2 for displaying what you converted.
So far I have gotten it to convert as I typed, but when I press Backspace, TextBox1 will erase the key but TextBox2 will remain with the same converted code, meaning I have to go to TextBox2 to erase it manually.
Is there a way I can make it so that when I press Backspace while typing in TextBox1, the TextBox2 will go back as well? And is there a way I can tell it how many spaces to go back?
It sounds like you are building the string by capturing each key. But couldn't you just convert the entire contents of TextBox1 and put it into TextBox2?
This is another way of doing it. See if it works
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox2.Text = shiftCharacters(TextBox1.Text)
End Sub
Private Function shiftCharacters(input As String) As String
Return String.Join("", input.Select(Function(c) ChrW(AscW(c) + 1)))
End Function
Since I don't know how you convert each character, I just shifted each character up by 1.
Here are some images
Then I deleted 123 with backspace.
Is this going to work?

VB.net strange textbox behavior

A customer of mine is having a problem with a text box. When he clicks on the string in the text box the cursor always jumps to the end of the string. This is a standard VB.net 2005 text box with multi-line true. On my development machine it works correctly. I click in the middle of a string and can edit where I click. Can anyone suggest what is wrong?
He has run the program both under terminal server and locally on his lap top and has the same problem.
TIA,
John
Is it possible to observe the user? For example, the user might be pressing shift-tab when they are past the text-box, and refering to that as "clicking" the text box.
You can always force behavior like:
Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = 0
End Sub

I have a "bug/glitch" in my vb program need assistance

I have a program that replaces every letter of the alphabet with something new inside the same textbox by clicking a button then buy clicking the button it translates it back to original text.
However when i run this program, yes it does work as needed but not 100%
Because after i click the button to translate, (yes it shows translated)
If delete the translated text and type something new, it goes back to the old text.
(this does not happen if i click the button again, it shows the orignal text. But if i type something new it will translate it.)
Any suggestions?
This is a continuation of your other question. You will need to reset your Boolean variable when you type in your TextBox i.e.
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
bConverted = False
End Sub

finding a solution for find string in listbox

I has 4 item on my form.........
tow listbox ,one button and one text box
I has a listbox 'A' with many items.....
i need a item in a listbox 'B' from listbox 'A'
steps are as follow.....that i like to performe...........
1)enter a word or character in a textbox
2)press a button
3)the list appear in listbox 'B'.......that is character or string start in listbox 'A' that is we write in textbox (for matching)
i need a help in which a item that can be in listbox 'B' is getting for listbox 'A'
that is Starting string or character we enter in the text box.
please try to solve me out..........
Not quite sure I follow. Using the text box' Changed event would be a good trigger instead of a button. Just iterate the list items and check for a match with String.StartsWith. For example:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox2.Items.Clear()
If TextBox1.Text.Length > 0 Then
For index As Integer = 0 To ListBox1.Items.Count - 1
Dim txt = ListBox1.Items(index).ToString()
If txt.StartsWith(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) Then
ListBox2.Items.Add(txt)
End If
Next
End If
End Sub
I don't have an IDE in front of me, and it's been a while since I've done WinForms development, so I may not have the exact event name or other stuff, but you get the idea. This also means my code will be in C# since I'm more familiar with that, but you should be able to find the VB equivalent.
You'll want to bind to the proper event on the text box first. Maybe the KeyPress or KeyUp event? Or TextChanged? You want one that fires any time text changes in the text box. In that event, you'll loop through the items in listbox A and compare their values to the text in the text box. Basic string comparison is all that's needed, if there's a .StartsWith() or something of that nature, otherwise some basic use of .Substring() will do fine (based on the length of the string in the text box).
The loop would likely be something along the lines of:
listboxA.Items.ForEach(i =>
{if (i.StartsWith(textboxA.Text)) listboxB.Items.Add(i);});
Or...
foreach (var i in listboxA.Items)
if (i.StartsWith(textBoxA.Text))
listboxB.Items.Add(i);
Like I said, this is all off the top of my head, so the code may not be exact. But hopefully you get the idea.