Monday, December 2, 2019

Template using Panel to Load Form


VB.NET Template using Panel to Load Form

DOWNLOAD SOURCE CODE: TEMPLATE USING PANEL TO LOAD FORM


---------------------------------------------------------------------------------------------------------


'NOTE
'Panel3.AutoSize = True
'Panel1.AutoSize = False
'Panel2.AutoSize = False
'Panel4.AutoSize = True

'Panel4.Dock = DockStyle = Button
'Panel3.Dock = DockStyle.Top
'Panel1.Dock = DockStyle.Left
'Panel2.Dock = DockStyle.fill
'Panel4.Dock = DockStyle.left *nested under panel1

'Panel1.BorderStyle = BorderStyle.FixedSingle
'Panel2.BorderStyle = BorderStyle.FixedSingle

'Button1.FlatAppearance.BorderSize = 0
'Button2.FlatAppearance.BorderSize = 0
'Button3.FlatAppearance.BorderSize = 0
Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = 1
        Form2.TopLevel = False
        MainPanel.Controls.Clear()
        MainPanel.Controls.Add(Form2)
        Form2.Show()
        Form2.WindowState = System.Windows.Forms.FormWindowState.Maximized

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = 2
        Form3.TopLevel = False
        MainPanel.Controls.Clear()
        MainPanel.Controls.Add(Form3)
        Form3.Show()
        Form3.WindowState = System.Windows.Forms.FormWindowState.Maximized

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Label1.Text = 3
        Form4.TopLevel = False
        MainPanel.Controls.Clear()
        MainPanel.Controls.Add(Form4)
        Form4.Show()
        Form4.WindowState = System.Windows.Forms.FormWindowState.Maximized
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ToolStripStatusLabel1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()
        ToolStripStatusLabel2.Spring = True
        ToolStripStatusLabel2.Text = " "
        Timer1.Interval = 1000
        Timer1.Enabled = True
        Label1.Visible = False
        button_default()

    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ToolStripStatusLabel3.Text = Date.Now
    End Sub



    Public Sub buttonselect(x As String)
        Select Case x
            Case Is = "1"
                Button1.PerformClick()
            Case Is = "2"
                Button2.PerformClick()
            Case Is = "3"
                Button3.PerformClick()

            Case Is = "0"

            Case Else

        End Select

    End Sub



    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        buttonselect(Label1.Text)
    End Sub

    Public Sub button_default()
        Button1.ForeColor = Color.Black
        Button2.ForeColor = Color.Black
        Button3.ForeColor = Color.Black
        Button1.BackColor = Color.Transparent
        Button2.BackColor = Color.Transparent
        Button3.BackColor = Color.Transparent
    End Sub



    Private Sub Label1_TextChanged(sender As Object, e As EventArgs) Handles Label1.TextChanged
        button_default()
        Select Case Label1.Text
            Case Is = "1"
                Button1.BackColor = Color.FromArgb(204, 206, 219)
            Case Is = "2"
                Button2.BackColor = Color.FromArgb(204, 206, 219)
            Case Is = "3"
                Button3.BackColor = Color.FromArgb(204, 206, 219)

        End Select
    End Sub

    Private Sub MainPanel_Paint(sender As Object, e As PaintEventArgs) Handles MainPanel.Paint

    End Sub
End Class





---------------------------------------------------------------------------------------------------------




Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.AutoScroll = True
        Me.FormBorderStyle = FormBorderStyle.None
    End Sub
End Class




---------------------------------------------------------------------------------------------------------



Public Class Form3
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.AutoScroll = True
        Me.FormBorderStyle = FormBorderStyle.None
    End Sub
End Class



---------------------------------------------------------------------------------------------------------



Public Class Form4
    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.AutoScroll = True
        Me.FormBorderStyle = FormBorderStyle.None
    End Sub
End Class


---------------------------------------------------------------------------------------------------------

Thursday, November 21, 2019















VB.NET code that auto resize controls upon form resize


DOWNLOAD SOURCE CODE: AUTO RESIZE CONTROL CODE



---------------------------------------------------------------------------------------------------------

Public Class Form1
    Dim rs As New Resizer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        rs.FindAllControls(Me)

    End Sub

    Private Sub Button1_Resize(sender As Object, e As EventArgs) Handles Button1.Resize
        rs.ResizeAllControls(Me)
    End Sub
End Class

---------------------------------------------------------------------------------------------------------



