VB.net adding multiple controls on button click - vb.net

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

Related

VB.NET get control name from button created at run time

I have this code to create 3 buttons at run time, which seems to be working ok.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rawData As String
Dim FILE_NAME As String = "C:\temp\test.txt"
Dim data() As String
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
rawData = objReader.ReadLine() ' & vbNewLine
data = Split(rawData, ",")
'data 0 = X loc, data 1 = Y loc, data 2 = Part Num, data 3 = Reference Des
Dim dynamicButton As New Button
dynamicButton.Location = New Point(data(0), data(1))
dynamicButton.Height = 20
dynamicButton.Width = 20
dynamicButton.FlatStyle = FlatStyle.Flat
dynamicButton.BackColor = Color.Transparent
dynamicButton.ForeColor = Color.FromArgb(10, Color.Transparent)
'dynamicButton.Text = "+"
dynamicButton.Name = data(2)
dynamicButton.FlatAppearance.BorderColor = Color.White
'dynamicButton.Font = New Font("Georgia", 6)
AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
Controls.Add(dynamicButton)
Dim myToolTipText = data(3)
ToolTip1.SetToolTip(dynamicButton, myToolTipText)
Loop
End Sub
I want to get the control name that was created when I click a button, but I cannot seem to get what I need. I have been doing this on the dynamicButton_click event.
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For Each cntrl In Me.Controls
If TypeOf cntrl Is Button Then
MsgBox("")
Exit Sub
End If
Next
End Sub
All you need to do is cast the sender variable to button. It will tell you which control was the source of the event:
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show("You clicked: " & btn.Name)
End Sub
You attached an event handler to the dynamic button and that is hitting?
Couple ways you can do this:
This will give you the button you just clicked without needing to loop, the sender is the button. You need to cast it from the object to button:
MessageBox.Show(DirectCast(sender, Button).Name)
Otherwise, inside your loop, cast cntrl (the generic Control object) to the specific Button control object and then get the name.
Dim btn as Button = DirectCast(cntrl, Button)
MessageBox.Show(btn.Name)

Creating a simple addition form application programmatically

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.

Get value of multiple created controls at runtime VB.NET

I can get the value of a single control that has been created at run time, that control is a DateTimePicker, but i can't get the values of multiple controls. how do i do it?
code:
This is where i add the DateTimePicker with every click at run time.
Private Sub btnAddTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTime.Click
Dim Time As New DateTimePicker()
Dim count As Integer = GroupBox1.Controls.OfType(Of DateTimePicker)().ToList().Count
Time.Location = New Point(9, (23 * count) + 22)
Time.Size = New Size(150, 20)
Time.Format = DateTimePickerFormat.Time
Time.ShowUpDown = True
Time.Name = "DateTimePicker" & (count + 1)
GroupBox1.Controls.Add(Time)
End Sub
And this is where i get the values of the controls.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer = 0
For Each cntrl In Form3.GroupBox1.Controls
Dim dt As DateTimePicker = Form3.GroupBox1.Controls.Item("DateTimePicker" & (i + 1))
If DateTime.Now.ToString = dt.Value Then
MsgBox("P")
End If
Next
End Sub
I can only get the the value of a created control if only i specified it in the code, i think i can't get the right Name of the control. I'm trying to make the program to read every value of the control in another form while the Timer ticks. help?
Try this:
For Each cntrl In Form3.GroupBox1.Controls
If TypeOf cntrl Is DateTimePicker Then
If DateTime.Now.ToString = cntrl.Value Then
MsgBox("P")
End If
End If
Next

access to new dynamically controls in vb.net

First of all excuse me for my poor grammar and vocabulary :)
please see this source and run it:
Public Class Form1
Public pointX As Integer
Public pointY As Integer = 32
Public dynamicText As TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pointX = 330
For i = 1 To 4
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "T" + Trim(Str(i))
dynamicText.Text = ""
dynamicText.Location = New Point(pointX, pointY)
dynamicText.Size = New Size(100, 20)
Me.Controls.Add(dynamicText)
pointX = pointX - 106
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
pointX = 330
pointY = pointY + 26
For i = 1 To 4
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "T" + Trim(Str(i))
dynamicText.Text = ""
dynamicText.Location = New Point(pointX, pointY)
dynamicText.Size = New Size(100, 20)
Me.Controls.Add(dynamicText)
pointX = pointX - 106
AddHandler dynamicText.Click, AddressOf printHello1
Next
End Sub
Private Sub printHello1(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(dynamicText.Name)
If dynamicText.Name = "T1" Then MsgBox("Oh! this is T1")
End Sub
End Class
why If never is not true?!
why MsgBox(dynamicText.Name) always return T4?!
i want all controlls to be access by name or array of names.
please help me thank you. :)
The global variable dynamicText takes the value of the last TextBox added in the loop inside the Button1_Click event. This happens to be the control named T4. You don't really need a global variable in this case. You can cast the sender parameter to a TextBox instance because the sender parameter is the control that has raised the event.
Private Sub printHello1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim txt = CType(sender, "TextBox")
if txt IsNot Nothing then
MsgBox(txt.Name)
If txt.Name = "T1" Then MsgBox("Oh! this is T1")
End If
End Sub
You also don't need to recreate the controls again in the button click event. The action executed in the form load event is enough (You could add the AddHandler there). Global variables are dangerous, avoid them when possible.
See if this is acceptable. Place a panel at the bottom of your form, set Dock to Bottom, add a single button to the panel and a TextBox. Place a FlowLayoutPanel onto the form, Dock = Fill, AutoScroll = True.
The code below creates the amount of TextBox controls as inputted into TextBox. Each newly created TextBox a click event is added with simple logic.
Form code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer = 0
If Integer.TryParse(TextBox1.Text, count) Then
Dim demo = New TextBoxCreate(FlowLayoutPanel1, "Demos", count)
demo.CreateTextBoxes()
End If
End Sub
End Class
Class code (add a new class to the project, name it TextBoxCreate.vb)
Public Class TextBoxCreate
Public Property TextBoxes As TextBox()
Public Property TextBoxBaseName As String
Public Property TextBoxCount As Integer
Public Property ParentControl As Control
Public Sub New(
ByVal ParentControl As Control,
ByVal BaseName As String,
ByVal Count As Integer)
Me.ParentControl = ParentControl
Me.TextBoxBaseName = BaseName
Me.TextBoxCount = Count
End Sub
Public Sub CreateTextBoxes()
Dim Base As Integer = 10
TextBoxes = Enumerable.Range(0, TextBoxCount).Select(
Function(Indexer)
Dim b As New TextBox With
{
.Name = String.Concat(TextBoxBaseName, Indexer + 1),
.Text = (Indexer + 1).ToString,
.Width = 150,
.Location = New Point(25, Base),
.Parent = Me.ParentControl,
.Visible = True
}
AddHandler b.Click, Sub(sender As Object, e As EventArgs)
Dim tb As TextBox = CType(sender, TextBox)
If tb.Name = TextBoxBaseName & "1" Then
tb.Text = "Got it"
Else
MessageBox.Show(tb.Name)
End If
End Sub
Me.ParentControl.Controls.Add(b)
Base += 30
Return b
End Function).ToArray
End Sub
End Class

Dynamic control in VB.NET

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