Solved

creating buttons and array in vb.net code

Posted on 2011-02-18
2
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

738 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