Solved

creating buttons and array in vb.net code

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

11 Experts available now in Live!

Get 1:1 Help Now