5 Replies - 765 Views - Last Post: 22 June 2012 - 11:55 AM Rate Topic: -----

#1 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Create a Button on Button Click in vb.net

Posted 20 June 2012 - 07:28 AM

Hello Everybody

I want to create something like this in vb.net

I have a Button called "btn1" on my Form called "frmMain".

In btn1_Click() event
'Check if btn2 exists
'If not then
'Code for Creating btn2 just below btn1
'End if
End Sub

After btn2 is Created

In btn2_Click() event
'Check if btn3 exists
'If not then
'Code for Creating btn3 just below btn2
'End if
End Sub

And So on... for many many buttons

But i dont know where to start from

Any help will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Create a Button on Button Click in vb.net

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Create a Button on Button Click in vb.net

Posted 20 June 2012 - 08:15 AM

before start you must to know how to:
1. create List of buttons to manipulate your buttons.
Private buttons As New List (Of Button)

2. create new button, add new button to the list and to the form, assign event handler.
Dim btn as New Button
'btn.Location = ...
'btn.Position = ...
buttons.add (btn)
me.Controls.Add (btn)
AddHandler btn.Click, AddressOf btn_Click


2. create event handler for all buttons for recognizing which button is pressed.
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = sender
If btn Is Buttons(0) Then
'button 0 is pressed!
End If
If btn Is Buttons(1) Then
' button 1 is pressed!
End If
'and so on...
End Sub

If something is not clear just ask.
Was This Post Helpful? 1
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 215
  • View blog
  • Posts: 1,408
  • Joined: 16-March 11

Re: Create a Button on Button Click in vb.net

Posted 20 June 2012 - 08:34 AM

I think what's not clear is Objects. Once you have a firm understanding of Objects, classes for objects(such as Buttons, Forms, etc) You will not be completely stumped at the thought of dynamically creating anything.

Fundamentals of OOP : VB.Net
Was This Post Helpful? 0
  • +
  • -

#4 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Re: Create a Button on Button Click in vb.net

Posted 22 June 2012 - 02:52 AM

Thanks Sela007 your post was somewhat Helpful
I have some problem
Can you reply once more......

If btn(i) already exists then
Clicking on btn(i-1) should not create btn(i)
End If


In Short i want the code for what i wrote in bold

Thanks for reply and +1
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 215
  • View blog
  • Posts: 1,408
  • Joined: 16-March 11

Re: Create a Button on Button Click in vb.net

Posted 22 June 2012 - 07:30 AM

Sela, he needs to do his own work he's not shown how he's researched or attempted to help himself. He's only presented that he's here for code hand outs.

@OP you've seen one thing very important here, a list. I suggest you use another variable and append the count to all buttons created. This variable should be the count of items in the button list. Once you've done this you should consider a sub routine which creates buttons using this method.

If you have questions on this method then look up each step, write some code and If you dont figure it out then come back here, post your code and show us the errors.

I'll even give you possible google searches..

"for loop list(of t)"
"get count list(of t)"

Using the already built in intellisense this should be cake though. Here at D.I.C. We don't hand out code, we're here to point you in the right direction usually after you've shown us your effort.

This post has been edited by trevster344: 22 June 2012 - 07:32 AM

Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Create a Button on Button Click in vb.net

Posted 22 June 2012 - 11:55 AM

View PostVishal1419, on 22 June 2012 - 02:52 AM, said:

Thanks Sela007 your post was somewhat Helpful
I have some problem
Can you reply once more......

If btn(i) already exists then
Clicking on btn(i-1) should not create btn(i)
End If


In Short i want the code for what i wrote in bold

Thanks for reply and +1

trevster344 is right. If I give you the code it will not suit the purpose and rules of the forum,especially when the problem is simple like this. Try to do something on your own.Learn the basics of OOP programming. The OOP fundamentals posted by trevster344 is a good start. Then you will learn that everything is an object, and how to check for existence of an object (btn(i)).

If we talking about lists, then if btn(i) is the button in the list of buttons(btn), the question is: How to find if button number i exist in the list. So if i=7 and number of buttons in the list is 5, it's obvious that btn(7) doesn't exist.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1