Site Search:
Sign in | Join | Help

This Blog

Syndication

VB.NET

Notes, Tricks and Tips on VB.NET

April 2009 - Posts

  • VB.NET index

  • Codebehind for a VB form

    [code languate="vb.net"]

    Public Class GeneralContractor
        Dim oFormObjects As New formObjects
        Dim _FormState As formStateType
        Private Property FormState() As formStateType
            Get
                Return _FormState
            End Get
            Set(ByVal value As formStateType)
                _FormState = value
                Select Case value
                    Case formStateType.clean
                        Me.lblFormState.Text = "Clean"
                    Case formStateType.dirty
                        Me.lblFormState.Text = "Dirty"
                    Case formStateType.loading
                        Me.lblFormState.Text = "Loading"
                    Case formStateType.populated
                        Me.lblFormState.Text = "Populated"
                    Case formStateType.newPending
                        Me.lblFormState.Text = "New"
                End Select

            End Set
        End Property
        Dim _bDirty As Boolean
        Private Property bDirty() As Boolean
            Get
                Return _bDirty
            End Get
            Set(ByVal value As Boolean)
                _bDirty = value
                Select Case value
                    Case True
                        Me.lblDirtyFlag.Text = "..."
                    Case False
                        Me.lblDirtyFlag.Text = ""
                End Select
            End Set
        End Property

        Private Sub GeneralContractor_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                Me.lblDirtyFlag.Text = ""
                Me.lblFormState.Text = ""

                oFormObjects.Populate(Me)
                'identify the special objects
                oFormObjects.Item("txtRowID").ControlClass = formObject.controlClassType.Key
                oFormObjects.Item("btnSave").ControlClass = formObject.controlClassType.SaveButton
                oFormObjects.Item("btnCancel").ControlClass = formObject.controlClassType.CancelButton
                oFormObjects.Item("btnDelete").ControlClass = formObject.controlClassType.DeleteButton
                oFormObjects.Item("btnLookup").ControlClass = formObject.controlClassType.LookupButton
                oFormObjects.Item("btnNew").ControlClass = formObject.controlClassType.NewButton

                For Each o As formObject In oFormObjects
                    Select Case o.ControlClass
                        Case formObject.controlClassType.CancelButton, formObject.controlClassType.DeleteButton, formObject.controlClassType.SaveButton
                        Case formObject.controlClassType.Key
                        Case formObject.controlClassType.LookupButton, formObject.controlClassType.NewButton
                        Case Else
                            AddHandler o.Control.TextChanged, AddressOf TextChangedEvent
                            AddHandler o.Control.Validating, AddressOf ValidatingEvent
                    End Select
                Next

                setFormState(formStateType.clean)
            Catch ex As Exception
                globalErrorHandler(ex)
            End Try

        End Sub
        Private Sub setFormState(ByVal formState As formStateType)
            Select Case formState
                Case formStateType.loading

                Case formStateType.clean
                    Me.FormState = formStateType.loading
                    'clear all fields
                    oFormObjects.SetClean()

                Case formStateType.populated
                    oFormObjects.SetPopulated()

                Case formStateType.dirty
                    oFormObjects.SetDirty()

                Case formStateType.newPending
                    oFormObjects.SetPopulated()
            End Select

            Me.FormState = formState
        End Sub


        Private Sub btnLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLookup.Click

            'create an instance of the 'Owner lookup form' form
            Dim frm As New GeneralContractorLookup

            'attach to the notify sub
            frm.NotifyRowDoubleClicked(New GeneralContractorLookup.gridRowDoubleClicked(AddressOf KeyChosen))


            'open the form modally
            frm.ShowDialog()
        End Sub


        'notice that the signature is the same as the DELEGATE
        Public Sub KeyChosen(ByVal intRowID As Int32)

            'this fires when the 'choose customer' fires the 'doubleClicked' event
            populateform(intRowID)

        End Sub
        Sub populateform(ByVal intRowID As Int32)
            Dim ofacGeneralContractor As New dynData.facPTGeneralContractor

            If ofacGeneralContractor.Fill(intRowID, AppUser.connectionString) Then
                fill(ofacGeneralContractor)
            End If
        End Sub

        Sub fill(ByVal o As dynData.facPTGeneralContractor)
            Me.FormState = formStateType.loading

            Me.txtRowID.Text = o.ContractorID
            Me.txtName.Text = o.ContractorName
            Me.txtAddress1.Text = o.Address1
            Me.txtAddress2.Text = o.Address2
            Me.txtCity.Text = o.City
            Me.txtState.Text = o.State
            Me.txtZip.Text = o.Zip
            Me.txtContact.Text = o.Contact
            Me.txtPhone.Text = o.Phone
            Me.txtFax.Text = o.Fax

            setFormState(formStateType.populated)

        End Sub

        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            'save
            If save() Then
                'reset form
                setFormState(formStateType.clean)

            End If

        End Sub

        Private Sub Owner_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim ofacGeneralContractor As New dynData.facPTGeneralContractor

            Select Case Me.FormState
                Case formStateType.dirty
                    If globalMessageBox("Save form?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                        If Not save() Then
                            e.Cancel = True

                        End If
                    End If
                Case formStateType.newPending
                    If globalMessageBox("Save form?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                        'save the form
                        If Not save() Then
                            'there was an error saving
                            e.Cancel = True
                        End If
                    Else
                        'the user doesn't want to save
                        ofacGeneralContractor.Delete(Me.txtRowID.Text, AppUser.connectionString)
                    End If
            End Select

        End Sub

        Function save() As Boolean
            Try

                Dim ofacGeneralContractor As New dynData.facPTGeneralContractor
                ofacGeneralContractor.ContractorID = Me.txtRowID.Text
                ofacGeneralContractor.ContractorName = Me.txtName.Text
                ofacGeneralContractor.Address1 = Me.txtAddress1.Text
                ofacGeneralContractor.Address2 = Me.txtAddress2.Text
                ofacGeneralContractor.City = Me.txtCity.Text
                ofacGeneralContractor.State = Me.txtState.Text
                ofacGeneralContractor.Zip = Me.txtZip.Text
                ofacGeneralContractor.Contact = Me.txtContact.Text
                ofacGeneralContractor.Phone = Me.txtPhone.Text
                ofacGeneralContractor.Fax = Me.txtFax.Text

                Return ofacGeneralContractor.Save(ofacGeneralContractor, AppUser.connectionString)


            Catch ex As Exception
                globalErrorHandler(ex)
                Return False
            End Try

        End Function
        Private Sub ValidatingEvent(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            If bDirty Then

                'the user has changed the value in a field other than Product Number
                Select Case Me.FormState
                    Case formStateType.loading

                    Case formStateType.clean

                    Case formStateType.populated
                        setFormState(formStateType.dirty)
                    Case formStateType.dirty

                End Select

                bDirty = False
            End If
        End Sub
        Private Sub TextChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            Select Case Me.FormState
                Case formStateType.populated, formStateType.newPending
                    bDirty = True
            End Select
        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            setFormState(formStateType.clean)
        End Sub

        Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
            Me.FormState = formStateType.loading

            Dim o As New dynData.facPTGeneralContractor
            Dim intRowID As Int32 = o.Create(AppUser.connectionString)
            Me.txtRowID.Text = intRowID

            setFormState(formStateType.newPending)


        End Sub
    End Class

     

    [/code]