Monday, December 13, 2010

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.

No comments:

Post a Comment