' Resizer
' This class is used to dynamically resize and reposition all controls on a form.
' Container controls are processed recursively so that all controls on the form
' are handled.
'
' Usage:
'  Resizing functionality requires only three lines of code on a form:
'
'  1. Create a form-level reference to the Resize class:
'     Dim myResizer as Resizer
'
'  2. In the Form_Load event, call the  Resizer class FIndAllControls method:
'     myResizer.FindAllControls(Me)
'
'  3. In the Form_Resize event, call the  Resizer class ResizeAllControls method:
'     myResizer.ResizeAllControls(Me)
'
'-------------------------------------------------------------------------------
Public Class Resizer

    '----------------------------------------------------------
    ' ControlInfo
    ' Structure of original state of all processed controls
    '----------------------------------------------------------
    Private Structure ControlInfo
        Public name As String
        Public parentName As String
        Public leftOffsetPercent As Double
        Public topOffsetPercent As Double
        Public heightPercent As Double
        Public originalHeight As Integer
        Public originalWidth As Integer
        Public widthPercent As Double
        Public originalFontSize As Single
    End Structure

    '-------------------------------------------------------------------------
    ' ctrlDict
    ' Dictionary of (control name, control info) for all processed controls
    '-------------------------------------------------------------------------
    Private ctrlDict As Dictionary(Of String, ControlInfo) = New Dictionary(Of String, ControlInfo)

    '----------------------------------------------------------------------------------------
    ' FindAllControls
    ' Recursive function to process all controls contained in the initially passed
    ' control container and store it in the Control dictionary
    '----------------------------------------------------------------------------------------
    Public Sub FindAllControls(thisCtrl As Control)

        '-- If the current control has a parent, store all original relative position
        '-- and size information in the dictionary.
        '-- Recursively call FindAllControls for each control contained in the
        '-- current Control
        For Each ctl As Control In thisCtrl.Controls
            Try
                If Not IsNothing(ctl.Parent) Then
                    Dim parentHeight = ctl.Parent.Height
                    Dim parentWidth = ctl.Parent.Width

                    Dim c As New ControlInfo
                    c.name = ctl.Name
                    c.parentName = ctl.Parent.Name
                    c.topOffsetPercent = Convert.ToDouble(ctl.Top) / Convert.ToDouble(parentHeight)
                    c.leftOffsetPercent = Convert.ToDouble(ctl.Left) / Convert.ToDouble(parentWidth)
                    c.heightPercent = Convert.ToDouble(ctl.Height) / Convert.ToDouble(parentHeight)
                    c.widthPercent = Convert.ToDouble(ctl.Width) / Convert.ToDouble(parentWidth)
                    c.originalFontSize = ctl.Font.Size
                    c.originalHeight = ctl.Height
                    c.originalWidth = ctl.Width
                    ctrlDict.Add(c.name, c)
                End If

            Catch ex As Exception
                Debug.Print(ex.Message)
            End Try

            If ctl.Controls.Count > 0 Then
                FindAllControls(ctl)
            End If

        Next '-- For Each

    End Sub

    '----------------------------------------------------------------------------------------
    ' ResizeAllControls
    ' Recursive function to resize and reposition all controls contained in the Control
    ' dictionary
    '----------------------------------------------------------------------------------------
    Public Sub ResizeAllControls(thisCtrl As Control)

        Dim fontRatioW As Single
        Dim fontRatioH As Single
        Dim fontRatio As Single
        Dim f As Font

        '-- Resize and reposition all controls in the passed control
        For Each ctl As Control In thisCtrl.Controls
            Try
                If Not IsNothing(ctl.Parent) Then
                    Dim parentHeight = ctl.Parent.Height
                    Dim parentWidth = ctl.Parent.Width

                    Dim c As New ControlInfo

                    Dim ret As Boolean = False
                    Try
                        '-- Get the current control's info from the control info dictionary
                        ret = ctrlDict.TryGetValue(ctl.Name, c)

                        '-- If found, adjust the current control based on control relative
                        '-- size and position information stored in the dictionary
                        If (ret) Then
                            '-- Size
                            ctl.Width = Int(parentWidth * c.widthPercent)
                            ctl.Height = Int(parentHeight * c.heightPercent)

                            '-- Position
                            ctl.Top = Int(parentHeight * c.topOffsetPercent)
                            ctl.Left = Int(parentWidth * c.leftOffsetPercent)

                            '-- Font
                            f = ctl.Font
                            fontRatioW = ctl.Width / c.originalWidth
                            fontRatioH = ctl.Height / c.originalHeight
                            fontRatio = (fontRatioW +
                            fontRatioH) / 2 '-- average change in control Height and Width
                            ctl.Font = New Font(f.FontFamily,
                            c.originalFontSize * fontRatio, f.Style)

                        End If
                    Catch
                    End Try
                End If
            Catch ex As Exception
            End Try

            '-- Recursive call for controls contained in the current control
            If ctl.Controls.Count > 0 Then
                ResizeAllControls(ctl)
            End If

        Next '-- For Each
    End Sub

