Solved

create controls dynamically in vb.net?

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

718 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