Solved

create controls dynamically in vb.net?

Posted on 2004-10-20
2
448 Views
Last Modified: 2010-04-24
Hi Experts,

I have to create the controls dynamicaly in my form according to the user's input in the pervious form. If the user inputs 5 in one then i have to create 5 label and 5 text boxes in my next form. And i like to put and get some values from the textboxes. How can I do that?

Thanks..

Regards,
Hems.
0
Comment
Question by:haihems
2 Comments
 
LVL 9

Expert Comment

by:checoo
ID: 12366596
inside a loop you can have something like the following to create the controls --
dim txt as textbox

for i = 1 to n (replace with your value)
 txt = new textbox
 'set values for atributes of txt like height, width, left,top etc
 yourformname.controls.add(txt)
next
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 50 total points
ID: 12366666
Here's a sample code to add Labels and TextBoxes Dynamically

Say on Click of Button1 you want to Create 5 Labels and 5 TextBoxes
Declare at Form level an Array

Dim Txt() As TextBox

Then on Button Click...

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Ct As Integer = 5 'This no. from User
        Dim i As Integer
        ReDim Txt(Ct)
        Dim Lbl As Label
        For i = 1 To Ct
            Txt(i) = New TextBox
            Lbl = New Label
            Lbl.Name = "Lbl" & i
            Lbl.Text = Lbl.Name
            Txt(i).Name = "Txt" & i
            Lbl.Top = 10 + i * 50
            Txt(i).Top = Lbl.Top
            Lbl.Left = 50
            Txt(i).Left = Lbl.Left + Lbl.Width + 10

            Lbl.Visible = True
            Txt(i).Visible = True
            Me.Controls.Add(Lbl)
            Me.Controls.Add(Txt(i))
            AddHandler Txt(i).TextChanged, AddressOf txtTextChanged
        Next
    End Sub

Here we are only storing References of TextBoxes in Array and not that of labels because you propbably won't require reading label Texts

Now I have wriite an AddHandler also, This is to Show you can even trap the events Now you can write

   Private Sub txtTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.text)
    End Sub


And For Reading the values of the TextBoxes all you need is Txt(i).Text

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question