End Class

Wednesday, August 1, 2018

RESTORE MSSQL DATABASE WITHOUT LDF ONLY MDF

RESTORE MSSQL DATABASE WITHOUT LDF ONLY MDF


USE master
GO
CREATE DATABASE <database_name>
ON (FILENAME='C:\<database_mdfname>.mdf')
FOR ATTACH_FORCE_REBUILD_LOG

Saturday, June 30, 2018

Value Updater using .txt file using VB.Net












VB.NET App that lets you Update/Add existing value using .txt  file


DOWNLOAD SOURCE CODE: VALUE UPDATER USING .TXT FILE


Imports System.Data
Imports System.Data.SqlClient


Public Class Form1
    Dim myconn As SqlConnection
    Dim mycmd As SqlCommand
    Dim mydr As SqlDataReader
    Dim dtable As DataTable
    Dim x() As String
    Dim y() As String
    Dim val1 As String
    Dim val2 As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dprocess()


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fd As OpenFileDialog = New OpenFileDialog()

        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "C:\"
        fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
        fd.FilterIndex = 2
        fd.RestoreDirectory = True

        If fd.ShowDialog() = DialogResult.OK Then
            txtaddress.Text = fd.FileName
        End If
        '------------------------------------------------------------------------
        Try
            Dim objreader As New System.IO.StreamReader(txtaddress.Text)
            txtloaded.Text = objreader.ReadToEnd
            objreader.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dview()
    End Sub

  


    Public Sub dview()

        Try
            myconn = New SqlConnection("Data Source=temp-pc\sqlexpress;Initial Catalog=test;User ID=sa;Password=tHr34d1ng")
            If myconn.State = System.Data.ConnectionState.Closed Then
                myconn.Open()
            End If

            mycmd = New SqlCommand("select * from table_1 ", myconn)
            mydr = mycmd.ExecuteReader()
            'MessageBox.Show("(" & xcounter & ") affected")
            Dim dtable As New DataTable()
            dtable.Load(mydr)

            DataGridView1.DataSource = dtable
            DataGridView1.Refresh()


            myconn.Close()
            mycmd.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "view")
            myconn.Close()
            mycmd.Dispose()
        End Try

    End Sub

    Public Sub dprocess()




        Dim n As Integer = 0
        Dim m As Integer = 0

        Try
            If Len(txtloaded.Text) <> 0 Then
                x = (txtloaded.Text.Split(vbCrLf))
                While x.Length <> n
                    x(n) = x(n).Replace(vbCr, "").Replace(vbLf, "")
                    y = x(n).Split(",")

                    dprocess2()

                    ' MsgBox(x(n))
                    n = n + 1
                End While

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "process")
        End Try

        Array.Clear(x, 0, x.Length)
        Array.Clear(y, 0, y.Length)




    End Sub

    Public Sub dprocess2()

        dsearch()


        Dim xcounter As Integer = 0
        Try
            myconn = New SqlConnection("Data Source=temp-pc\sqlexpress;Initial Catalog=test;User ID=sa;Password=tHr34d1ng")
            If myconn.State = System.Data.ConnectionState.Closed Then
                myconn.Open()
            End If

            y(1) = Val(y(1)) + Val(val1)
            y(2) = Val(y(2)) + Val(val2)

            mycmd = New SqlCommand("update table_1 set val1='" & y(1) & "' , val2='" & y(2) & "'  where item ='" & y(0) & "'", myconn)
            xcounter = mycmd.ExecuteNonQuery
            MessageBox.Show("(" & xcounter & ") affected")

            myconn.Close()
            mycmd.Dispose()

            dview()



        Catch ex As Exception
            MessageBox.Show(ex.Message, "update")
            myconn.Close()
            mycmd.Dispose()


        End Try

    End Sub



    Public Sub dsearch()

        MessageBox.Show(y(0))

        Dim i As Integer = 0
        Dim rcount As Integer = 0
        Dim ifound As Boolean = False



        Try
            rcount = DataGridView1.RowCount
            DataGridView1.MultiSelect = False

            While i < rcount And ifound = False
                DataGridView1.Rows(i).Selected = True

                If DataGridView1.SelectedRows(0).Cells(0).Value = y(0) Then

                    val1 = DataGridView1.SelectedRows(0).Cells(1).Value
                    val2 = DataGridView1.SelectedRows(0).Cells(2).Value

                    DataGridView1.CurrentCell = DataGridView1(0, i)

                    ifound = True

                End If

                i = i + 1

            End While



        Catch ex As Exception
            MessageBox.Show(ex.Message, "found")
        End Try

        If ifound = False Then MessageBox.Show("no record found", "")
    End Sub

