Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 5th, 2007, 06:10 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic created buttons in Vb.Net

hai i'm using Vb.net 1.1.

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
Code:
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.
 
Old December 6th, 2007, 04:20 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works fine now. i tried this code...
Code:
                    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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic image is not being created on crystal repo prat_0510 Crystal Reports 0 May 23rd, 2006 06:44 AM
dynamic buttons harpua Flash (all versions) 3 October 19th, 2004 07:33 PM
Dynamic images from VB .net to CR .net. Given Path Alita Crystal Reports 0 September 22nd, 2004 01:51 AM
Create dynamic label in vb.net snowydust VB How-To 1 July 24th, 2004 05:32 AM
Dynamic Buttons? TSEROOGY Javascript How-To 6 September 4th, 2003 11:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.