Solved

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

Posted on 2009-05-06
4
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

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 86

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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