Remove last character from textbox on button click - vb.net

I have a textbox, textbox1, whose ReadOnly property is set to true. It is somewhat like an on-screen keyboard. When the user presses a button, I can add characters by using:
Textbox1.Text = Textbox1.Text & A
But, when the user wants to remove the text, which should work exactly like the Backspace key, the last character should be removed from the textbox.
I am ready to give up normal textbox and use richtextbox if there is any way ahead with the rich one.
How to remove last character of a textbox with a button?

The previously suggested SubString method is what I would have posted too. However, there is an alternative. The Remove method works by removing every character after the given index. For instance:
TextBox1.Text = Textbox1.Text.Remove(TextBox1.Text.Length - 1)

You can use the SubString method to get all but the last character, for instance:
TextBox1.Text = TextBox1.Text.SubString(0, TextBox1.Text.Length - 1)

The above functions such as Substring and Remove are not executed in Visual Basic 6 (or earlier versions). I recommend to use Mid function.
For instance,in this example last character of Hello is deleted and rest of word is saved in Text1
Text1=Mid("Hello",1, Len("Hello") - 1)
and result is Text1=Hell

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

I want the user to insert data in a TextBox only in a specific manner

In a car number the format of writing will be same. For example, the Car Number is Written only in the manner: RJ20CF2091.
Now consider it from left to right. In the first 2 places, only alphabet should come, the next two (3rd and 4th place) should be digits only, the 5th & 6th position, only letters and the last 4 (7th, 8th, 9th & 10th) only digit.
The data in the TextBox should be exactly of size 10.
Using a MaskedTextBox, you can provide a Mask that allows only specific sets of characters in defined positions.
Since your Input is: 2 Letters + 2 Numbers + 2 Letters + 4 Numbers, this can be expressed, using the mask elements described in the control's MaskedTextBox.Mask property, as: LL00LL0000.
Note that you also need to set the AsciiOnly property to True.
Since the input appears to only use capitol letters, you can use the KeyPress event to automatically convert to uppercase the user input:
Private Sub MaskedTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MaskedTextBox1.KeyPress
e.KeyChar = Char.ToUpper(e.KeyChar)
End Sub
First of all you must define, when the validation should take place:
When textbox losing the focus?
For each typed letter?
When the user hits a button, e.g. "OK"?
Then you need to define, what shoudl happen if the user input is not valid:
Make something red?
Show a exclamantion mark or another logo somewhere?
Deactivate other controls?
For your actual problem, you can use Regluar expressions like this:
bool isValid = System.Text.RegularExpressions.Regex.IsMatch("RJ20CF2091",#"^[A-Za-z]{2}[0-9]{2}[A-Za-z]{2}[0-9]{4}");
(see https://regex101.com/r/4VPBXx/1)

Populating a textbox, if another textbox is populated

i've got two textboxes in my form.
According to title, when I write something in one textbox (a random one), i need that at the same time, in the other textbox a text (given in the code) appears.
1 letter for 1 letter
1)Example with random text:
1 textbox ) How are you?
2 textbox) Let's just c
2)Example with random text:
1 textbox ) What is the aim of the project?
2 textbox) Let's just chill out for a mome
thanks so much
You will need to determine which responses you would like to which questions. However, the tackle to letter for letter problem. I would use the substring method and get the length of the current text.
Dim Response as String = "Hello World!"
Private Sub Text(ByVal..) Handles Textbox1.changed
Textbox2.text = Response.Substring(0, Textbox1.text.length)
End Sub
As the text changes in the textbox you are typing in, this will output the response corresponding to the length of the text input. Since the event is textbox.changed, each character entered will update the second textbox
You need to use an event based code here. I would use a loop per stroke of the key and make the event "Key Press." Write the code onto the textbox you are inserting data into.

Automatically send barcode scanner input to textbox VB.Net / Winforms

I've reviewed dozens of options/solutions on this and I just can't get it to work.
Simply put, I have a VB.Net Winform that has a textbox where a user can manually type in text or they can use a USB connected barcode scanner (that simulates a keyboard) to capture a UPC.
What I'm trying to do is get the barcode input to get entered into the textbox regardless of which control has the current focus.
I have set the KeyPreview property of the form to True.
I then added some code to the frmMain_Keypress event as follows:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
End If
Very simple...and it works, sort of...
If txtSearch already has the focus, the entire barcode/UPC gets entered into the text box.
However, if another control has the focus, every character of the barcode/UPC EXCEPT THE FIRST CHARACTER gets entered into the text box. It always strips off the first character.
I placed some debug statements in the above code to see if the initial character was being read at all and it is being read...just not sent to the text box.
I've seen so many other REALLY complicated solutions to barcode scanning and it seems like I'm really close with something really simple, but, obviously it won't work if it strips the leading character.
Hopefully I'm missing something very obvious.
Change the code in your KeyPress event to:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
txtSearch.Text = e.KeyChar.ToString
txtSearch.SelectionStart = txtSearch.Text.Length
e.Handled = True
End If
That way you capture the first key that comes in.

Create masked textbox but hide masked characters when empty

I want to validate text taken from a textbox and display it in a specific format.
I have tried using a MaskedTextBox, but when the textbox is empty it shows the empty blank lines (underscores) in the text box.
How can I avoid that and show the masked textbox like a simple empty (still masked) textbox?
Also, I want data like csc-(somenumber). Can I add some random number automatically after 'csc-' characters?
The reason the masked text box is showing a blank line is because underscore "_" is the default prompt character for a masked text box. You have two options for changing this.
If you want the prompt to be visible while the user is editing the text but hidden otherwise, set the HidePromptOnLeave property to true.
MaskedTextBox1.HidePromptOnLeave = True
If you never want to have the underscore as the prompt character, you can change the PromptChar property to be a space " ". You cannot make the PromptChar nothing, the field must have a value.
MaskedTextBox1.PromptChar = " "
For your textbox, use the MaskedTextBox class.
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx
For getting a random number
Dim s = "csc-" & New Random().Next(1000, 10000).ToString