I have a masked textbox in VB.NET to input a time value. I've tried the masks 90:00, 00:00, and ##:##. The first time a value is entered in the box, it inputs fine. I later clear the text with
mskTime.Text = ""
I've also tried
mskTime.Clear()
mskTime.ResetText()
The issue is after the text is cleared and a new time is entered, the first character typed is deleted. More precisely, when I add the time 12:34, I type the 1, it appears in the first character slot. I then press 2, and the 1 disappears and the 2 appears in the second character slot. The character does not disappear when you go to type it again to fix it.
Has anyone seen this issue or know why the first character disappears?
Problem found:
I had a KeyDown function to handle the user pressing enter in the time field:
If e.KeyCode = Keys.Enter Then
Schedule()
e.SuppressKeyPress = True
End If
Commenting out
e.SuppressKeyPress = True
solves the issue. I originally used that to prevent Windows from making a ding sound each time enter was pressed.
I had the exact same problem in C#. The input worked fine until the MaskedTextBox was cleared programmatically. After that the first character entered in one of the MaskedTextBoxes was deleted after a second character has been entered. Turned out that the problem lied within the KeyDown event. I was waiting for the Enter-Key there, did some stuff and cleared the TextBoxes. After that, the problem occured.
I solved it now by replacing the KeyDown-Event with a KeyPress-Event.
Here is what I ended up using to catch the Enter Key and move to the next field and not screw up the masked textbox when I come back to it.
Douglas
private void TxtLocID_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
}
Related
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
Im working on a code in VB.NET wherein I scan a barcode(21 digit input) into a text box, and immediately, it’s supposed to show some data. I get the barcode input via a text box, which is supposed to contain the entire 21 digits, and I’m using the function textChanged() to see if the text box value has updated.
The problem is, the code runs after only the first value of the barcode gets read, and the rest of the values don’t reflect in the text box at all.
Is there any way I can get the entire 21 digits and then pursue the remainder of the code (note: it has to be automatic after the 21 digits are entered)
Code:
Private sub number_textChanged(sender as Object, E as EventArgs) handles number.TextChanged
If number.text.length =21 then
Value=number.text
End if
Send your textChanged() code, in that you can count then entered valued once reached 21 count then you can show 21 digit barcode. Or you can use textbox mouseout event, in that it will wait until you enter value fully, when you mouseout from textbox, it will trigger event.
Otherwise you can use javascript to get textbox value count and show after 21 digit.
function checklength() {
var len = document.getElementById("TextBox18").value.length;
if (len == 21) {
alert(document.getElementById("TextBox18").value);
}
}
and Textbox in source code add onkeyup event like this
<asp:TextBox ID="TextBox18" onKeyUp="checklenght()" runat="server"></asp:TextBox>
You can store that value in hidden value use in server side.
I'm working in VB.NET, Visual Studio 2017. I have a combobox with DropdownStyle = Dropdown. If the user types something invalid in the text field of the combobox (invalid means it doesn't match a value in the combobox) then we display a message and then return focus to the text field with the text highlighted, so they can see what they typed. The message is displayed from the Validating event.
This works fine if they don't open the dropdown. If they do open the dropdown and, while it is open, they type in an invalid entry, the message displays but the entry they typed clears.
I have put in debug statements to see what events are firing. Before the message displays, I get a DropDownClosed (text is still there), then a TextChanged (text is still there), then a second TextChanged (text is now empty). I think something about losing focus to display the message may be triggering something, but I can't figure out what.
I can save off the text and then replace it after the message displays, but while the message is up, the text field is blank.
Any ideas?
While I still don't understand the series of events that causes the problem, I found a solution. Just before the message is displayed from within the Validating event of the combobox, I set focus to the combobox. I'm guessing that tabbing away from the invalid entry loses focus, and because the dropdown is open, it then closes which somehow clears the entry. Go figure.
Here is the code from the validating event:
If testInList Then
ResultTextBox.Enabled = True
Else
TestComboBox.Focus
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
The "TestComboBox.Focus" line has fixed the issue of the text disappearing. However, if the user's invalid entry is a partial match for an item in the dropdown, then the text in the text field of the combobox is updated to that item, so now it looks like they typed a valid entry but are getting an message that it's invalid. For example, if there is an entry in the dropdown of "NAMC" and they type "NA" (with the dropdown open) and tab away, the entry changes to "NAMC".
Any ideas on how to prevent that?
p.s. The AutocompleteMode is set to None.
So again, I don't understand the sequence of Events that causes the issue, but I've found something that works. If I move the new "TestCombobox.Focus" line outside of the If condition, the text remains in the text field before, during and after the message displays:
TestComboBox.Focus()
testInList = TestEntryValid()
If testInList Then
EnableResultFields(True)
Else
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
This moves the .Focus before TestEntryValid, but I don't see anything in there that would trigger any events:
Private Function TestEntryValid() As Boolean
Dim item As String = TestComboBox.Text
Dim validTest As Boolean = False
If item.Length > 0 Then
Dim index As Integer = TestComboBox.FindStringExact(item)
If index > -1 Then
validTest = True
End If
End If
Return validTest
End Function
If anyone can explain why this works, I'd love to know. If not, thanks to everyone who responded!
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.
I'm trying to make a text box where you can enter a page number and the form will change. I'm using a textbox to allow the user to enter a page number and I want the form to change when the are no longer actively editing the textbox, the problem is that the textbox is the only editable control in the form so it never loses focus and the validating event never fires. Is there any way to do this?
As matzone said use the keypress but not for the page number keys.
For example, say your user wants to goto page "51", then the user can type 51 and press the return key.
Have the events fire on the return key,
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
//13 is the keycode for Return
If (e.KeyCode == 13)
{
//SuperAweomseEventGoesHere
}
}
Use a button as was mentioned to indicate the user wants to load a new page.
Use the validating event to make sure the user entered a valid value.
Alternatively:
If your page numbers are all double digits you could also use the TextChanged event and keep checking if the text value equals a page number. Having at least 2 digits is important since 1 will load a page and the user won't be able to load 10
Another alternative:
Use a NumberUpDown control or a combobox so the user is restricted to the proper values