473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic created buttons in Vb.Net

115 New Member
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
Expand|Select|Wrap|Line Numbers
  1. Dim btnMenu As System.Windows.Forms.Button
  2.  
  3. Private Sub display_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         btnGO = New System.Windows.Forms.Button
  5.         btnGO.Size = New System.Drawing.Size(132, 28)
  6.         btnGO.Location = New System.Drawing.Point(18, 164)
  7.         btnGO.Tag = "GOBtn"
  8.         btnGO.Text = "GO"
  9.         AddHandler btnGO.Click, AddressOf btnGO_Click
  10.         Me.Controls.Add(btnGO)
  11.     End Sub
  12.  
  13.  Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  14.  myConnection.Open()
  15.  
  16.         Dim strSQL1 As String
  17.         'if Go button is clicked
  18.           If DirectCast(sender, Button).Tag.ToString = "GOBtn" Then
  19.                 strSQL1 = "select Item, Description from MG" & (txtValue + 1) & " where Main= " & txtValue & " order by Item ASC"
  20.            End If
  21.  
  22.             Dim myCommand1 As New OleDbCommand(strSQL1, myConnection)
  23.             Dim myreader1 As OleDbDataReader = myCommand1.ExecuteReader
  24.             While myreader1.Read
  25.                 ItemArray.Add(Trim(myreader1(0)))
  26.                 DescArray.Add(Trim(myreader1(1)))
  27.             End While
  28.             myreader1.Close()
  29.             'myConnection.Close()
  30.         End If
  31.  
  32.         'if any Menu Button is clicked
  33.         If DirectCast(sender, Button).Tag.ToString = "MenuBtn" Then
  34.             MainValue = Trim(MainArray(buttonTagNum).ToString)
  35.             SubValue = Trim(SubArray(buttonTagNum).ToString)
  36.  
  37.            If ItemValue = 2 Or ItemValue = 3 Then
  38.                 If ItemValue = 2 Then
  39.                     'if Itemvalue = 2, then need to check MG table
  40.                     strSQL1 = "select Item, Description where Main= " & MainValue & " and Sub= " & SubValue & " order by Item ASC"
  41.  
  42.                    Dim myCommand1 As New OleDbCommand(strSQL1, myConnection)
  43.                     Dim myreader1 As OleDbDataReader = myCommand1.ExecuteReader
  44.                     While myreader1.Read
  45.                         ItemArray.Add(Trim(myreader1(0)))
  46.                         DescArray.Add(Trim(myreader1(1)))
  47.                     End While
  48.                     myreader1.Close()
  49.                    ElseIf ItemValue = 3 Then
  50.                     'if Itemvalue = 3, then need to check MM table
  51.                     strSQL1 = "select Item, Description where Main= " & MainValue & " and Sub= " & Subvalue & " order by Item ASC"
  52.  
  53.                   Dim myCommand1 As New OleDbCommand(strSQL1, myConnection)
  54.                     Dim myreader1 As OleDbDataReader = myCommand1.ExecuteReader
  55.                     While myreader1.Read
  56.                         ItemButtonArray.Add(Trim(myreader1(0)))
  57.                         DescArray.Add(Trim(myreader1(1)))
  58.                     End While
  59.                     myreader1.Close()
  60.                     End If
  61.             End If
  62.         End If
  63.  
  64.         If ItemArray.Count - 1 > 0 And DescArray.Count - 1 > 0 Then
  65.             me.controls.clear()
  66.  
  67.             Dim x As Integer
  68.             Try
  69.             For x = 0 To 64 - 1  
  70.  
  71.                     If x <> 0 Then
  72.                         If x Mod 8 = 0 Then
  73.                              'start new row for buttons
  74.                         End If
  75.                     End If
  76.  
  77.                     For i = 0 To ItemButtonArray.Count - 1
  78.                         If x = ItemButtonArray(i).ToString Then
  79.                             ButtonText = Trim(DescArray(i).ToString)
  80.                             btnFontColor = Trim(FontColorArray(i).ToString)
  81.                             btnBackColor = Trim(BackColorArray(i).ToString)
  82.                             NoButtonDisplay = False
  83.                             Exit For
  84.                         Else
  85.                             NoButtonDisplay = True
  86.                         End If
  87.                     Next
  88.  
  89.                     btnMenu = New System.Windows.Forms.Button
  90.  
  91.                     If NoButtonDisplay = True Then
  92.                         'no need to display this location button. 
  93.                        If CheckNext = True Then
  94.                             If NextLine = True Then
  95.                                 'Get next row Locations for butons.                                 newHorizontal = locHorizontal
  96.                                 newVertical = newVertical + 44
  97.                                 btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
  98.                                 btnMenu.Visible = False
  99.                                 NextLine = False
  100.                                 GoTo GetNextButton
  101.                             ElseIf NextLine = False Then
  102.                                 newHorizontal = newHorizontal + 94
  103.                                 btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
  104.                                 btnMenu.Visible = False
  105.                                 GoTo GetNextButton
  106.                             End If
  107.                         End If
  108.                     End If
  109.  
  110.                     'Need to display these buttons
  111.                     If CheckNext = True Then
  112.                         If NextLine = True Then
  113.                             'get the next row horizontal and vertical location were button need to display
  114.                             newHorizontal = locHorizontal
  115.                             newVertical = newVertical + 44
  116.                             btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
  117.                             NextLine = False
  118.                         ElseIf NextLine = False Then
  119.                             'increment the horizontal location were next button need to display
  120.                             newHorizontal = newHorizontal + 94
  121.                             btnMenu.Location = New System.Drawing.Point(newHorizontal, newVertical)
  122.                         End If
  123.                     End If
  124.  GetNextButton:
  125.                     btnMenu.Size = New System.Drawing.Size(90, 40)
  126.                     btnMenu.Name = "RunTimeBtn" & CStr(x)
  127.                     btnMenu.Text = ButtonText
  128.                     btnMenu.ForeColor = System.Drawing.ColorTranslator.FromOle(btnFontColor)         
  129.                     btnMenu.BackColor = System.Drawing.ColorTranslator.FromOle(btnBackColor)
  130.                     btnMenu.Tag = "MenuBtn"
  131.                     Me.Controls.Add(btnMenu)
  132.  
  133.                     AddHandler btnMenu.Click, AddressOf btnGO_Click
  134.                 Next
  135.             Catch
  136.             End Try
  137.         End If
  138.  
  139.         myConnection.Close()
  140.  End Sub
  141.  
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.
Dec 5 '07 #1
2 2172
shweta123
692 Recognized Expert Contributor
Hi,

