?
Solved

create controls dynamically in vb.net?

Posted on 2004-10-20
2
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

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