Solved

Clear Out, Reload Usercontrols in a Tab in VB.NET

Posted on 2009-05-06
4
307 Views
Last Modified: 2012-05-06
I'm looking for some guidence to the best way to clear my tab form of dynamically generated UserControls, and then to reload it with the same UserControls (with different data, pulled from the database.

I load the controls like this:
        For x = 0 To x
            Dim cont As New NamesControl.ProductImage
            AddHandler cont.SaveMyImage, AddressOf SaveMyImage
            AddHandler cont.ChangeOrder, AddressOf ChangeOrder
            contName = "id_" & x.ToString
            cont.Name = "id_" & x.ToString
            Me.TabPage2.Controls.Add(cont)
            cont.Top = 100 * y
            cont.Left = l
            cont.LargeNameText = ImageArray(3, x)
            cont.ThumbNameText = ImageArray(2, x)
            cont.ImageOrderValue = ImageArray(4, x)
            cont.idNum = ImageArray(0, x)
            cont.msgText = ""
            cont.ProdNameText = ProductName
            contName = "http://www.mysite.com/image/" & ImageArray(2, x)
            cont.ThumbImageFile = LoadWebImageToPictureBox(contName)
            If x >= 5 Then
                y = 0
                l = 450
            Else
                y += 1
                l = 12
            End If
        Next

The usercontrol has 2 buttons - one for changing the order (fires the "ChangeOrder" event), and the 2nd for changing the image (fires the "SaveMyImage") event.

Since the UserControl is dynamically created, then loaded from the database, when the SaveMyImage event, or the ChangeOrder events are called, I need to update the database (which i can do), pull values from the database (which i can do) and then update the UserControls, which I can't seem to do.

The main issue I think is that I don't know how to refer to the UserControl that was clicked...  When i tried "id_1" as the control name - VB.NET doesn't recognize it (i can understand why) but it returns an error and I can't compile/build/test.  if the UserControl is loaded into the form, it should be available outside of the sub that created it, right?

Hope this isn't too confusing... thanks for your help!
0
Comment
Question by:steverguy
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24316388
The simplest solution is to modify your UserControl so that the Events also pass out a "sender" parameter that is a reference to the source UserControl.

Simplified example:

    Public Class UserControl1

        Public Event ChangeOrder(ByVal sender As UserControl1)

        Public Sub foo()
            RaiseEvent ChangeOrder(Me) ' <--- pass out THIS usercontrol with "ME"
        End Sub

    End Class

Now, in the method that handles the ChangeOrder event, you can use the "sender" parameter to reference the UserControl that fired the event...  =)
0
 
LVL 1

Author Comment

by:steverguy
ID: 24316802
Hi Idle,

Thanks for the quick response.  The following is the first few lines from my UserControl code:
    Public Event SaveMyImage(ByVal txt As String, ByVal id As Integer)
    Public Event ChangeOrder(ByVal x As Integer, ByVal id As Integer, ByVal OrigOrder As Integer)
    Public OrigOrder As Integer

    Private Sub btnChangeOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeOrder.Click
        If IsNumeric(ImageOrder.Text) = True Then
            msg.Text = ""
            RaiseEvent ChangeOrder(ImageOrder.Text, id.Text, OrigOrder)
        Else
            msg.Text = "Only numbers please..."
            ImageOrder.Focus()
            Exit Sub
        End If
    End Sub

In this case, how do I pass the "ME" parameter as part of the
   RaiseEvent ChangeOrder(ImageOrder.Text, id.Text, OrigOrder)?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24316838
You would need to change your Events to:

    Public Event SaveMyImage(ByVal sender As NamesControl.ProductImage, ByVal txt As String, ByVal id As Integer)
    Public Event ChangeOrder(ByVal sender As NamesControl.ProductImage, ByVal x As Integer, ByVal id As Integer, ByVal OrigOrder As Integer)

Then change the lines that raise that events to include "Me" as the first parameter:

    RaiseEvent ChangeOrder(Me, ImageOrder.Text, id.Text, OrigOrder)

Finally, you need to change the methods that RECEIVE your new events so that they also have "ByVal sender As NamesControl.ProductImage" as the first parameter.  Then you can use "sender" in those methods to reference the UserControl that fired the event.
0
 
LVL 1

Author Closing Comment

by:steverguy
ID: 31578500
Perfect, thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question