Wednesday, February 2, 2011

String split problem

sometimes string split works well on single characters, but when trying to using a string to split, problems may occur, to overcome this issue, use the following code will do the job:



Dim linecol() As String
linecol = line.Split(New String() {"&|&"}, StringSplitOptions.None)  'define a new string to use string as delimiter

Monday, January 17, 2011

Populating combbox using a List with two columns

        uList = SybizDB.GetUserList
        cbUser.DataSource = uList
        cbUser.DisplayMember = "Name"
        cbUser.ValueMember = "UserNumber"

easy as that.
displaying the name field while selecting the actual value of usernumber field.

Friday, January 14, 2011

SQL Selection Queries

To compare two table values and select the ones that aren't common, say common field is ProductID:


        select ProductID from Table1 where ProductID not in (select ProductID from Table2)

Tuesday, January 11, 2011

Dynamically create tab control in VB.NET

Creating a tab control with textbox/label/buttons:


    Private tabSupplierArray As System.Windows.Forms.TabControl
    Private lableArray(3) As Label
    Private txtSuppArray(3) As TextBox


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

        'Create and set position of tab control
        Dim tabControl As New TabControl()
        tabControl.Location = New System.Drawing.Point(0, 10)
        tabControl.Size() = New Size(900, 200)

        Dim fnt As New Font("Courier New", 9.75)

        'Loop 8 times to create 8 tab pages---------------------------------------
        For c As Integer = 1 To 8
            Dim tabPage As New TabPage()
            Dim btnSup As New Button()

            If c = 1 Then
                tabPage.Text = "Preferred Supplier"
            Else
                tabPage.Text = c.ToString()
            End If

            Dim txtCode As New TextBox
            'Creating labels and textbox horizontally twice----------------------
            For i As Integer = 0 To 1
                lableArray(i) = New System.Windows.Forms.Label
                With lableArray(i)
                    .Size() = New Size(40, 22)
                    .Font = fnt
                    .Location = New System.Drawing.Point(30 + (i * 350), 30)
                    .TextAlign = HorizontalAlignment.Right
                End With
                txtSuppArray(i) = New System.Windows.Forms.TextBox
                With txtSuppArray(i)
                    If i = 0 Then
                        .Size() = New Size(150, 22)
                    Else
                        .Size() = New Size(367, 22)
                    End If
                    .ReadOnly = True
                    .Location = New System.Drawing.Point(130 + (i * 350), 30)
                    .TextAlign = HorizontalAlignment.Left
                End With
            Next
            'Creating labels and textbox horizontally twice----------------------
            For i As Integer = 2 To 3
                lableArray(i) = New System.Windows.Forms.Label
                With lableArray(i)
                    .Size() = New Size(100, 22)
                    .Font = fnt
                    .Location = New System.Drawing.Point(30 + ((i - 2) * 350), 70)
                    .TextAlign = HorizontalAlignment.Right
                End With
                txtSuppArray(i) = New System.Windows.Forms.TextBox
                With txtSuppArray(i)
                    If i = 2 Then
                        .Size() = New Size(200, 22)
                    Else
                        .Size() = New Size(367, 22)
                    End If
                    .MaxLength = 30
                    .Location = New System.Drawing.Point(130 + ((i - 2) * 350), 70)
                    .TextAlign = HorizontalAlignment.Left
                End With
            Next
            lableArray(0).Text = "Code"
            lableArray(1).Text = "Name"
            lableArray(2).Text = "Part Number"
            lableArray(3).Text = "Notes"

            'Create button-----------------------------------
            With btnSup
                .Size() = New Size(45, 22)
                .Location = New System.Drawing.Point(285, 30)
                .Font = fnt
                .Text = "Btn"
            End With

            'Add controls onto tab control---------
            tabPage.Controls.Add(btnSup)
            tabPage.Controls.AddRange(lableArray)
            tabPage.Controls.AddRange(txtSuppArray)
            tabControl.Controls.Add(tabPage)
        Next

        Me.Controls.Add(tabControl)

    End Sub

Monday, December 13, 2010

find and replace regular expressions

