This is my code for dynamic textbox controls in button click event. The code is working well. If i click the button 3 times, it is generated 3 text boxes. But I have no idea to assign text box values to a variable. I dont know the names of dynamic generated controls. if i want to add value to 3rd text box, how to do it?
Dim txtBx As TextBox
Static x As Integer
Static i As Integer
txtBx = New TextBox
txtBx.Location = New Point(10, 10 + x)
txtBx.Size = New Size(100, 20)
i = i + 1
x = x + 20
Me.Controls.Add(txtBx)
if i create normal textbox i can do it with,
TextBox3.Text = "Some value"
But I dont know to do this for dynamic controls.
Here's an example, storing the references in a List(Of Textbox):
Public Class Form1
Private tbList As New List(Of TextBox)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim tb As TextBox
Dim n As Integer
n = tbList.Count + 1
tb = New TextBox
With tb
.Location = New Point(10, 10 + (n * 20))
.Name = "dynTB" & n.ToString
.Size = New Size(100, 20)
End With
Me.tbList.Add(tb)
Me.Controls.Add(tb)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' Testing:
If Me.tbList.Count >= 3 Then Me.tbList(2).Text = "This is textbox 3"
End Sub
End Class
Related
I want to create a simple addition program in vb.net form application. Also i want to create all controls programmatically. I have three text boxes and one button. When the button is clicked , it take value from two text boxes and assign it to the third text box value.
I am unable to get text boxes string from button click handle.
My coding is as follows:
Public Class Form1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Pushbutton
Dim PushButton As New Button()
PushButton.Text = "Add"
PushButton.Location = New Point(10, 100)
Me.Controls.Add(PushButton)
AddHandler PushButton.Click, AddressOf myButtonHandler_Click
'
'TextBox1
Dim TextBox1 As New TextBox
TextBox1.Location = New Point(10, 3)
Me.Controls.Add(TextBox1)
'TextBox2
Dim TextBox2 As New TextBox
TextBox2.Location = New Point(200, 3)
Me.Controls.Add(TextBox2)
Dim TextBox3 As New TextBox
TextBox3.Location = New Point(200, 100)
Me.Controls.Add(TextBox3)
End Sub
Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Public Function ADD(x As Double, y As Double) As Double
ADD = x + y
End Function
End Class
Moving the declaration of the controls to Form level allows them to be seen in all the methods of the form.
Public Class Form3
Private TextBox1 As New TextBox
Private TextBox2 As New TextBox
Private TextBox3 As New TextBox
Private PushButton As New Button
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Pushbutton
PushButton.Text = "Add"
PushButton.Location = New Point(10, 100)
Me.Controls.Add(PushButton)
AddHandler PushButton.Click, AddressOf myButtonHandler_Click
'TextBox1
TextBox1.Location = New Point(10, 3)
Controls.Add(TextBox1)
'TextBox2
TextBox2.Location = New Point(200, 3)
Controls.Add(TextBox2)
'TextBox3
TextBox3.Location = New Point(200, 100)
Controls.Add(TextBox3)
End Sub
Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim firstNumber = CDbl(TextBox1.Text)
Dim secondNumber = CDbl(TextBox2.Text)
TextBox3.Text = ADD(firstNumber, secondNumber).ToString
End Sub
Public Function ADD(x As Double, y As Double) As Double
ADD = x + y
End Function
End Class
You can technically do it the way you were, but only if you use an anonymous event handler for the button click event as shown below:
Public Class Form1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TextBox1
Dim TextBox1 As New TextBox
TextBox1.Location = New Point(10, 3)
Me.Controls.Add(TextBox1)
'TextBox2
Dim TextBox2 As New TextBox
TextBox2.Location = New Point(200, 3)
Me.Controls.Add(TextBox2)
Dim TextBox3 As New TextBox
TextBox3.Location = New Point(200, 100)
Me.Controls.Add(TextBox3)
'Pushbutton
Dim PushButton As New Button()
PushButton.Text = "Add"
PushButton.Location = New Point(10, 100)
Me.Controls.Add(PushButton)
AddHandler PushButton.Click, Sub()
Dim dblA, dblB As Double
If Double.TryParse(TextBox1.Text, dblA) AndAlso Double.TryParse(TextBox2.Text, dblB) Then
TextBox3.Text = ADD(dblA, dblB)
Else
MessageBox.Show("One or more Invalid Inputs")
End If
End Sub
End Sub
Public Function ADD(x As Double, y As Double) As Double
ADD = x + y
End Function
End Class
Though I do recommend moving those control declarations out to Form level as Mary suggested.
I'v created a program that dynamically creates text-boxes according to the number selected from the combobox. The problem is that when I change the number from a big number to a smaller one the text-boxes keep the same ex(10 then 3).
see this image
I tried entering a code to remove the text-boxes before the *for loop but it didn't work.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
num = ComboBox1.Text
Dim tp As TabPage = tabControl1.TabPages(0)
*For i = 1 To num
Dim TB As New TextBox
TB.Size = New System.Drawing.Size(60, 20)
TB.Font = New System.Drawing.Font("arial", 20)
TB.Location = New Point(10, i * 40)
TB.Name = "tbt" & i
Select Case i
Case 1 To 5
TB.Location = New Point(10, i * 45)
Case 6 To 10
Dim i2 As Integer = i - 5
TB.Location = New Point(80, i2 * 45)
End Select
tp.Controls.Add(TB)
Next
End Sub
note: I tried this method without tabcontrol it worked perfectly.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
_Textboxes.ForEach(New Action(Of TextBox)(AddressOf Controls.Remove))
_Textboxes.Clear()
num = ComboBox1.Text
...
End Select
Controls.Add(TB)
_Textboxes.Add(TB)
Next
I'm trying to make a userinterface that generates itself on request (button click)
Private Sub Body_new_part_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Body_new_part_add.Click
So when i add a Combobox as first it's no problem it generates the box & places it on the right position etc.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Now i want to add another control, a textbox next to the Combobox.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Dim oTextbox As New TextBox
oTextbox.name = "test"
oTextbox.Location = New System.Drawing.Point(50, 78)
Body_parts.Controls.Add(oTextbox)
This gives me this error.
'New' cannot be used on an interface.
What do i need to change in order to get this done? I need to add +- 10 controls on each button click event.
Try this one
Public Class Form1
Dim cLeft As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddNewTextBox()
End Sub
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
txt.Top = cLeft * 25
txt.Left = 100
txt.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
Return txt
End Function
End Class
I want to add a label to the form with a click of the button.
When I use that code here, it only adds 1 label but I want to add unlimited amount every time I click the button; it even adds 1 label even if I change the name.
Thank you every one.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(12, 180) 'set your location
lbl.Text = (TextBox1.Text) 'set your name
Me.Controls.Add(lbl) 'add your new control to your forms control collection
End Sub
Just a rehash of the previous 2 perfectly good answers. Here it tries to make clear that at least the location and text must be set based on logic provided by the user
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' YOU decide where EACH new label goes and pass the X,Y for each
' new label; YOU decide on the text and pass it. you can make them
' variables, but YOU have to do some of the thinking....
' <...> == information YOU provide
Private x As integer = <where you want the new label>
Private y as integer = <where you want the new label>
Private txt as String = <Text for this new label>
' EXAMPLEs
' a) set text from a textbox
' txt = txtLblText.Text
' b) Set X position from another code integer variable
' x = thisX
' c) Set Y position from textbox input
' y = Integer.Parse(txtLblYPos.Text)
Dim lbl as Label = MakeNewLabel(x, y, txt As string)
Me.Controls.Add(lbl)
End Sub
Friend function MakeNewLabel(x as integer, y as Integer, txt As String) as label
Dim lbl As New label
' add other label props here as needed
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(x, y) 'set your location
lbl.Text = txt
Return lbl
End Function
As correctly pointed out by #jlvaquero, you are overlapping your labels. The reason for this is that you're not changing the Point where these labels are being added to the Form.
One solution is to have field variables that can adjust the Point.
Private x As Integer = 12
Private y As Integer = 180
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(x, y) 'set your location
lbl.Text = (TextBox1.Text) 'set your name
Me.Controls.Add(lbl) 'add your new control to your forms control collection
x += 10 'arbitrary value, you could adjust y, too
End Sub
hello and thank you everyone for your great help i would no got that far without you her si the code that i used
Public Class Form1
Dim counter As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New Label
lbl.Name = "Label" & counter
lbl.Size = New Size(150, 20)
lbl.Location = New Point(200, counter * 22)
lbl.Text = TextBox1.Text 'text on label
Me.Controls.Add(lbl)
counter += 1
End Sub
End Class
I have a tab in a form. On form load, I am getting text from a text file line by line and displaying them as labels on a form Tabcontrol Tabpage along with dynamically displaying buttons beside them. Now on those buttons click I want to copy the text in the associated labels. Can anyone suggest what to put in the Nextbtn_Click event?
Dim FILE_NAME As String = "D:\1.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
i += 1
Next
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
End Sub
Store the assc. label in the tag property and you can cast it back when you click on the button. The sender object is the button that is currently clicked.
Dim FILE_NAME As String = "D:\1.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
Nextbtn.Tag = NextLabel
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
i += 1
Next
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
End Sub
Private Sub Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
TextBox2.Text = b.Name
Clipboard.SetText(b.Name)
End Sub