Dynamic created buttons

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
hai i'm using Vb.net.

i'm creating 64 dynamic created buttons of 8 rows and 8 columns. And i have 1 Go button, 1 textbox. those were created dynamically. if i enter one number inside textbox and hit Go Button, then it checks corresponding Database and display buttons on page. and inside Database i have a field called Item, that tells the button number to display.

for eg: if Database1 contain items 0,10,20,50. then i need to display button0 in 1st row 1st column, button10 in 2nd row 3rd column, button20 in 3rd row 5th column, buton50 in 7th row 3rd column in page. and rest of the buttons should be visible false.

for eg1: if Database2 contain items 1,8,17,63. then i need to display button1 in 1st row 2st column, button8 in 2nd row 1rd column, button17 in 3rd row 2th column, buton63 in 8th row 8rd column in page. and rest of the buttons should be visible false.

this is the code i'm using
VB.NET:
Dim btnMenu As System.Windows.Forms.Button

Private Sub display_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnGO = New System.Windows.Forms.Button
        btnGO.Size = New System.Drawing.Size(132, 28)
        btnGO.Location = New System.Drawing.Point(18, 164)
        btnGO.Tag = "GOBtn"
        btnGO.Text = "GO"
        AddHandler btnGO.Click, AddressOf btnGO_Click
        Me.Controls.Add(btnGO)
    End Sub

 Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 myConnection.Open()
              
        Dim strSQL1 As String
        'if Go button is clicked
          If DirectCast(sender, Button).Tag.ToString = "GOBtn" Then
                strSQL1 = "select Item, Description from MG" & (txtValue + 1) & " where Main= " & txtValue & " order by Item ASC"
           End If

            Dim myCommand1 As New OleDbCommand(strSQL1, myConnection)
            Dim myreader1 As OleDbDataReader = myCommand1.ExecuteReader
            While myreader1.Read
                ItemArray.Add(Trim(myreader1(0)))
                DescArray.Add(Trim(myreader1(1)))
            End While
            myreader1.Close()
            'myConnection.Close()
        End If

        'if any Menu Button is clicked
        If DirectCast(sender, Button).Tag.ToString = "MenuBtn" Then
            MainValue = Trim(MainArray(buttonTagNum).ToString)
            SubValue = Trim(SubArray(buttonTagNum).ToString)

           If ItemValue = 2 Or ItemValue = 3 Then
                If ItemValue = 2 Then
                    'if Itemvalue = 2, then need to check MG table
                    strSQL1 = "select Item, Description where Main= " & MainValue & " and Sub= " & SubValue & " order by Item ASC"

                   Dim myCommand1 As New OleDbCommand(strSQL1, myConnection)
                    Dim myreader1 As OleDbDataReader = myCommand1.ExecuteReader
                    While myreader1.Read
                        ItemArray.Add(Trim(myreader1(0)))
                        DescArray.Add(Trim(myreader1(1)))
                    End While
                    myreader1.Close()
                   ElseIf ItemValue = 3 Then
                    'if Itemvalue = 3, then need to check MM table
                    strSQL1 = "select Item, Description where Main= " & MainValue & " and Sub= " & Subvalue & " order by Item ASC"

                  Dim myCommand1 As New OleDbCommand(strSQL1, myConnection)
                    Dim myreader1 As OleDbDataReader = myCommand1.ExecuteReader
                    While myreader1.Read
                        ItemButtonArray.Add(Trim(myreader1(0)))
                        DescArray.Add(Trim(myreader1(1)))
                    End While
                    myreader1.Close()
                    End If
            End If
        End If

        If ItemArray.Count - 1 > 0 And DescArray.Count - 1 > 0 Then
            me.controls.clear()

            Dim x As Integer
            Try
            For x = 0 To 64 - 1  

                    If x <> 0 Then
                        If x Mod 8 = 0 Then
                             'start new row for buttons
                        End If
                    End If

                    For i = 0 To ItemButtonArray.Count - 1
                        If x = ItemButtonArray(i).ToString Then
                            ButtonText = Trim(DescArray(i).ToString)
                            btnFontColor = Trim(FontColorArray(i).ToString)
                            btnBackColor = Trim(BackColorArray(i).ToString)
                            NoButtonDisplay = False
                            Exit For
                        Else
                            NoButtonDisplay = True
                        End If
                    Next

                    btnMenu = New System.Windows.Forms.Button

                    If NoButtonDisplay = True Then
                        'no need to display this location button. 
                       If CheckNext = True Then
                            If NextLine = True Then
                                'Get next row Locations for butons.                                 newHorizontal = locHorizontal
                                newVertical = newVertical + 44
                                btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
                                btnMenu.Visible = False
                                NextLine = False
                                GoTo GetNextButton
                            ElseIf NextLine = False Then
                                newHorizontal = newHorizontal + 94
                                btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
                                btnMenu.Visible = False
                                GoTo GetNextButton
                            End If
                        End If
                    End If

                    'Need to display these buttons
                    If CheckNext = True Then
                        If NextLine = True Then
                            'get the next row horizontal and vertical location were button need to display
                            newHorizontal = locHorizontal
                            newVertical = newVertical + 44
                            btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
                            NextLine = False
                        ElseIf NextLine = False Then
                            'increment the horizontal location were next button need to display
                            newHorizontal = newHorizontal + 94
                            btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
                        End If
                    End If
 GetNextButton:
                    btnMenu.Size = New System.Drawing.Size(90, 40)
                    btnMenu.Name = "RunTimeBtn" & CStr(x)
                    btnMenu.Text = ButtonText
                    btnMenu.ForeColor = System.Drawing.ColorTranslator.FromOle(btnFontColor)         
                    btnMenu.BackColor = System.Drawing.ColorTranslator.FromOle(btnBackColor)
                    btnMenu.Tag = "MenuBtn"
                    Me.Controls.Add(btnMenu)
                   
                    AddHandler btnMenu.Click, AddressOf btnGO_Click
                Next
            Catch
            End Try
        End If

        myConnection.Close()
 End Sub

