Imports System.Data.OleDb
Imports System
Imports System.Management


Public Class AddMethod
    Dim conn As OleDbConnection
    Dim SQL As OleDbCommand
    Dim RSet As OleDbDataReader
    Private Sub Panel3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel3.Paint

    End Sub
    Private Function Display_Net_Adapters() As Boolean
        Try

            Dim lSingleItem As ListViewItem
            Dim oSearcher As New System.Management.ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration ")
            Dim oObject As System.Management.ManagementObject
            Dim sIP() As String
            Dim Fin_IP As String = ""
            Dim Index As String = ""
            Dim Desc As String = ""
            Dim sString As String
            Dim sMessage As String = ""
            For Each oObject In oSearcher.Get
                Index = Nothing
                Desc = Nothing
                sIP = Nothing
                If Not oObject("ipaddress") Is Nothing Then Index = oObject("Index")
                If Not oObject("ipaddress") Is Nothing Then Desc = oObject("Description")
                If Not oObject("ipaddress") Is Nothing Then sIP = oObject("ipaddress")
                If Not Index = "" Then
                    For Each sString In sIP
                        Fin_IP = sString
                        'sMessage += Desc + vbCrLf + Index + vbCrLf
                        'sMessage += sString + vbCrLf
                    Next
                    lSingleItem = ListView1.Items.Add(Desc, 0)
                    lSingleItem.SubItems.Add(Index)
                    lSingleItem.SubItems.Add(Fin_IP)
                End If
            Next
            Display_Net_Adapters = True
        Catch
            Display_Net_Adapters = False
        End Try

    End Function

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub AddMethod_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        ListView1.View = View.Details
        ListView1.SmallImageList = ImageList1
        Display_Net_Adapters()
    End Sub
    Private Function Test_Net_Adapter(ByVal IndexA As String) As Boolean
        Try
            Dim oSearcher As New System.Management.ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration where index = " & IndexA & " ")
            Dim oObject As System.Management.ManagementObject
            Dim sIP() As String
            Dim Fin_IP As String = ""
            Dim Index As String = ""
            Dim Desc As String = ""
            Dim sString As String
            Dim sMessage As String = ""
            For Each oObject In oSearcher.Get
                Index = Nothing
                Desc = Nothing
                sIP = Nothing
                If Not oObject("ipaddress") Is Nothing Then Index = oObject("Index")
                If Not oObject("ipaddress") Is Nothing Then Desc = oObject("Description")
                If Not oObject("ipaddress") Is Nothing Then sIP = oObject("ipaddress")
                If Not Index = "" Then
                    For Each sString In sIP
                        Fin_IP = sString
                        'sMessage += Desc + vbCrLf + Index + vbCrLf
                        sMessage += "Detected IP Address: " + sString + vbCrLf
                    Next
                    MsgBox(sMessage)
                End If
            Next
            Test_Net_Adapter = True
        Catch
            Test_Net_Adapter = False
        End Try

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If Not My.Computer.Network.IsAvailable = True Then
            MsgBox("This feature is not available offline.", MsgBoxStyle.Information)
            Exit Sub
        End If
        Try

            If Not TextBox3.Text.StartsWith("http://") Or TextBox3.Text.StartsWith("https://") Then
                MsgBox("URL must start with http:// or https://", MsgBoxStyle.Critical)
                Exit Sub
            End If
            Dim Page As String = ""
            Dim F_IP As String = ""
            Dim IsVal As Boolean = False
            Page = Functions.Test_Connect(TextBox3.Text, TextBox5.Text, TextBox6.Text)
            If Page = "Failed" Then
                Exit Sub
            End If
            F_IP = Functions.FindIPInString(Page, TextBox4.Text)
            If F_IP = "Keyword not found" Then
                MsgBox("IP detection failed. " + "The search keyword was not found.", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
            IsVal = Functions.ValidIP(F_IP)
            If IsVal = False Then
                MsgBox("IP detection failed. " + "The IP is invalid.", MsgBoxStyle.Exclamation)
                Exit Sub
            Else
                MsgBox("Detected IP address is: " + F_IP, MsgBoxStyle.Information)
                Exit Sub
            End If
        Catch
            MsgBox(Err.Description)
        End Try
    End Sub

    Private Sub TextBox3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.GotFocus
        If TextBox3.Text = "http://192.168.1.1/StaRouter.htm **Example**" Then TextBox3.Text = ""
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub TextBox4_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.GotFocus
        If TextBox4.Text = "wan_ip **Example**" Then TextBox4.Text = ""
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Isval As Boolean = False
        Try
            If TextBox8.Text = "" Then Exit Sub
            Isval = Functions.ValidIP(TextBox8.Text)
            If Not Isval = True Then
                MsgBox("Invalid IP Address.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            If TextBox7.Text = "" Then
                MsgBox("You must enter a unique name.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App_Path() & "progdb.mdb;")
            conn.Open()
            SQL = New OleDbCommand("select * From IP_Detection where Ident = '" & TextBox7.Text & "'", conn)
            RSet = SQL.ExecuteReader
            If RSet.HasRows = True Then
                MsgBox("The requested Name is already in use.", MsgBoxStyle.Critical)
                Exit Sub

            End If
            RSet.Close()

            SQL = New OleDbCommand("INSERT INTO IP_Detection(Ident, IP, Method, Status) " + "VALUES ('" & TextBox7.Text & "', '" & TextBox8.Text & "', 'Manual', 'Operational') ", conn)
            RSet = SQL.ExecuteReader
            TextBox7.Text = ""
            TextBox8.Text = ""
            MsgBox("The IP detection method was created.", MsgBoxStyle.Information)
            conn.Close()
            RSet.Close()

        Catch
            MsgBox(Err.Description)
        End Try

    End Sub

    Private Sub TabPage3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage3.Click

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Isval As Boolean = False
        Dim IP As String = ""
        Dim Inde As String = ""

        Try

            If ListView1.SelectedItems.Count <= 0 Then
                Exit Sub
            End If


            IP = ListView1.SelectedItems(0).SubItems(2).Text.ToString
            Inde = ListView1.SelectedItems(0).SubItems(1).Text.ToString
            If IP = "" Then Exit Sub
            Isval = Functions.ValidIP(IP)
            If Not Isval = True Then
                MsgBox("Invalid IP Address.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            If TextBox1.Text = "" Then
                MsgBox("You must enter a unique name.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App_Path() & "progdb.mdb;")
            conn.Open()
            SQL = New OleDbCommand("select * From IP_Detection where Ident = '" & TextBox1.Text & "'", conn)
            RSet = SQL.ExecuteReader
            If RSet.HasRows = True Then
                MsgBox("The requested Name is already in use.", MsgBoxStyle.Critical)
                Exit Sub

            End If
            RSet.Close()

            SQL = New OleDbCommand("INSERT INTO IP_Detection (Ident, A_Index, IP, Method, Status) " + "VALUES ('" + TextBox1.Text + "', '" + Inde + "', '" + IP + "', 'Adapter', 'Operational')", conn)
            RSet = SQL.ExecuteReader
            TextBox1.Text = ""

            MsgBox("The IP detection method was created.", MsgBoxStyle.Information)
            conn.Close()
            RSet.Close()
        Catch
            MsgBox(Err.Description)
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        If Not My.Computer.Network.IsAvailable = True Then
            MsgBox("This feature is not available offline.", MsgBoxStyle.Information)
            Exit Sub
        End If
        Try

            If Not TextBox3.Text.StartsWith("http://") Or TextBox3.Text.StartsWith("https://") Then
                MsgBox("URL must start with http:// or https://", MsgBoxStyle.Critical)
                Exit Sub
            End If
            Dim Page As String = ""
            Dim F_IP As String = ""
            Dim IsVal As Boolean = False
            Page = Functions.Test_Connect(TextBox3.Text, TextBox5.Text, TextBox6.Text)
            If Page = "Failed" Then
                Exit Sub
            End If
            F_IP = Functions.FindIPInString(Page, TextBox4.Text)
            If F_IP = "Keyword not found" Then
                MsgBox("IP detection failed. " + "The search keyword was not found.", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
            IsVal = Functions.ValidIP(F_IP)
            If IsVal = False Then
                MsgBox("IP detection failed. " + "The IP is invalid.", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
            If TextBox2.Text = "" Then
                MsgBox("You must enter a unique name.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App_Path() & "progdb.mdb;")
            conn.Open()
            SQL = New OleDbCommand("select * From IP_Detection where Ident = '" & TextBox2.Text & "'", conn)
            RSet = SQL.ExecuteReader
            If RSet.HasRows = True Then
                MsgBox("The requested name is already in use.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            RSet.Close()
            SQL = New OleDbCommand("INSERT INTO IP_Detection (Ident, URL, Keyword, IP, UserID, [Password], Method, Status) " + "VALUES ('" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + F_IP + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', 'Router', 'Operational')", conn)
            RSet = SQL.ExecuteReader
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            MsgBox("The IP detection method was created.", MsgBoxStyle.Information)
            conn.Close()
            RSet.Close()



        Catch
            MsgBox(Err.Description)
        End Try
    End Sub
End Class
