Solved

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

Posted on 2009-05-06
4
305 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now