when i give one values inside textbox and hit Go button, it displays the buttons in page. and if i enter another values inside textbox and hit Go button, then i'm clearing all controls in page and creating new buttons in page. this makes the program slow to display buttons in page.

so i'm finding anotherway to make the program faster. we were creating 64 buttons. so without deleting all created controls, whether we can use those created buttons nexttime and that will make the program faster. according to the datatable make each button visible or invisible and change the button text. is that help to make program faster.

and i tried giving the about codes inside a function and when go button is hit for the first time then we will call that function and create 64 buttons. and from the second time onwards we need to change the button text and make button visible or invisible according to database.

if we click any of this button we will know which button clicked using Addhandler. but from second time onwards how can we say which button to visible and give corresponding button text. buttons were created during run time. so i can't specify the button name (to assign button text and visible property to button). for eg: in runtime how can i tell change Button0's text to this.

if you have any idea how to do this please let me know and if you can provide an example, then it will be great help for me.

thanks in advance.
 
a function method that can return a button by its name:
VB.NET:
Function getControl(ByVal name As String) As Control
    For Each c As Control In Controls
        If c.Name = name Then Return c
    Next
    Return Nothing
End Function
 
all you need to do is keep track of which buttons you set
VB.NET:
.visible = true
the first time, then when you want to reset it, set those buttons
VB.NET:
.visible = false
, choose your new buttons, set
VB.NET:
.visible = true
and keep track of which buttons are visible + so on
 
Thanks a lot for your help johnH and Paul. It works fine now.

As you said i tried this code...
VB.NET:
                    If NoButtonDisplay = False Then
                       'Button to display in Page
                        For Each c As Control In Controls
                            If c.Name = "RunTimeBtn" & x Then
                                c.Text = ButtonText
                                c.Visible = True
                                Exit For
                            End If
                        Next
                    ElseIf NoButtonDisplay = True Then
                        'Button not to display in Page
                        For Each c As Control In Controls
                            If c.Name = "RunTimeBtn" & x Then
                                c.Visible = False
                                Exit For
                            End If
                        Next
                    End If

Now i can take each button i created during runtime and change there text or make then visible or invisible. Thanks a lot for this help.
 
Back
Top