Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27004

VS 2012 [RESOLVED] Add labels and textbox's during runtime

$
0
0
I'm trying to add a textbox and a label during runtime when the users presses a button. When I press the button a label and textbox is created, when I click it again nothing happens. Here is the code I have:

vb.net Code:
  1. [CODE]    
  2.     Dim CreatedGroupBox As New GroupBox
  3.     Dim CreatedCombobox As ComboBox
  4.     Dim CreatedTextBox As TextBox
  5.     Dim CreatedCheckBox As CheckBox
  6.     Dim CreatedRadioButton As RadioButton
  7.     Dim RadioButtonCount As Integer
  8.  
  9.     Dim DetailsLabel As Label
  10.     Dim DetailsTextbox As TextBox
  11.     Dim DetailsButton As New Button
  12.     Dim DetailsCombobox As New ComboBox
  13. Private Sub radRadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles radRadioButton.CheckedChanged
  14.         If radRadioButton.Checked = True Then
  15.  
  16.             '//SETUP DETAILS
  17.             grpDetails.Controls.Clear()
  18.             '//Radiobutton 1 label's label
  19.             DetailsLabel = New Label
  20.             DetailsLabel.Name = "lblRadioButtonName" & RadioButtonCount
  21.             DetailsLabel.Text = "Radio Button " & RadioButtonCount + 1 & " label:"
  22.             DetailsLabel.AutoSize = True
  23.             DetailsLabel.Location = New Point(6, 22)
  24.             grpDetails.Controls.Add(DetailsLabel)
  25.  
  26.             '//Radio button 1 label's textbox
  27.             DetailsTextbox = New TextBox
  28.             DetailsTextbox.Name = "txtRadioButtonName" & RadioButtonCount
  29.             DetailsTextbox.Text = "Radio Button " & RadioButtonCount + 1
  30.             DetailsTextbox.Location = New Point(DetailsLabel.Location.X + DetailsLabel.Width, 19)
  31.             AddHandler DetailsTextbox.TextChanged, AddressOf RadioButtonName
  32.             grpDetails.Controls.Add(DetailsTextbox)
  33.  
  34.             '//Add radiobutton button
  35.             DetailsButton.Text = "+"
  36.             DetailsButton.Name = "btnAddRadioButton"
  37.             DetailsButton.Location = New Point(DetailsTextbox.Location.X + DetailsTextbox.Width + 5, 19)
  38.             DetailsButton.Size = New Size(20, 20)
  39.             AddHandler DetailsButton.Click, AddressOf AddRadioButton
  40.             grpDetails.Controls.Add(DetailsButton)
  41.  
  42.             '//Add Detfault selection label
  43.             DetailsLabel = New Label
  44.             DetailsLabel.Name = "lblDefaultRadioButton"
  45.             DetailsLabel.Text = "Default Radio Button:"
  46.             DetailsLabel.AutoSize = True
  47.             DetailsLabel.Location = New Point(6, DirectCast(Me.Controls.Find("lblRadioButtonName" & RadioButtonCount, True)(0), Label).Location.Y + 29)
  48.             grpDetails.Controls.Add(DetailsLabel)
  49.  
  50.             '//Add Default selection combobox
  51.             DetailsCombobox.Items.Add(DetailsTextbox.Text)
  52.             DetailsCombobox.Location = New Point(DetailsLabel.Location.X + DetailsLabel.Width, DirectCast(grpDetails.Controls.Find("lblRadioButtonName" & RadioButtonCount, True)(0), Label).Location.Y + 25)
  53.             DetailsCombobox.DropDownStyle = ComboBoxStyle.DropDownList
  54.             DetailsCombobox.SelectedIndex = 0
  55.             grpDetails.Controls.Add(DetailsCombobox)
  56.  
  57.             RadioButtonCount += 1
  58.  
  59.         End If
  60.     End Sub    
  61.  
  62. Public Sub AddRadioButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
  63.  
  64.         '//Move the default selection combobox and label and button down
  65.         DetailsCombobox.Location = New Point(DetailsCombobox.Location.X, DetailsCombobox.Location.Y + 30)
  66.         DirectCast(grpDetails.Controls.Find("lblDefaultRadioButton", True)(0), Label).Location = New Point(6, DetailsCombobox.Location.Y + 3)
  67.         DetailsButton.Location = New Point(DetailsButton.Location.X, DetailsButton.Location.Y + 30)
  68.  
  69.         '//Create new label
  70.         DetailsLabel = New Label
  71.         DetailsLabel.Name = "lblRadioButtonName" & RadioButtonCount
  72.         DetailsLabel.Text = "Radio Button " & RadioButtonCount + 1 & " label:"
  73.         DetailsLabel.AutoSize = True
  74.         DetailsLabel.Location = New Point(6, DirectCast(grpDetails.Controls.Find("lblRadioButtonName" & RadioButtonCount - 1, True)(0), Label).Location.X + 43)
  75.         grpDetails.Controls.Add(DetailsLabel)
  76.  
  77.         '//Create new radio button
  78.         DetailsTextbox = New TextBox
  79.         DetailsTextbox.Name = "txtRadioButtonName" & RadioButtonCount
  80.         DetailsTextbox.Text = "Radio Button " & RadioButtonCount + 1
  81.         DetailsTextbox.Location = New Point(DetailsLabel.Location.X + DetailsLabel.Width, DirectCast(grpDetails.Controls.Find("lblRadioButtonName" & RadioButtonCount - 1, True)(0), Label).Location.X + 40)
  82.         AddHandler DetailsTextbox.TextChanged, AddressOf RadioButtonName
  83.         grpDetails.Controls.Add(DetailsTextbox)
  84.  
  85.         RadioButtonCount += 1
  86.     End Sub[/CODE]

The default label and default combobox move down, but it wont create another label and textbox. Any ideas?

Viewing all articles
Browse latest Browse all 27004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>