End Class


Wednesday, June 20, 2018

MONITOR NETWORK PING :
















VB.NET App that lets you monitor your Network Equipment with Logs


DOWNLOAD SOURCE CODE: MONITOR NETWORK PING

Imports System.Text.RegularExpressions


Public Class Form1

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Application.Exit()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        logs.ScrollBars = ScrollBars.Vertical
        logs.Multiline = True
        txttmr.Enabled = False
        txttmr.Text = 50
        Dim line As String
        Dim IPini() As String
        Dim desc() As String = {"a", "b"}
        Dim ips() As String = {"a", "b"}
        Dim icount As Integer
        Dim isplit() As String

        Using sr As New System.IO.StreamReader(".\settings.ini")
            line = sr.ReadToEnd
            IPini = line.Split(vbCrLf)

            While IPini.Length <> icount
                IPini(icount) = IPini(icount).Replace(vbCr, "").Replace(vbLf, "")
                isplit = IPini(icount).Split(",")

                desc(icount) = isplit(0)
                ips(icount) = isplit(1)
                icount = icount + 1

            End While
        End Using

        lblip1.Text = ips(0)
        lblip2.Text = ips(1)
        btnip1.Text = desc(0)
        btnip2.Text = desc(1)


        ipwatch(lblip1.Text, btnip1)
        ipwatch(lblip2.Text, btnip2)
        Tmr.Enabled = True


        Try
            Dim objreader As New System.IO.StreamReader(".\logs.txt")
            logs.Text = objreader.ReadToEnd
            objreader.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "load")
        End Try


    End Sub


    Public Sub ipwatch(ByVal ip As String, ByVal desc As Button)

        If My.Computer.Network.Ping(ip) Then
            desc.BackColor = Color.Green
        Else
            desc.BackColor = Color.Red


            Try
                Dim objreader As New System.IO.StreamReader(".\logs.txt")
                logs.Text = objreader.ReadToEnd
                objreader.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "load")
            End Try

            logs.Text = DateTime.Now & " - " & ip & " - timeout" & vbCrLf & logs.Text

            Try
                Dim objwrite As New System.IO.StreamWriter(".\logs.txt")
                objwrite.Write(logs.Text)
                objwrite.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "load")
            End Try

            Try
                Dim objreader As New System.IO.StreamReader(".\logs.txt")
                logs.Text = objreader.ReadToEnd
                objreader.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "load")
            End Try

        End If

    End Sub

    Private Sub btnip1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnip1.Click

        Form2.lblbtn.Text = 1
        Form2.txtip.Text = lblip1.Text
        Form2.txtdesc.Text = btnip1.Text
        Form2.Show()
        Me.Hide()
    End Sub

    Private Sub btnip2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnip2.Click
        Form2.lblbtn.Text = 2
        Form2.txtip.Text = lblip2.Text
        Form2.txtdesc.Text = btnip2.Text
        Form2.Show()
        Me.Hide()

    End Sub

    Private Sub Tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tmr.Tick
        If ProgressBar1.Value < 100 Then
            ProgressBar1.Value = CDbl(ProgressBar1.Value) + 2
            txttmr.Text = Val(txttmr.Text) - 1
        Else
            ipwatch(lblip1.Text, btnip1)
            ipwatch(lblip2.Text, btnip2)
            ProgressBar1.Value = 0
            txttmr.Text = 50

        End If
    End Sub
End Class


'CREATED BY ROSSI CATLY