Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Create and handle controls in runtime in VB 2008
 

Create and handle controls in runtime in VB 2008

hello, I am new in VB 2008
I have this problem, I wanna create on runtime an number of textboxes as array. (that i did)
For i = 1 To GivenNumbers
Dim tbTmp As New TextBox
tbTmp.Name = "TBNum" & i.ToString
tbTmp.Width = 25
tbTmp.Top = 10
tbTmp.Left = 5 + (i * 25)
Me.Panel1.Controls.Add(tbTmp)
Next

How can i use the events ?
Is that i did correct?

I want also to declare an Array of Integers in Form Class but it gives error or it doesn t get values
Public Class Form1
Private theNumbers(GivenNumbers) As Integer

....

For i=1 to x
theNumbers(i)= Num
next
Gives an index error

and last ...
How can i create on runtime column in datagrid?


Thx in advadage

e-freak  Monday, January 28, 2008 10:35 PM

1). You can use the AddHandler statement to add an event handler for a control dynamically, something like this:

Code Snippet


Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim j As Integer = 0

Do While (j < 10)

Dim tx As TextBox = New TextBox

AddHandler tx.TextChanged, AddressOf Me.tx_TextChanged

j = (j + 1)

Me.Controls.Add(tx)

Loop

End Sub

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

' stuff here

End Sub




2). For declaring array in VB.NET 9.0, you can read this document: Overview of Visual Basic 9.0

3). If you mean the .NET 1.x control DataGrid, then you should add a column to its data source at run time.
If you mean the DataGridView control, you can either adding a column to the DataGridView itself by code something like me.dataGridView.Columns.Add("newColumn","newColumn"),
or adding a column to the data source under-lying if the DataGridView is data bound.

Zhi-Xin Ye  Wednesday, January 30, 2008 1:36 PM

1). You can use the AddHandler statement to add an event handler for a control dynamically, something like this:

Code Snippet


Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim j As Integer = 0

Do While (j < 10)

Dim tx As TextBox = New TextBox

AddHandler tx.TextChanged, AddressOf Me.tx_TextChanged

j = (j + 1)

Me.Controls.Add(tx)

Loop

End Sub

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

' stuff here

End Sub




2). For declaring array in VB.NET 9.0, you can read this document: Overview of Visual Basic 9.0

3). If you mean the .NET 1.x control DataGrid, then you should add a column to its data source at run time.
If you mean the DataGridView control, you can either adding a column to the DataGridView itself by code something like me.dataGridView.Columns.Add("newColumn","newColumn"),
or adding a column to the data source under-lying if the DataGridView is data bound.

Zhi-Xin Ye  Wednesday, January 30, 2008 1:36 PM

Dim l As New Label


For


For

i = 0 To 10Try

l

= New Label

l.Name =

"label" & (i + 1)

l.Location =

New Point(8, y)

l.Height = 20

l.Width = 100

l.Text =

"Feature # " & (i + 1) & " : "

l.TextAlign = System.Drawing.ContentAlignment.MiddleRight

l.Tag =

" "

f.Controls.Add(l)
next

alrazgi  Thursday, April 23, 2009 10:12 PM

You can use google to search for other answers

Custom Search

More Threads

• Visual Inheritance
• Designtime mapping name problem
• DataGridViewComboBox Column DataBinding
• TableLayoutPanel troubles -- adding runtime controls
• Dock Padding
• Taking backup of SQL Server 2000 database in VB.Net 2005?
• Flicker and Crash problem in VS.NET 2005 Pro
• Failed to create component 'AxHost'. How to remove reference???
• How to set the hot key on a user control?
• White backround in user control