Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

11 Experts available now in Live!

Get 1:1 Help Now