Keep cursor out of all Textboxes on form - vb.net

I have a form with several buttons and TextBoxes. After the user clicks on a button, the text in one or more of the TextBoxes changes, and the cursor (edit: caret, not the mouse cursor) goes into a textbox. (This question helped, identifying that the TextBox with the lowest TabStop as being the one where it would land, and explaining how to avoid having text in that box selected when this happens.) But what I'd like to see is that the cursor (caret) stays out of all the TextBoxes unless the user decides he wants to put it there -- highlight & copy something, for instance. This wouldn't be common, but it could happen.
How can I make adjustments to the form or the TextBoxes so that the cursor (caret) doesn't show up in any textbox (so long as the user doesn't click inside a TextBox)?
Thanks!

If the code in your button's click event causes the focus to change to another control, you could update the last line of the click event, like this:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'Your code here
Button1.Focus() 'this last line of the sub sets the focus back to the button
End Sub

After some experimentation, it looks like setting the TabStop property of each TextBox to False will also prevent the cursor from moving into any of the TextBoxes on its own after a Refresh.
I suspect that sBarbacki's answer will work equally well, or probably better with a large number of controls that one wants to avoid getting the focus automatically.

Related

Is there anyway to not let user lost focus from winforms in vb.net?

Issue:
My Question may be weird but I need help with this.
I have this weird problem with my client PC and it all happens sometimes.
The application loses control when I close one form and opens another form.
Then he has to use the mouse and click on the form every time.
Similar but another issue.
In an MDI child form, what I do is make the user fill data in a bunch of textboxes and make the user click (or hit Enter Key) that adds that data in a datagridview row and clears all textbox and make focus on the first textbox.
At that time too the form loses focus and the user is not able to write anything. Though the textbox is still coloured (custom coloured textbox on got focus) but form loses control.
The Temporary solution:
-I make his PC restart and everything starts working fine.
-Even Closing the whole application won't help it. I have to restart the PC only.
MY Code between two forms:
Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click
....
Hide()
frmUser.Show()
End Sub
I use Hide to close 1st form else I use close() to switch between forms.
I even face this issue of losing focus in MDI child forms too.
I even made a custom textbox that changes its background colour when it gets focus (in gotfocus event) and reverse to white when loses focus (lost focus event), so when the issue starts the textbox remains coloured and even the cursor blinking inside but I cannot type in it. Again I have to click on the application form (anywhere) and I am able to type again. Until that form closed or changed in the application.
Please help with this weird issue. Thanks...

highlight / select text in numeric updown on enter

Using VB.net for a windows form application. I'm tired of always having to backspace the default '0' of a numeric updown control. What I would like to do is have the value selected automatically on enter, so that I can just type over it.
I've tried this:
Private Sub updown1_Enter(sender As Object, e As EventArgs) Handles updown1.Enter
Me.updown1.Select(0, updown1.Text.Length)
End Sub
I've used a break point to verify that it does indeed run, but it doesn't seem to do anything. What am I missing?
Your code is actually selecting the value as intended, it is just being undone by a mouse event almost instantaneously. When you click to enter a NumericUpDown, the events fire in the following order:
Enter
GotFocus
MouseDown
Click
MouseUp
As you probably know, in controls with text fields the native behavior places the cursor wherever you've clicked inside the textbox. This is what's causing your problem. You have selected the text at Enter, but then a bunch of mouse events come along and undo all your hard work. The obvious solution is to just use the MouseUp event since that's at the end of the list, but MouseUp will fire for anywhere you click inside the control so you'll have to decide if that behavior is acceptable for you.

Preventing a textbox from capturing the MouseWheel event

