Solved

creating buttons and array in vb.net code

Posted on 2011-02-18
2
151 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now