Results 1 to 8 of 8

Thread: [Help] VB6 to VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    12

    [Help] VB6 to VB.NET

    Anyone here knows how to add controls at runtime then handle its event? -> its easy to do this with vb6 but im not sure how to do it in vb.net since it does not support control arrays.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [Help] VB6 to VB.NET

    Here's an example that adds a button to the form.
    Code:
      Private Sub AddButton()
        Dim btn As New Button()
        btn.Text = "New Button"
        btn.Location = New Point(10, 10) 'equal to .Top and .Left in VB6
        'Add the event handler
        AddHandler btn.Click, AddressOf MyButtonClick
        'Add the button to the form
        Me.Controls.Add(btn)
      End Sub
    
      Private Sub MyButtonClick(sender As Object, e As EventArgs)
        MessageBox.Show("Click")
      End Sub

  3. #3

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    12

    Re: [Help] VB6 to VB.NET

    thanks, i got it answered before reading your comments. thanks anyway

    Im still starting out with .NET and im more use to vb6 since its way easy for me to things like this.

    Anyway, my solution for my problem is "something" like this.

    'Global
    Dim cWebDownloader() As cDownloader -> custom control based of WebClient

    'Somewhere in button click event
    For i as integer = 1 to 5
    ReDim cWebDownloader(i)
    cWebDownloader(i) = New cDownloader
    AddHandler cWebDownloader(i).DownloadComplete, AddressOf cWebDownloader_DownloadComplete
    next

    'Then this is the sub where the event will get consumed
    Private Sub cWebDownloader_DownloadComplete(Sender As System.Object, Filename As String)
    'i used sender to determine the object that calls it
    End Sub

    Thanks,

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: [Help] VB6 to VB.NET

    Take a look at the List(of T). Generics were added with 2005 and have some HUGE advantages. Once you see what that can do you'll use arrays a lot less. Of course, a List makes use of an array behind the scenes, too, but it gives you Add, Insert, AddRange, Remove, RemoveAt, and many other methods that get rid of the need for Redim and Redim Preserve (and isn't that what you wanted anyways, since Redim will erase the array each time). The List resizes the underlying array as needed, but it does it much more efficiently than you will do with Redim. There are also a bunch of other generics beyond the List.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    12

    Re: [Help] VB6 to VB.NET

    Quote Originally Posted by Shaggy Hiker View Post
    Take a look at the List(of T). Generics were added with 2005 and have some HUGE advantages. Once you see what that can do you'll use arrays a lot less. Of course, a List makes use of an array behind the scenes, too, but it gives you Add, Insert, AddRange, Remove, RemoveAt, and many other methods that get rid of the need for Redim and Redim Preserve (and isn't that what you wanted anyways, since Redim will erase the array each time). The List resizes the underlying array as needed, but it does it much more efficiently than you will do with Redim. There are also a bunch of other generics beyond the List.
    Yes, you are right, but actually, releasing object after i use it is also what im after. so after DownloadComplete, object will get release, then will rely on array length to be used in for loop to create another set of object.

    Will see list of T and see how i can use it on my code. Thanks,

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: [Help] VB6 to VB.NET

    Why create and destroy? Why not reuse?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    12

    Re: [Help] VB6 to VB.NET

    Quote Originally Posted by Shaggy Hiker View Post
    Why create and destroy? Why not reuse?
    Not sure.. haha.. its the first thing that came to my mind when i coded it.

Posting Permissions

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



Click Here to Expand Forum to Full Width