dcsimg
Results 1 to 4 of 4

Thread: How to use a dynamically generated textbox (with dynamic button)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    5

    How to use a dynamically generated textbox (with dynamic button)

    I had to create some dynamic elements in an empty form and I've done so by doing the following:

    Code:
    Public Sub FormZadatak4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
            Dim labela As New Label With {
                .Size = New Size(400, 22),
                .BackColor = Color.Bisque,
                .Location = New Point(0, 0),
                .Text = "All of the elements in this form a dynamically created, as is this label.",
                .Visible = True
            }
            Controls.Add(labela)
    
            Dim tekst As New TextBox With {
                .Name = "txtTekst",
                .Size = New Size(400, 22),
                .Location = New Point(0, 22),
                .Visible = True,
            }
    
            Controls.Add(tekst)
    
            Dim btnAdd As New Button With {
                .Name = "btnAdd",
                .Size = New Size(195, 22),
                .Location = New Point(0, 45),
                .Text = "Add",
                .Visible = True
            }
            Controls.Add(btnAdd)
    
    
    
            Dim btnDelete As New Button With {
                .Name = "btnDelete",
                .Size = New Size(195, 22),
                .Location = New Point(200, 45),
                .Text = "Delete",
                .Visible = True
            }
            Controls.Add(btnDelete)
    
    
        End Sub
    Now I'm supposed to create an event which would write out 2^n & " " (starting from 0 and incrementing with each click) in the textbox each time the Add button is clicked.

    I know I'm supposed to use an event Handler somewhere and somehow, but I'm having difficulties figuring out where and how.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,938

    Re: How to use a dynamically generated textbox (with dynamic button)

    Write a sub that contains the code you want, and use AddHandler to set it as the handler for the appropriate event, eg:
    Code:
        AddHandler btnAdd.Click, AddressOf YourButtonClickEventSub
    https://docs.microsoft.com/en-us/dot...dler-statement

    You need to make sure that your sub has the same parameters as as the usual handler for the event. That will sometimes be (sender As Object, e As EventArgs) , but you need to check for each event... a good way to check is to create the right kind of control at design time, and create the event you want, as it will have the correct parameters.

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,067

    Re: How to use a dynamically generated textbox (with dynamic button)

    Use the WithEvents keyword, for example:
    Code:
    Dim WithEvents tekst As New TextBox
    An alternative is the AddHandler statement but it's more complicated and you don't need it in your present code.

    BB

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    5

    Re: How to use a dynamically generated textbox (with dynamic button)

    Nevermind, thank you for the input, it helped!
    I've figured out my problem in the meantime!
    Last edited by tbontbg; Jun 16th, 2019 at 07:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width