473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Adding Controls at Runtime

Hi Brent,

I don't know if this is best practices per se`, but it
works.

The following generates a new button control each time
menu item 1 is clicked and assigns an event handler to
it. Each button uses the same default event handler.

In this case, when a button is clicked I simply display
the TabIndex of the clicked button.

<snip>
Private Sub MenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MenuItem1.Click

Dim newMemberButton As New System.Windows.Forms.Button

With newMemberButton
.Visible = True
.Dock = DockStyle.Left
.Size = New Size(96, 96)
End With

Me.Controls.Add(newMemberButton)
AddHandler newMemberButton.Click, AddressOf
newMemberButton_Click

End Sub

Private Sub newMemberButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs)

Dim tempButton As New Button
tempButton = sender
MessageBox.Show(tempButton.TabIndex)

End Sub
<end snip>

As you can see, you don't have to name each control. I'm
not the expert here, but my best guess is that when you
create the control object and assign it to the forms
control collection, all the form knows about the object
is the Address of it. But thats all it needs.

Hope this helps...

kurt
-----Original Message-----
Is it possible to add controls to a form at runtime. If
so, how would you write the Handles statement for an eventif the program can theoretically load thousands of
controls? I would have to programmatically name the
control so how could I get the event handler to run for
that control if I don't know the name of the control untilruntime?

Thanks
Brent
.

Nov 20 '05 #1
0 1996

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
8
by: Thomas H. | last post by:
Hi folks, what's the best way to add (web)controls at runtime ?? TIA T.H.
4
by: slylos | last post by:
I've got a Windows form that has a TabControl. I add TabPages to this TabControl at Runtime, along with two Textboxes and a button. The problem I'm having is I can't figure out how to reference...
2
by: Mark Siffer | last post by:
I am trying to add a compare validator at runtime. I would add it before but my input controls are built at runtime via an editable datagrid. Therefore, the names of the controls are not known...
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
3
by: Toe Dipper | last post by:
In short we have a lengthy process when a form is loaded that adds activex controls to our windows form. This process in itself works fine however we would like to push this processing to a thread...
0
by: sonic | last post by:
I am trying to dynamically load a validator and must be missing something elementary here. I extended TextBox control to add some functionality to it. One new feature it contains is IsRequired...
11
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.