using a ".txt" file stored on a particular path, and read the file when trying to replace the regex.
I use comma as a split delimiter in rows to separate one row into two columns. so that
string(0) = finding regex, string(1) = replacement


    Private Sub UpdateHTML(ByVal FilterFile As String)
        Dim line As String
        Dim sr As StreamReader = Nothing
        Dim converted As String

        converted = SaveHtml()



        Try
            sr = New StreamReader(FilterFile)
        Catch ex As Exception
            Exit Sub
        End Try
        'Replacing regex using .txt file stored on server-
        Dim find(0) As String
        Dim Replace(0) As String
        Do
            line = sr.ReadLine()
            If Not line Is Nothing Then
                Dim linecol() As String
                linecol = line.Split(",")
                ReDim Preserve find(find.Length)
                find(UBound(find)) = linecol(0)
                If linecol(1) = "empty" Then
                    ReDim Preserve Replace(Replace.Length)
                    Replace(UBound(Replace)) = ""
                Else
                    ReDim Preserve Replace(Replace.Length)
                    Replace(UBound(Replace)) = linecol(1)
                End If
            Else
                Exit Do
            End If
        Loop
        sr.Close()

        For k As Integer = 0 To UBound(find) - 1
            find(k) = find(k + 1)
            Replace(k) = Replace(k + 1)
        Next
        ReDim Preserve find(UBound(find) - 1)
        ReDim Preserve Replace(UBound(Replace) - 1)

        For i As Integer = 0 To find.Length - 1
            Dim regex As New Regex(find(i), RegexOptions.Multiline Or RegexOptions.Compiled Or RegexOptions.IgnoreCase)
            Dim regexReplace As String = Replace(i)
            converted = regex.Replace(converted, regexReplace)
        Next
        RTBsource.Text = converted
end sub

how to dynamically add controls to a form

i read through a file path to get all the file names & counts first:

    'retrive file names and counts from server to use to dynamically create convertion buttons.
    Private Sub getFileNames()
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(filepath)
            Dim fName As String = My.Computer.FileSystem.GetName(foundfile)
            fName = fName.Remove(fName.Length - 4)
            ReDim Preserve Filterfile(Filterfile.Length)
            Filterfile(UBound(Filterfile)) = fName.Substring(3)
        Next
        For k As Integer = 0 To UBound(Filterfile) - 1
            Filterfile(k) = Filterfile(k + 1)
        Next
        ReDim Preserve Filterfile(UBound(Filterfile) - 1)
        FileCount = Filterfile.Length
    End Sub

then using the counts and filenames to add tooltipstrip menuitems dynamically.


   Private Sub CreatemenuitemsArray()
        Dim ndx As Short
        Dim toolstriparray(FileCount - 1) As System.Windows.Forms.ToolStripMenuItem
        'Destroy any previous instances
        For ndx = 0 To FileCount - 1
            If ConversionsToolStripMenuItem.DropDownItems.Contains(toolstriparray(ndx)) Then
                With toolstriparray(ndx)
                    RemoveHandler .Click, AddressOf Me.toolstriparray_Click 'Detatch handler reference
                End With
                ConversionsToolStripMenuItem.DropDownItems.Remove(toolstriparray(ndx))
                toolstriparray(ndx).Dispose()
            End If
        Next

        'Create textbox controls
        For ndx = 0 To FileCount - 1
            toolstriparray(ndx) = New System.Windows.Forms.ToolStripMenuItem()
            With toolstriparray(ndx)
                .Text = Filterfile(ndx)
                AddHandler .Click, AddressOf Me.toolstriparray_Click 'Attach handler reference
            End With
        Next
        ConversionsToolStripMenuItem.DropDownItems.AddRange(toolstriparray)
    End Sub

    Private Sub toolstriparray_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        'click event handler, calls the update sub to format the html
        Dim file As ToolStripMenuItem
        Try
            file = DirectCast(sender, ToolStripMenuItem)
        Catch ex As Exception
            Exit Sub
        End Try
        Dim fName As String
        fName = "RE_" & file.Text & ".txt"
        Dim filt As String = filepath & fName
        Try
            UpdateHTML(filt)
        Catch ex As Exception
            Exit Sub
        End Try
        wbHTML.Document.Body.InnerHtml = bodycss & RTBsource.Text
    End Sub


Note: wbHTML is a web browser control.

Web browser controls

this morning i encounted a problem with web browser control:

webcontrol.documenttext returns funny popup messages everytime i change something in design or source code.

then i changed to webcontrol.document.body.innerhtml and it worked perfectly