views:

293

answers:

3

Hi everyone,

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. I would be grateful for your help.

Thanks

A: 

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.

Eadwacer
+1  A: 

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.

ANC_Michael
i have done as you suggested. Many thanks for your answer. I will take your answer as the accepted one. Many thanks for your help.
mazrabul
+1  A: 

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

Raja
thank you for a very clear answer. I have done as you suggested. Unfortunatly, ANC_Michael suggested the same thing only one minute earlier so I will take his answer as the accepted answer. sorry.
mazrabul
That is not a problem at all. All we care in this community is to help each other out. As long your problem is solved I am happy :-)
Raja