Please refer this
Example
Dec 6 '07 #2
remya1000
115 New Member
Thanks a lot for your. It works fine now.

As you said i tried this code...
Expand|Select|Wrap|Line Numbers
  1.                     If NoButtonDisplay = False Then
  2.                        'Button to display in Page
  3.                         For Each c As Control In Controls
  4.                             If c.Name = "RunTimeBtn" & x Then
  5.                                 c.Text = ButtonText
  6.                                 c.Visible = True
  7.                                 Exit For
  8.                             End If
  9.                         Next
  10.                     ElseIf NoButtonDisplay = True Then
  11.                         'Button not to display in Page
  12.                         For Each c As Control In Controls
  13.                             If c.Name = "RunTimeBtn" & x Then
  14.                                 c.Visible = False
  15.                                 Exit For
  16.                             End If
  17.                         Next
  18.                     End If
  19.  
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.
Dec 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library...
3
by: Grey | last post by:
I know i can use placeholder control for dynamic create controls. but my requirement is i need to create multiple controls. when the user click the button, one textbox will be created. If user...
1
by: Klom Dark | last post by:
I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in the table assigned to it's CommandArgument property and...
3
by: WebBuilder451 | last post by:
I have a series of dynamic link buttons created based upon a datareader. I've added a click event and it calls the sub ok: example: "while loop through the reader" Dim ltrCtrl As New...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
7
by: msdev | last post by:
Hello, I am creating my own webbrowser to learn VB .Net. I am stuck on an issue with regards to dynamically-created controls, in this case tabs on a tabcontrol and webbrowsers created within...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
5
by: Gui | last post by:
Hi, I'm working in C# .net 2005 with Ajax. I have a page that loads dynamic user controls depending on the scenario. In those user controls, I create dynamic linkbuttons. The user controls are...
2
Frinavale
by: Frinavale | last post by:
I've created a ASP.NET control that displays a "book" of schedules. It dynamically displays scheduling times and allows the user to page through the schedules. It also lets the user edit the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.