Solved

creating buttons and array in vb.net code

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

622 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