Site Search:
Sign in | Join | Help

This Blog

Syndication

VB.NET

Notes, Tricks and Tips on VB.NET

March 2009 - Posts

  • Code to Populate a FormObjects Collection

    Code for the object 

     Public Class formObject
        Public Name As String
        Public Type As String
        Public Control As Control
        Public Tag As String = ""
    
    
        Sub New(ByVal name As String, ByVal type As String, ByVal control As Control, ByVal tag As String)
            Me.Name = name
            Me.Type = type
            Me.Control = control
    
    
            If Not String.IsNullOrEmpty(tag) Then
                Me.Tag = tag
            End If
        End Sub
    End Class
     

    'at the form level:
    Dim formObjects As New List(Of formObject)
    Dim bDirty As Boolean
    
    
    
    
         Sub populateFormObjectsCollection()
            Dim o As formObject
            'load the form fields into a collection
    
    
    
            For Each c As Control In Me.Controls
                Select Case Microsoft.VisualBasic.Left(c.Name, 3).ToUpper
                    Case "TXT", "DDL", "BTN"
                        o = New formObject(c.Name, Microsoft.VisualBasic.Left(c.Name, 3).ToUpper, c, c.Tag)
                        formObjects.Add(o)
                End Select
            Next
    
    
    
    For Each o In formObjects
        Select Case o.Tag.ToUpper
            Case "KEY"
            Case "NOENABLE"
            Case Else
                AddHandler o.Control.TextChanged, AddressOf TextChangedEvent
                AddHandler o.Control.Validating, AddressOf ValidatingEvent
        End Select
    Next
    
    
    
        End Sub

    Code for the event handler

     Private Sub TextChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        Select Case Me.FormState
            Case formStateType.clean, formStateType.populated
                bDirty = True
                Me.txtStatus1.Text = "Dirty"
        End Select
    End Sub
    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
                    Exit Sub
                Case formStateType.clean
                    globalMessageBox("Please choose a Project first")
    
    
                Case formStateType.populated
                    setFormState(formStateType.dirty)
                Case formStateType.dirty
    
    
            End Select
    
    
            bDirty = False
        End If
    End Sub

  • Code for a VB AppUser class

    This is the class that I use in VB Win Forms applications for user data

     Public Class AppUser
        Public Shared ReadOnly Property UserID() As String
            Get
                Dim strUser As String
                strUser = System.Security.Principal.WindowsIdentity.GetCurrent.ToString
                Return strUser
            End Get
        End Property
        Public Shared ReadOnly Property database() As String
            Get
                Return "dbName"
            End Get
        End Property
        Public Shared ReadOnly Property connectionString() As String
            Get
                Return "Data Source=myServer;User Id=username;Password=mypass;Initial Catalog=dbname"
            End Get
        End Property
    End Class

  • Standard code to write a VB forms application

    Here is my library code for VB Forms apps. Having it available like this helps speed development

More Posts Next page »