0

I have a TextBox that should contain a file name. It's mandatory for the program to work so I've put:

    Private Sub tbScanFilter_Validating(sender As Object, e As CancelEventArgs) Handles tbScanFilter.Validating
    If tbScanFilter.Text.Length = 0 Then
        e.Cancel = True
        ErrorProvider1.SetError(tbScanFilter, "Filter is required.")
    End If
End Sub

Near to the TextBox I've put a button (...) that will open the open file dialog window to select the file.

The issue is that if the TextBox is empty I the ErrorProvider1 will be set and will not allow the focus to move to the button.

So what I would like to do is something like

If destination <> button (...) then
        If tbScanFilter.Text.Length = 0 Then
            e.Cancel = True
            ErrorProvider1.SetError(tbScanFilter, "Filter is required.")
        End If

But how can I know which is the destination control? I've tried to check the Enter event of the button but is not fired before the validating.

Any idea? any help?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178

1 Answers1

0

Rather than using a Button to launch the dialog, why not use the TextBox itself? Something along these lines:

Private Sub tbScanFilter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tbScanFilter.Click
    Using ofd As New OpenFileDialog
        With ofd
            'Set any properties here
            '.Filter = "*.txt|*.txt"
            '.MultiSelect = False
            'Etc...

            If .ShowDialog() = DialogResult.OK Then
                tbScanFilter.Text = .FileName
            End If
        End With
    End Using
End Sub

Edit - You could even change the event from Click to DoubleClick and provide a small label below the TextBox to inform the user to either Click or DoubleClick on the TextBox to launch the dialog.

David
  • 5,877
  • 3
  • 23
  • 40