Print Page | Close Window

VB.NET -- Dynamically created controls

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=10590
Printed Date: 28 March 2026 at 5:31pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: VB.NET -- Dynamically created controls
Posted By: pmormr
Subject: VB.NET -- Dynamically created controls
Date Posted: 24 May 2004 at 7:36pm
is there any way to a function to a dynamically created button on a Win Form for VB.NET? Even if it could inherit the functions and attributes from another button it would be fine.

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/



Replies:
Posted By: Mart
Date Posted: 25 May 2004 at 2:53am

Dim YourButton As New System.Web.UI.Button

YourButton.Text = "Your text"

YourButton.OnClick = New EventHandler(AddressOf YourButton_OnClick)

Then put a place holder where you want the button to go and use

[PlaceHolder].Controls.Add(YourButton)

Or add it straight to the page without a placeholder like this:

Me.Controls.Add(YourButton)



Posted By: pmormr
Date Posted: 25 May 2004 at 3:48pm

win forms... it gives me an error

C:\Documents and Settings\Administrator\Desktop\Programming Development\dotNET\Comnk\Form1.vb(88): 'System.Windows.Forms.Control.Protected Overridable Overloads Sub OnClick(e As System.EventArgs)' is not accessible in this context because it is 'Protected'.



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date Posted: 26 May 2004 at 2:50am
Just remove the 'Protected' and change it to 'Public'


Posted By: pmormr
Date Posted: 26 May 2004 at 3:26pm
the protected where? i never declared it protected

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 26 May 2004 at 3:29pm

the code i'm trying to use this with is...

Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter

Dim location As System.Drawing.Point

Dim x As Integer

Dim y As Integer

x = CInt(Int((1024 * Rnd()) + 1))

y = CInt(Int((768 * Rnd()) + 1))

location.X = x

location.Y = y

Button1.Location = location

Dim buttonarray As Collection

Dim btn(1) As Button

Dim i As Integer = 0

For i = 0 To 1

btn(i) = New Button()

x = CInt(Int((1024 * Rnd()) + 1))

y = CInt(Int((768 * Rnd()) + 1))

btn(i).Location = New System.Drawing.Point(x, y)

btn(i).Name = "btn(" & i & ")"

btn(i).Size = New System.Drawing.Size(120, 40)

btn(i).Text = "CLICK ME!"

Me.Controls.Add(btn(i))

Next

End Sub



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 26 May 2004 at 3:30pm
wait... does it need to be in a different method that doesn't handle button1.mouseenter?

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 26 May 2004 at 3:33pm
nope... same problem

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date Posted: 26 May 2004 at 3:40pm

It will be declared in the Windows Forms Designer Code Region



Posted By: pmormr
Date Posted: 26 May 2004 at 6:45pm

tried both public and friend... neither worked... maybe i should just give up. .onclick doesn't even show up in the intellisense...



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Leeb65
Date Posted: 27 May 2004 at 2:28am

try just Click, that's the function for windows forms.

 



-------------
Lee

http://www.rheinsitemedia.de">


Posted By: Mart
Date Posted: 27 May 2004 at 11:24am

Stumbled accross this which was pretty lucky

http://www.aspfree.com/c/a/.NET/Dynamically-Adding-Controls-to-a-Windows-Form/ - http://www.aspfree.com/c/a/.NET/Dynamically-Adding-Controls- to-a-Windows-Form/



Posted By: pmormr
Date Posted: 27 May 2004 at 3:21pm

this .click method shows this error

'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

maybe if i told you what i'm doing it would be a little bit easier... what i need to happen is when the mouse comes over a button is for two more identical buttons to be created that do the same thing. and when the mouse comes over any of those three buttons they create two more and so on...



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 27 May 2004 at 3:23pm
speaking of that error... there must be a way to set the method... there wouldn't be any point in putting an option to raise an event on an empty method in the framework

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date Posted: 27 May 2004 at 3:31pm

Well the JIT debugger has answred your question

just use

RaiseEvent Click(Me, Nothing)

instead



Posted By: pmormr
Date Posted: 27 May 2004 at 3:35pm

that won't set what the function does... only call a blank method



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date Posted: 27 May 2004 at 3:44pm

What do you actually want to do? Run a sub when the button is clicked?

 



Posted By: pmormr
Date Posted: 27 May 2004 at 9:52pm
on the MouseOver event run a sub

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date Posted: 28 May 2004 at 2:39am
[Button].OnMouseOver += New EventHandler(AddressOf SubName)


Posted By: pmormr
Date Posted: 28 May 2004 at 3:37pm

that gives me the same protected error... i just had an idea that might work better. Is there anyway that i can simply copy the button i'm using as a base and set it to the new button i just created dynamically. I tried using this...

dynambtn = basebutton

but that's literally setting it to the actual button so i'm only manipulating the original button and it doesn't add two more. I wish there was a function called button.copyattributes so it would make a new, but exactly identical button.



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 28 May 2004 at 3:39pm
i'll make a post over at ASP.net and see if anyone there has any suggestions on the topic.

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date Posted: 28 May 2004 at 3:45pm
It's windows forms! Post it at DevJunkies.com or WindowsForms.net


Posted By: pmormr
Date Posted: 28 May 2004 at 3:50pm

i'll post a reference to the thread on both... i need all the help i can get



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 28 May 2004 at 3:50pm
and then once it's solved i'll write an article on DJ about this god darned topic

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: plaudeman
Date Posted: 28 May 2004 at 6:25pm

Originally posted by pmormr pmormr wrote:

is there any way to a function to a dynamically created button on a Win Form for VB.NET? Even if it could inherit the functions and attributes from another button it would be fine.

Sure, it's fairly simple. Here is an example:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim b As New Button
b.Text = "Click Me"

AddHandler b.Click, AddressOf B_Click

Me.Controls.Add(b)

End Sub

Private Sub B_Click(ByVal sender As Object, ByVal e As EventArgs)

MsgBox("Button was clicked!")

End Sub



Posted By: pmormr
Date Posted: 28 May 2004 at 7:20pm
you have got to be F***ING kidding me... i owe you one... or two... or three... thanks a bunch

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 28 May 2004 at 7:21pm
it was that simple all along... thank god that one's solved

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net