Solved

create controls dynamically in vb.net?

Posted on 2004-10-20
2
426 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now