Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-06
4
Medium Priority
?
314 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

722 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