Solved

create controls dynamically in vb.net?

Posted on 2004-10-20
2
460 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

713 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