Hi Friends,
I am working on Project(Window Application in VB.NET) and unfortunately i am getting out of time,Need ur help.
Theres one situation in which application to accept Five Email Address but the condtion is that during form load only one textbox need to display first and a button adjacent to it for adding more if needed upto Five.
So,need the control for adding Button at Runtime which can deleted if required.Please help me bcoz i am running out of time.

if u want to help you can mail the Code Snippet to jainjayesh1681@yahoo.com or post it in Daniweb.

Eg.
TextBox Button to Add if needed
TextBox Button to delete the Textbox if nedded

I have attached one Doc File which has screenshot of Contact Screen and u can refer to that for more

Recommended Answers

All 2 Replies

Try this.
It's very simple, and it won't keep track of which textbox you remove.

Private btnID As Integer = 1
Private txtTop As Integer = firsttextBox.Top

Private Sub addButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles addButton.Click
   Dim txtBox As New TextBox
   txtBox.Name = "txt" & buttonID
   txtBox.Width = firstTextBox.Width
   txtBox.Top = txtTop + txtBox.Height + 3
   txtBox.Left = firsttextBox.Left
   txtTop = txtBox.Top
   Me.Controls.Add(txtTop)

   Dim btnRemove As New Button
   btnRemove.Name = "btn" & buttonID
   btnRemove.Width = addButton.Width
   btnRemove.Text = "Remove"
   btnRemove.Top = txtBox.Top
   btnRemove.Left = addButton.Left
   AddHandler btnRemove.Click, AddressOf removeBtnTxtBox_Click
   Me.Controls.Add(txtTop)

   btnID += 1
End Sub

Private Sub removeBtnTxtBox_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim ID As Integer = DirectCast(sender, Button).Name.SubString(3, 1)
   
   Me.Controls.RemoveByKey("txt" & ID)
   Me.Controls.RemoveByKey("btn" & ID)
End Sub

The following code shows how to create controls at run time. Create a new form and define one button at the top left with the name btnAdd. Paste in the code and give it a try.

Imports System.Windows.Forms

Public Class Form1

    Private xoffset As Integer      'x coordinate of new button 
    Private yoffset As Integer      'y coordinate of new button 
    Private btnID As Integer        'button number              

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click

        'Create a new button and set size, location and text

        Dim button As New Button
        button.Size = New Size(75, 23)
        button.Location = New Point(xoffset, yoffset)
        button.Text = "Button " & btnID

        'add the button to the form controls (or you won't be able to see/click it)

        Me.Controls.Add(button)

        'tell it what code to run when the button is clicked

        AddHandler button.Click, AddressOf RTButton_Click

        'adjust the location and button number for the next button

        yoffset += 30
        btnID += 1

    End Sub

    Private Sub RTButton_Click(sender As System.Object, e As System.EventArgs)
        MsgBox(sender.Text)
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        xoffset = 75
        yoffset = 50
        btnID = 1

    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.