473,763 Members | 8,483 Online
Bytes | Software Development & Data Engineering Community
+ 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 2208
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
2924
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 that sets the properties send/passed from the main windows form. I'm having problems with the class library, the button control collection and my referencing it ie this.Control.Add(aControl);. Any and all help is appreciated. Thanks in advance.
3
1528
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 click three times, there will be three textbox created. However, just only one textbox was created whatever times I click. PLs help. Million thanks
1
2234
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 the name of a common command (btn_Command) assigned to it's Command property. The creation of the table is done by a function called drawGeo. drawGeo is called during the initial Page_Load (!IsPostBack), but should be called by btn_Command on...
3
1862
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 LiteralControl Dim lbtnCtrl As New LinkButton ltrCtrl.Text = "<br>" lbtnCtrl.Text = "WE: " & dtrCal(10).ToString lbtnCtrl.ToolTip = dtrCal(10).ToString & " these events" & dtrCal(1) lbtnCtrl.ID = "wecc" & i.ToString
3
13753
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. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
7
2558
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 each new tab. Using AddHandler in a class, I can get the delegate Sub to display the name of the webbrowser that has been clicked (by using msgbox(sender.tag), where I set the .tag value when creating the control). What I need to be able to do is...
13
10184
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 first order. Likewise if he clicks the second LinkButton, he will be shown the details of the second order he had placed. The Text of the LinkButtons will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the LinkButtons. The problem is...
5
1858
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 loaded on the .aspx PageLoad and the linkbuttons are created on the .ascx PageLoad. The problem I have is that those linkbuttons don't work on the first click. I have to make 2 clicks to have their event fired. What can I do for that ? Thanks.
2
4464
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 schedule times if the user has permissions to edit schedules. When the user is not editing, or the user is not allowed to edit, the buttons that let the user edit are not displayed. If the user is allowed to edit schedules but they are not editing a link...
0
9564
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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 we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.