Solved

How to dynamically create textboxes on textchage even of initial textbox

Posted on 2013-01-17
11
277 Views
Last Modified: 2013-01-22
Hi.  

I would like to create a form that will serve as a questionaire.  The user will enter all the questions on setup, so they may either want 1 question, up to 20 questions.

I want to add the first textbox, then on that textboxes textchanged event, I will add another textbox.  That I can do, but how do I add a third textbox on the dynamically created second textbox?

Any ideas?
0
Comment
Question by:Jasmin01
[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
  • 6
  • 5
11 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38791890
post your existing code.
0
 
LVL 39

Expert Comment

by:appari
ID: 38791893
you need to use addhandler method and add textchanged event handler to the newly added textbox.
0
 

Author Comment

by:Jasmin01
ID: 38791896
My existing code to create a new textbox on the textchanged event of the first textbox:

Private Sub txtDescr1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDescr1.TextChanged
        Dim tb As New TextBox()
        tb.Name = "tbTest"
        tb.Size = New Point(383, 20)
        tb.Location = New Point(158, 118)

        tb.Text = ""
        Me.Controls.Add(tb)

    End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jasmin01
ID: 38791899
Can you give me an example of how I would use the addhandler event?
0
 
LVL 39

Expert Comment

by:appari
ID: 38791915
try like this

        Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim tb As New TextBox()
        tb.Name = "tbTest"
        tb.Size = New Point(383, 20)
        tb.Location = New Point(CType(sender, TextBox).Location.X, CType(sender, TextBox).Location.Y + CType(sender, TextBox).Size.Height)

        tb.Text = ""
        Me.Controls.Add(tb)

        AddHandler tb.TextChanged, AddressOf txt_TextChanged
        RemoveHandler CType(sender, TextBox).TextChanged, AddressOf txt_TextChanged

    End Sub

    Private Sub Form10_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler txtDescr1.TextChanged, AddressOf txt_TextChanged
    End Sub

Open in new window

0
 

Author Comment

by:Jasmin01
ID: 38791922
Thanks, it works well.  How would I stop it from entering more than 20 textboxes?
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38791930
have a counter and check the value inside textchanged event
   Dim noOfQuestions As Integer = 1
    Const maxQuestions As Integer = 20
    Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        RemoveHandler CType(sender, TextBox).TextChanged, AddressOf txt_TextChanged
        If noOfQuestions = maxQuestions Then Return

        noOfQuestions += 1

        Dim tb As New TextBox()
        tb.Name = "tbTest"
        tb.Size = New Point(383, 20)
        tb.Location = New Point(CType(sender, TextBox).Location.X, CType(sender, TextBox).Location.Y + CType(sender, TextBox).Size.Height)

        tb.Text = ""
        Me.Controls.Add(tb)

        AddHandler tb.TextChanged, AddressOf txt_TextChanged


    End Sub

    Private Sub Form10_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler txtDescr1.TextChanged, AddressOf txt_TextChanged
    End Sub

Open in new window

0
 

Author Comment

by:Jasmin01
ID: 38792668
I have the following code below,  where I added a combobox when each textbox is dynamically drawn.  Now, what I tried to do, is dynamically create 3 controls based on the selection of the combobox, ie. if the user selects Yes/No, then 2 radio buttons must appear, if they select freetext, a texbox must appear, and if they select List, a listbox must appear.  I have added the code for the scenario where the yes/no is selected, but this only works for the first combobox, and not the ones that are dynamically created.

Public Class Form1

    Dim noOfQuestions As Integer = 1
    Const maxQuestions As Integer = 20

    Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        RemoveHandler CType(sender, TextBox).TextChanged, AddressOf txt_TextChanged
        If noOfQuestions = maxQuestions Then Return

        noOfQuestions += 1

        Dim tb As New TextBox()
        tb.Name = "tbTest"
        tb.Size = New Point(383, 20)
        tb.Location = New Point(CType(sender, TextBox).Location.X, CType(sender, TextBox).Location.Y + 10 + CType(sender, TextBox).Size.Height)

        Dim cboSelType1 As New ComboBox()
        cboSelType1.Name = "cboSelType"
        cboSelType1.Size = New Point(121, 21)
        cboSelType1.Location = New Point(CType(sender, TextBox).Location.X + 400, CType(sender, TextBox).Location.Y + 20 + CType(sender, TextBox).Size.Height)
        cboSelType1.Items.Insert(0, "<Selection Type>")
        cboSelType1.Items.Insert(1, "Yes/No")
        cboSelType1.Items.Insert(2, "FreeText")
        cboSelType1.Items.Insert(3, "List")

        tb.Text = ""
        Me.Controls.Add(cboSelType1)
        Me.Controls.Add(tb)

        cboSelType1.SelectedItem = cboSelType1.Items(0)
        AddHandler tb.TextChanged, AddressOf txt_TextChanged
        RemoveHandler CType(sender, TextBox).TextChanged, AddressOf txt_TextChanged
        AddHandler cboSelType1.SelectedValueChanged, AddressOf cboSelType_SelectedValueChanged
        'RemoveHandler CType(sender, ComboBox).SelectedValueChanged, AddressOf cboSelType_SelectedValueChanged

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
       
        AddHandler txtDescr1.TextChanged, AddressOf txt_TextChanged
    End Sub

    Private Sub cboSelType_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles cboSelType.SelectedValueChanged

        If cboSelType.SelectedIndex = 1 Then
            Dim rb1 As New RadioButton()
            Dim rb2 As New RadioButton()
            rb1.Name = "rdbYes"
            rb2.Name = "rdbNo"
            rb1.Size = New Point(60, 70)
            rb2.Size = New Point(60, 70)
            rb1.Text = "Yes"
            rb2.Text = "No"

            Me.Controls.Add(rb1)
            Me.Controls.Add(rb2)
            rb1.Location = New Point(CType(sender, ComboBox).Location.X + 150, CType(sender, ComboBox).Location.Y - 25)
            rb2.Location = New Point(CType(sender, ComboBox).Location.X + 210, CType(sender, ComboBox).Location.Y - 25)
        End If

    End Sub
End Class

Open in new window

0
 
LVL 39

Expert Comment

by:appari
ID: 38799510
in the cboSelType_SelectedValueChanged event handler you need to use sender object instead of using cboSelType.
replace cboSelType. with ctype(sender, ComboBox).
0
 

Author Comment

by:Jasmin01
ID: 38800010
Thanks.

I got it working now, so that if the user selects "Yes/No", a yes and no radio button appears, and if they select "Freetext". then a textbox appears.  The only issue now, is that, if I select freetext, the textbox appears.  Then if I change my selection to Yes/No, the radioboxes appear behind the textbox.  I would like the textbox to disappear and the radio buttons to appear.  Here is my current code:

If cboSelType.SelectedIndex = 1 Then
            Dim rb1 As New RadioButton()
            Dim rb2 As New RadioButton()
            rb1.Name = "rdbYes2"
            rb2.Name = "rdbNo2"
            rb1.Size = New Point(60, 17)
            rb2.Size = New Point(60, 17)
            rb1.Text = "Yes"
            rb2.Text = "No"

            Me.Controls.Add(rb1)
            Me.Controls.Add(rb2)
            rb1.Location = New Point(CType(sender, ComboBox).Location.X + 130, CType(sender, ComboBox).Location.Y)
            rb2.Location = New Point(CType(sender, ComboBox).Location.X + 210, CType(sender, ComboBox).Location.Y)
        ElseIf cboSelType.SelectedIndex = 2 Then
            Dim txtFreeText As New TextBox()
            txtFreeText.Size = New Point(224, 20)
            Me.Controls.Add(txtFreeText)
            txtFreeText.Location = New Point(CType(sender, ComboBox).Location.X + 150, CType(sender, ComboBox).Location.Y)

        End If

Open in new window

0
 

Author Closing Comment

by:Jasmin01
ID: 38804615
Thanks for your help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 45
vb.net 1 month apart 11 39
Create a datatable in vb.net dynamically 1 39
vb.net deleting excel sheet in workbook 11 34
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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