Solved

creating buttons and array in vb.net code

Posted on 2011-02-18
2
152 Views
Last Modified: 2012-05-11
I have a tab control that I automatically create tabpages based on the number of groups I received from a dataset.  No I want to go through each of those groups, and add a panel and 25 butttons to each panel based on number of items in that group.  So If I have a panel of 25 buttons, 25 items can fit on a page.  If there are more than 25 items, but less than 51 I need to panels of 25 buttons on this tabpage.   So how can I add the panel or panels to tab page then added the appropriate number of buttons to the panel, and use the button, name, tag, and text fields to fill in the values for that perticular button.

Attached is the code I have so far.

Private Function FillForm() As Boolean
        Dim da As New SqlDataAdapter
        Dim myCmd As New SqlCommand
        Dim myConn As New SqlConnection
        Dim x As Integer ' page count needed on tab
        myConn.ConnectionString = m_connectstring
        With myCmd
            .Connection = myConn
            .CommandText = "SELECT * FROM TAG_InspectionItems"
            .CommandType = CommandType.Text
        End With
        da.SelectCommand = myCmd
        Try
            myConn.Open()
            da.Fill(dsItems, "Items")
            dsItems.WriteXml("C:\AptInspections\Items.xml")
            myConn.Close()
        Catch ex As Exception
            If myConn.State = ConnectionState.Open Then
                myConn.Close()
            End If
        End Try

        Dim i As Integer
        For i = 0 To TABInspection.TabPages.Count - 1
            dsItems.Tables(0).DefaultView.RowFilter = "category = " & TABInspection.TabPages(i).Tag
            Dim test As Integer = dsItems.Tables(0).DefaultView.Count

            TABInspection.TabPages(i).Select()
            If test > 25 Then
                If test > 50 Then
                    If test > 75 Then
                        ' I need 3 pages
                    Else
                        ' I need 2 pages
                    End If
                Else

                    ' only need one page
                    For x = 1 To 2
                        Dim myPage As New System.Windows.Forms.Panel
                        Dim mySize As System.Drawing.Size
                        mySize.Height = "369"
                        mySize.Width = "714"
                        With myPage
                            .Text() = "Page 1"
                            .Size() = mySize
                            .BackColor() = System.Drawing.Color.Blue
                        End With
                        Dim y As Integer
                        For y = 0 To dsItems.Tables(0).Rows.Count - 1
                            Dim myButton As New System.Windows.Forms.Button
                            Dim myButtonSize As New System.Drawing.Size
                            myButtonSize.Width = 100
                            myButtonSize.Height = 55
                            With myButton
                                .Name() = dsItems.Tables(0).Rows(y).Item(1).ToString
                                .Text() = dsItems.Tables(0).Rows(y).Item(2).ToString
                                .Size() = New Size(myButtonSize)
                                .FlatStyle() = FlatStyle.Popup
                            End With
                        Next y
                    Next x

                End If

            Else
                For x = 1 To 1
                    Dim myPanel As New System.Windows.Forms.Panel
                    Dim mySize As System.Drawing.Size
                    mySize.Height = "369"
                    mySize.Width = "714"
                    With myPanel
                        .Text() = "Page 1"
                        .Size() = mySize

                        .BackColor() = System.Drawing.Color.LightBlue
                    End With
                    TABInspection.TabPages(i).Controls.Add(myPanel)
                    Dim y As Integer
                    For y = 0 To test
                        Dim myButton As New System.Windows.Forms.Button
                        Dim myButtonSize As New System.Drawing.Size
                        myButtonSize.Width = 100
                        myButtonSize.Height = 55
                        With myButton
                            .Name() = dsItems.Tables(0).Rows(y).Item(1).ToString
                            .Text() = dsItems.Tables(0).Rows(y).Item(2).ToString
                            .Size() = New Size(myButtonSize)
                            .FlatStyle() = FlatStyle.Popup
                        End With
                        myPanel.Controls.Add(myButton)
                    Next y
                    TABInspection.TabPages(i).Controls.Add(myPanel)
                Next x
            End If

        Next
    End Function

Open in new window

0
Comment
Question by:mgmhicks
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34927959
Haven't look closely at your code...

Couldn't you just use a For Loop with a "Step 25" to get the right number of pages needed?

    For i As Integer = 1 to test Step 25
   
    Next

Also, do you need events for these buttons?  If so, use the AddHandler() function.
0
 

Author Comment

by:mgmhicks
ID: 35012399
I apologize for not getting back.  I'm have to create tabs at runtime and also create the number of buttons on each tab.   Not sure how else to handle this.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question