How can I limit a textchanged event for a textbox to keyboard input only? - vb.net

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.

Related

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.

Nothing works anymore after building project in vb.net

I was going to build my project when i noticed i didn't put an icon,
and since i couldn't access the icon value of the original form because i used a theme i C+X the container and access it from the grey form, change it, C+V the container, built the project.
Nothing changed, all the name of the buttons and stuff are the same, but i feel like nothing is connected to the code anymore, i don't know what happened, i just recently got re-interested into coding, and i have no idea what to do, i tried some things but nothing worked, so here i am, desperate (i spent 3 days on this, i'm REALLY starting from bottom)
link to the project: http://www.mediafire.com/file/2zrbe32lzpx2qhz/SchedulerProjectVBNET.rar
Thanks in advance
It sounds like you have lost all the Handles clauses off your event handlers. As an example, if you add a Button to your form and double-click it, you will get a Click event handler like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Note the Handles clause at the end, which is what connects the code to the event of the control. If you cut that Button from your form to the Clipboard then it no longer exists as part of the form, so that Handles clause will be automatically removed. If you paste the Button back onto the form, the Handles clause is not restored, so the method no longer handles the event.
If that's what has happened - which you can easily check just by looking at the code - then you need to restore all those Handles clauses. You can do that manually, i.e. write them all yourself in the code window, or you can have the designer regenerate them for you. To do the latter, select a control in the designer, open the Properties window, click the Events button, select the desired event and then select the appropriate method from the drop-down list.
Note that you can also double-click an event there to generate a new event handler, which can be useful for handling events other than the default event. You can generate a handler for the default event simply by double-clicking the control.

Keep cursor out of all Textboxes on form

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.

vb.net activated fires multiple times

I want to update a database based on the form that is currently activated. I had originally decided to use the GotFocus event. However I now understand that will not work as the form has controls on it. So I then thought I wouls use the activated event. This works but seems to fire multiple times. I put in the following code:
Private Sub frmNewTicket_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
MsgBox("Form Activated")
End Sub
I select the form and make it activated and the message box appears about 15 times.
Why does it do this? How should I handle this. I only want my code to execute once when the form is activated.
NOTE: There are several forms that the users will be changing between, incuding forms from other applications.
Each time you click OK on the messagebox, the form regains the focus and is activated again.
Put a static Boolean value in your frmNewTicket_Activated like someone has posted here:
Static HasRan As Boolean=False
If Not HasRan Then
HasRan=True
'put code here
End If
It sounds like you are wanting to do something everytime your form gets activated. The Form Activated event will work fine as long as what you are doing doesn't pull focus from the Form which will then trigger another Activation event when the Form gets focus again. Try using something other than a MessageBox for testing like Beep or changing the Form's Color

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.