How can I make a textbox perform a backspace when I'm Not typing in it VB.NET - 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?

Related

Overload Clipboard.SetText VB.net 3.5 (or how to copy 3-4 text box conents at same time)

I have a form where I require user input and when the user presses the "generate" key it will copy all the data from the relevant text boxes to the clipboard for them to enter the data in an external application.
At the moment i can only find out how to get the function to copy one text box at a time, and not string them into a sentence.
Private Sub Notes_Click(sender As Object, e As EventArgs) Handles Notes.Click
My.Computer.Clipboard.SetText(TextBox1.Text, TextBox6.Text, TextBox5.Text, TextDataFormat.Text)
The problem is that the function can no be overloaded. Can someone please confirm the function i can use (or provide me a sample code) for how to copy all text box contents to the clipboard?
This is ALL on one form
The clipboard only accepts one text value at a time, so it's pretty clear why your overload won't work. If you need multiple values, concatenate them; if you need to keep them separately identifiable, put a separator character between them and parse them back out on the receiving end.
My.Computer.SetText(TextBox1.Text + TextBox2.Text + TextBox3.Text +...)

Textbox: missing first character typed on the TextBox

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"

Replace certain word in textbox with vb and retrieve whole string after

I'm looking to make an application that replaces a users selected word with underscores, the issue here is the fact that it does that well, however, it does not return the entire string but rather the underscores themselves.
tbText.Text = Replace(tbText.Text, tbText.SelectedText,
generateUnderscores(tbText.SelectedText), tbText.SelectionStart, 1)
Generateunderscores is a function i created that returns underscores depending on the number of letters in the selectedtext
tbText is the textbox, when a user highlights it I want this function to run. This will replace the selected text with underscores.
Notice how I make tbText.Text contain this, it then becomes ONLY underscores without the remainder of the text in the textbox.
How can I return the text in the text-box as-well as the underscores in it?
I've tried using a string replace however, the issue with that was it found multiple words instead of the ONE word I wanted removed (selected word)
Thanks.
when a user highlights it I want this function to run I dont know how you will do this part, because there is no TextSelected or SelectedTextChanged event. I used right mouse down. You could try to use Left Mouse Up, but that means the text is changed even if the user wants to made a mistake or wants to change whats selected.
Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs)
Handles TextBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right AndAlso
TextBox1.SelectedText.Length > 0 Then
TextBox1.SelectedText = MakeUnderScores(TextBox1.SelectedText.Length)
End If
End Sub
Function MakeUnderScores(n As Integer) As String
Return New String("_"c, n)
End Function
I am not sure that VB's Replace function wouldn't do the same as String.Replace

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.