I have a textbox and when I scroll down my form with the mouse wheel and hit the textbox with the mouse, it stops scrolling.
Is there a way to avoid that ?
OK, If I understand you correctly, what you are searching is to keep mouse wheeling while passing on your TextBox. Is that correct ?
There is, I think, a way to achieve that. This code however has not been tested, so keep me informed if it worked.
Public Sub New()
InitializeComponents()
'Other inits here
AddHandler TextBox1.MouseWheel, AddressOf TBMouseWheel
End Sub
Private Sub TBMouseWheel(sender As Object, e As MouseEventArgs)
Me.OnMouseWheel(e)
End Sub
This way, when your textbox captures a MouseWheel event, it is passed on the form, which will handle it (I think). Sorry I don't have the opportunity to test it right now, but I believe this should do the trick.
If you just want the visual part, change the property "Cursor" of the textbox to "Arrow"
If you don't want a TextBox to be focused, you have a few solutions
Set the ReadOnly property to True
Nobody will be able to enter text in your TextBox, exept if you do it programatically. However, it will still be possible to click on it and if clicked you will see the blinking cursor (don't know the name in English). When the mover hovers it it will turn into a cursor.
This means this option allows the focus on the control, but it will not be possible to enter data.
Set the Enabled property to False
Again, it will not be possible to enter any data. Also, it will not be possible to click on it and the cursor will not change if you hover it.
This means this option disallows the focus on the control.
Put a Label instead
If nobody will ever be able to insert data in your TextBox, maybe it is better to put a Label there. If you choose option 1 or 2, it is because at some point you might allow user to change the text inside. But if it will only be modified by the program, Label is good enough.
Focus is never allowed on Labels.

How can I limit a textchanged event for a textbox to keyboard input only?

Please allow me to explain what I have and what I am trying to achieve.
I have a textbox (called txtb1) and a button under it (called btn_browse) on a winform in a vb.net project.
When the user clicks the button a folder browser dialog appears. The user selects his desired folder and when he/she clicks 'ok' the dialog closes and the path of the folder selected appears in the textbox. I also want to store that value in a variable to be used somewhere else(the value will be copied to an xml file when the user clicks 'apply' on the form, but this has no effect nor is related to my problem).
To achieve that I have the following code:
Public myVar As String
Private Sub btn_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_browse.Click
Dim f As New FolderBrowserDialog
If f.ShowDialog() = DialogResult.OK Then
txtb1.Text = f.SelectedPath
End If
myVar = txtb1.text
f.Dispose()
End Sub
This part works with no problems.
Now, what if the user either:
1- decides to enter the path manually rather than use the browse button. or,
2- after using the browse button and selecting the folder they decide to manually change the location
In trying to solve this I added a textchanged event to the textbox as follows:
Private Sub txtb1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtb1.TextChanged
myVar = txtb1.Text
End Sub
However, this is not working. Apparently, and I don't know if this is relevant, when the user selects the desired folder using the browse button the textchanged event is also triggered. and when I click on the textbox (to give it focus) and press any keyboard key the application simply stops responding.
So my questions are: am I going about this the right way? if my logic is flawed, could someone point me to how usually such a thing could be achieved? is it possible to limit the triggering events to only keyboard input as a way around this? I tried the keydown and keypress events but I am getting the freeze.
Set the TextBox.ReadOnly property to true and then set the backcolor to white and forecolor to black to look like a normal textbox but they can't edit it.
Then you have no need to worry about handling any events from the textbox like u are doing.
I think your solution is pretty simple. Just treat the textbox as a File upload control in web forms. Make it readonly. Don't let the users to edit the text. This solves two problems:
The user will always a select a folder using a known mechanism (clicking on button and seleting folder)
No need to use any variable since you can always get the location from the textbox.
HTH
Why do you need to store this value in an additional variable? So long as the textbox is visible to the user, it contains the definitive value and could be accessed directly. So, in this case you would have clicking the "Apply" button read the value from the text box instead of the variable, thus avoiding this problem with events altogether.

VB.NET: Dialog exits when enter pressed?

My problem seems to be quite simple, but it's not working the intuitive way.
I'm designing a Windows Forms Application, and there is a dialog that should NOT exit when the enter key is pressed, instead it has to validate data first, in case enter was pressed after changing the text of a ComboBox.
I've tried by telling it what to do on KeyPress event of the ComboBox if e is the Enter key:
Private Sub ComboBoxSizeChoose_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBoxSizeChoose.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
Try
TamanhoDaNovaFonte = Single.Parse(ComboBoxSizeChoose.Text)
Catch ex As Exception
Dim Dialogo2 As New Dialog2
Dialog2.ShowDialog()
ComboBoxSizeChoose.Text = TamanhoDaNovaFonte
End Try
End If
End Sub
But no success so far. When the Enter key is pressed, even with the ComboBox on focus, the whole dialog is closed, returning to the previous form. The validation is NOT done at all, and it has to be done before exiting. In fact, I don't even want to exit on the form's enter KeyPress, the only purpose of the enter key on the whole dialog is to validate the ComboBox (but only when in focus, for the sake of an intuitive UI).
I've also tried appending the validation to the KeyPress event of the whole dialog's form, if the key is Enter. NO SUCCESS! It's like my code wasn't there at all.
What should I do?
(Visual Studio 2008, VB.NET)
Make sure you don't have a Button on the dialog that is set to something other than DialogResult.None.
For example, if you have a button set to DialogResult.OK, it will act as the "default" button and close your form.
Although not your answer, I would recommend against using exceptions to control logic flow. That said, try Single.TryParse instead to make your flow less...well, exceptional.
To change the behavior you are seeing, change the dialog's AcceptButton from your Ok button to none. Changing that button's DialogResult to None doesn't keep the click event from firing, only from closing the dialog. Although the behavior may sound like something you desire, the result does not.