Imports System
Imports System.IO
Imports System.Text

Public Class frmDynamicControls

#Region "Controls Variable Declaration"

    'Label Controls
    Dim lblCustomerNo As New Label
    Dim lblCustomerName As New Label
    Dim lblAddress As New Label
    Dim lblCity As New Label
    Dim lblPincode As New Label
    Dim lblErrMessage As New Label

    'TextBox Controls
    Dim txtCustomerNo As New TextBox
    Dim txtCustomerName As New TextBox
    Dim txtAddress As New TextBox
    Dim txtCity As New TextBox
    Dim txtPincode As New TextBox

    'Button Controls
    Dim btnSave As New Button
    Dim btnClear As New Button
    Dim btnExit As New Button

    'Write Save Records Text File Path
    Dim strFilePath As String = "../SaveRecords/SaveRecords(" &
Now.Date.ToString("dd-MM-yyyy") & ").txt"

#End Region

#Region "DynamicCreateCtrls Form Load Event"

    Private Sub frmDynamicControls_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.Width = 300
            Me.Height = 250
            Call ShowControl()
            txtCustomerNo.Focus()
            lblErrMessage.Location = New Point(Me.Width / 4, 190)
            lblErrMessage.AutoSize = True
            Me.Controls.Add(lblErrMessage)
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Show Controls Method"

    Protected Sub ShowControl()
        Try
            'Add Label Controls
            lblCustomerNo.Text = "Customer Number"
            lblCustomerNo.ForeColor = Color.Blue
            lblCustomerNo.Location = New Point(10, 10)
            lblCustomerNo.AutoSize = True
            Me.Controls.Add(lblCustomerNo)

            lblCustomerName.Text = "Customer Name"
            lblCustomerName.ForeColor = Color.Blue
            lblCustomerName.Location = New Point(10, 40)
            lblCustomerName.AutoSize = True
            Me.Controls.Add(lblCustomerName)

            lblAddress.Text = "Address"
            lblAddress.ForeColor = Color.Blue
            lblAddress.Location = New Point(10, 70)
            lblAddress.AutoSize = True
            Me.Controls.Add(lblAddress)

            lblCity.Text = "City"
            lblCity.ForeColor = Color.Blue
            lblCity.Location = New Point(10, 100)
            lblCity.AutoSize = True
            Me.Controls.Add(lblCity)

            lblPincode.Text = "Pincode"
            lblPincode.ForeColor = Color.Blue
            lblPincode.Location = New Point(10, 130)
            Me.Controls.Add(lblPincode)

            'Add TextBox Controls
            txtCustomerNo.ForeColor = Color.Red
            txtCustomerNo.Location = New Point(130, 10)
            txtCustomerNo.Width = 150
            Me.Controls.Add(txtCustomerNo)

            txtCustomerName.ForeColor = Color.Red
            txtCustomerName.Location = New Point(130, 40)
            txtCustomerName.Width = 150
            Me.Controls.Add(txtCustomerName)

            txtAddress.ForeColor = Color.Red
            txtAddress.Location = New Point(130, 70)
            txtAddress.Width = 150
            Me.Controls.Add(txtAddress)

            txtCity.ForeColor = Color.Red
            txtCity.Location = New Point(130, 100)
            txtCity.Width = 150
            Me.Controls.Add(txtCity)

            txtPincode.ForeColor = Color.Red
            txtPincode.Location = New Point(130, 130)
            txtPincode.Width = 150
            Me.Controls.Add(txtPincode)

            'Add Button Controls
            btnSave.Text = "Save"
            btnSave.Location = New Point(20, 160)
            btnSave.Width = 75
            btnSave.Height = 25
            AddHandler btnSave.Click, AddressOf btnSave_Click
            Me.Controls.Add(btnSave)

            btnClear.Text = "Clear"
            btnClear.Location = New Point(100, 160)
            btnClear.Width = 75
            btnClear.Height = 25
            AddHandler btnClear.Click, AddressOf btnClear_Click
            Me.Controls.Add(btnClear)

            btnExit.Text = "Exit"
            btnExit.Location = New Point(180, 160)
            btnExit.Width = 75
            btnExit.Height = 25
            AddHandler btnExit.Click, AddressOf btnExit_Click
            Me.Controls.Add(btnExit)
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Clear Controls Method"

    Protected Sub ClearControls()
        Try
            txtCustomerNo.Text = ""
            txtCustomerName.Clear()
            txtAddress.Clear()
            txtCity.Clear()
            txtPincode.Clear()
            txtCustomerNo.Focus()
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Exit Controls Method"

    Protected Sub ExitMethod()
        Try
            Call ClearControls()
            Dim strExit As String = String.Empty
            strExit = MessageBox.Show("Are You Sure Want To Quit?",
Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If strExit = MsgBoxResult.Yes Then
                Application.Exit()
            Else
                Exit Sub
            End If
            Me.Dispose()
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Save Button Click Event"

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
EventArgs)
        Try
            Dim strContents As String = String.Empty
            If ValidateControls() Then
                strContents = txtCustomerNo.Text.Trim() & "|" &
txtCustomerName.Text.Trim() & "|" & txtAddress.Text.Trim() & "|" &
txtCity.Text.Trim & "|" & txtPincode.Text.Trim()
                Call WriteSaveRecords(strContents & "       " &
Now.ToString("dd-MM-yyyy HH:mm"))
                lblErrMessage.Text = "Record Saved Successfully..."
                lblErrMessage.ForeColor = Color.Green
                Call ClearControls()
            End If
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Clear Button Click Event"

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As
EventArgs)
        Try
            Call ClearControls()
            lblErrMessage.Text = String.Empty
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Exit Button Click Event"

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As
EventArgs)
        Try
            Call ExitMethod()
            lblErrMessage.Text = String.Empty
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Write Save Records In Text File Method"

    Private Sub WriteSaveRecords(ByVal strContents As String)
        Try
            Dim objStreamWriter As New StreamWriter(strFilePath, True)
            objStreamWriter.WriteLine(strContents)
            objStreamWriter.Close()
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Sub

#End Region

#Region "Controls Validation Method"

    Private Function ValidateControls() As Boolean
        Dim strValidateErr As String = String.Empty
        Try
            If txtCustomerNo.Text.Trim = "" Then
                strValidateErr = "Customer Number," & vbCrLf
            End If
            If txtCustomerName.Text.Trim = "" Then
                strValidateErr = strValidateErr & "Customer Name," &
vbCrLf
            End If
            If txtAddress.Text.Trim = "" Then
                strValidateErr = strValidateErr & "Address," & vbCrLf
            End If
            If txtCity.Text.Trim = "" Then
                strValidateErr = strValidateErr & "City," & vbCrLf
            End If
            If txtPincode.Text.Trim = "" Then
                strValidateErr = strValidateErr & "Pincode" & vbCrLf
            End If
            If strValidateErr.Trim() = String.Empty Then
                Return True
            Else
                MsgBox(strValidateErr & "Should Not Empty!!!", ,
"Validation Summary")
                Return False
            End If
        Catch ex As Exception
            lblErrMessage.Text = ex.Message
            lblErrMessage.ForeColor = Color.Red
        End Try
    End Function

#End Region

End Class

Reply via email to