function help

rjhe22

Well-known member
Joined
May 22, 2008
Messages
88
Programming Experience
Beginner
VB.NET:
Public Function IsValidWebPage(ByVal url As String) As Boolean
    Dim blnOK As Boolean = IO.File.Exists(url)
    If blnOK = False Then
        If url.ToLower().StartsWith("www.") Then url = "http://" & url
        Dim webResponse As System.Net.HttpWebResponse = Nothing
        Try
            Dim webRequest As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
            webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
            If webResponse IsNot Nothing Then blnOK = True
        Catch ex As Exception
            blnOK = False
        Finally
            If Not (webResponse Is Nothing) Then webResponse.Close()
        End Try
    End If
    Return blnOK
End Function
VB.NET:
Private Sub txtDocument_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDocument.Validating

End Sub
what do i have to put in in the txt validate part to call the top function and get it working can anyone help
 
Last edited by a moderator:
In short:
VB.NET:
e.Cancel = Not Me.IsValidWebPage(Me.txtDocument.Text)
That said, you might want to display a message to the user if the data fails validation:
VB.NET:
If Not Me.IsValidWebPage(Me.txtDocument.Text)
    Me.txtDocument.SelectAll()
    Me.txtDocument.HideSelection = False
    MessageBox.Show("Invalid web address.")
    Me.txtDocument.HideSelection = True
    e.Cancel = True
End If
 
Public Function IsValidWebPage(ByVal url As String) As Boolean
Dim blnOK As Boolean = IO.File.Exists(url)

If blnOK = False Then
If url.ToLower().StartsWith("www.") Then url = "http://" & url
Dim webResponse As System.Net.HttpWebResponse = Nothing
Try
Dim webRequest As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
If webResponse IsNot Nothing Then blnOK = True
Catch ex As Exception
blnOK = False
Finally
If Not (webResponse Is Nothing) Then webResponse.Close()
End Try
End If
Return blnOK
End Function
Private Sub txtDocument_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDocument.Validating


If Not Me.IsValidWebPage(Me.txtDocument.Text) Then
Me.txtDocument.SelectAll()
MessageBox.Show("Invalid web address.")
Me.Close()

End If

ok i have got this far and it work for www sites. now i need to use file(exists) so it works for files any ideas how i would get that working in the function
 
I don't quite understand why you're putting Me.Close in a Validating event handler but that's up to you. To answer your question:
VB.NET:
If Not Me.IsValidWebPage(Me.txtDocument.Text) AndAlso Not IO.File.Exists(Me.txtDocument.Text) Then
You might also want to change your error message.
 
thaanks very much that was a great help. ya changing my message.
also taking out me.close actually put that in by mistake and forgot to take it out
 
Back
Top