I use AutoCompleteMode set to AutoCompleteMode.Append in TextBox control.
After the auto-complete feature appends text to the existing and selects it, if I press Ctrl+A to select all the text, the textBox is cleared.
It happens with any textbox, you can test it by your own.
Do you know how to fix it?
It can be annoying, if you use this shortcut, that is.
That combination is intercepted along the way and misbehaves when AutoCompleteMode = AutoCompleteMode.Append. You can see it when a word in the list is partially selected; press ENTER (the CTRL+A equivalent here) to select it all, then press END and BACKSPACE: the text Selection wasn't actually cleared, the last letter is magically re-selected instead of deleted.
As a simple workaround, you can suppress the key presses when you detect that combination and use SelectAll() to select the text yourself:
(as noted, pressing Enter would to the same thing)
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.Control AndAlso e.KeyCode = Keys.A Then
e.SuppressKeyPress = True
TextBox1.SelectAll()
End If
End Sub
Related
I'm not sure if it is possible, but I would like to know if a listbox can be updated by adding or removing text by typing that text into a textbox without using a button event or some type of click event. I've tried using the text_changed event but it inserts the text as I type so I am unable to type an entire string and then move that into a listbox as a whole string.
What I am trying to do is scan a magnetic ID card through a reader and have it insert the data from that card into a listbox and when I scan the same card again, it will remove the data. This is for an employee logging system.
Thanks.
With the following code, if you type a string in TextBox1 and hit Enter, we check to see if the string already exists in ListBox1. If so, the string is removed from the ListBox, otherwise it is added. Then TextBox1 is cleared.
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = vbCr Then
If ListBox1.Items.Contains(TextBox1.Text) Then
ListBox1.Items.Remove(TextBox1.Text)
Else
ListBox1.Items.Add(TextBox1.Text)
End If
TextBox1.Clear()
e.Handled = True
End If
End Sub
I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx
How can I tell if the Tab key has been pressed inside a certain textbox.
I tried enabling the AcceptsTab property to true and creating a function that handles texstboxname.KeyPress event, but when I tab while inside it it just tabs out of the box, instead of hitting my event. (normal keys fire my event, but tab never even fires it)
The Multiline property needs to be true as well.
From MSDN (emphasis in bold)
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.
so set the Multiline property to true.
As MiniTech also pointed out, KeyDown is easier to handle since it provides you with an e.KeyCode property whereas the KeyPress event only provides an e.KeyChar property.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
'user pressed the Tab key...
End If
End Sub
It seems that you can either Additionally
Enable the multiline property on the TextBox
You can override IsInputKey before the Keydown or Keypress event first.
To do that you could derive a class from TextBox and ovverride it there, ( as Hans Passat suggested)
or you can also handle the PreviewKeyDown event and ovverride the IsInputKey to true in there.
I went with the second part of option two, so that I wouldn't have to worry about the multiline property being on and could Leave my code with the keypress event
Private Sub txtEntryBar_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtEntryBar.PreviewKeyDown
If e.KeyData = Keys.Tab Then
e.IsInputKey = True
End If
End Sub
Ref: http://www.vbforums.com/showthread.php?670904-Detecting-when-the-tab-key-is-pressed-in-a-textbox&p=4124911&viewfull=1#post4124911
I am working on a little story type thing in visual basic. Nothing too complicated but i have run into a problem. I am trying to make a pause feature. I have it all working to when i press escape, the form goes into a menu type thing. The only problem is that it only works once. The first press works, but if i hit continue, it continues from where i left off. But if i press it again, nothing happens. Here is the code i am using:
Private Sub Story_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Text.Hide()
Background.Hide
ContinueButton.Show
OptionsButton.Show
End If
End Sub
Is there any way for me to make this work more than just once? I am using a KeyDown event in a private sub.
Hope this made sense, but thanks for any help guys!
You're using Form.KeyPreview = true to capture the form keystrokes. I think Text.Hide() or one of Show operations could be the problem such as getting the TabStop's out of order or leaving a gap in the tab sequence.
Try resetting the controls or using SendKeys to reset the tab sequence index (to the approriate a control to have focus), For example, Tab forward and Alt Tab back:
SendKeys.Send("{TAB}");
or
SendKeys.Send("%{TAB}");
Ref:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
Using a class level boolean as toggle should work:
Dim Hide As Boolean = False
Private Sub Story_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Hide = Not Hide
If Hide Then
Text.Hide()
Background.Hide
ContinueButton.Show
OptionsButton.Show
Else
Text.Show()
Background.Show
ContinueButton.Hide
OptionsButton.Hide
End If
End Sub
If nothing else helps, you can always have a private class-level variable like _processingKeyDown, set to True at handler start, and then to False when done. The first line in the handler would be:
If _processingKeyDown Then Return
How could i validate a textbox in vb.net, so that it gives error message if i enter anything apart from alphabets
You can check the text string, i.e., textbox1.text, to make sure it has nothing besides alphabet characters in the .Leave event. This will catch an error when the user tabs to the next control, for example. You can do this using a regular expression (import System.Text.RegularExpressions for this example), or you can check the text "manually."
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
If Not Regex.Match(TextBox1.Text, "^[a-z]*$", RegexOptions.IgnoreCase).Success Then
MsgBox("Please enter alpha text only.")
TextBox1.Focus()
End If
End Sub
If you want to stop the user as soon as a non-alpha key is pressed, you can use the TextChanged event instead of the .Leave event.
CustomFieldValidator with a regex.
If it's a standard textbox in a WinForms app you can validate every typed character by handling the KeyPressed event and have the following code in the event handler:
e.Handled = Not Char.IsLetter(e.KeyChar)
The user could still use the mouse to paste something in there though so you might need to handle that as well.
Another option is to handle the Validating event and if the textbox contains any non alphabetic characters you set e.Cancel to true.