开发者

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:

  1. The user will always a select a folder using a known mechanism (clicking on button and seleting folder)
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