SilverShotBee SilverShotBee - 1 year ago 135 Question

Adding a User Control dynamically to a FlowLayoutPanel

I've created a UserControl and added it to a

. The UserControl is being used to allow a user to enter a material, cost and delivery status to the form. When the user has filled it in, I want another UserControl to appear underneath it in the

The UserControl simply generates a string based on the text entered into two
controls and the status of two
controls. it also has a property that indicates when the user has filled in enough information. I want to use this property to signal generating a new UserControl.

At the moment I have my first UserControl on the
, it is successfully passing the String and
property back.

The problems I am encountering are:

  1. How do I monitor to see if
    has changed to True?

  2. How do I add a control to the form and +1 to the controls name for future referencing

  3. Once the new control is added, I need to monitor to see if the new
    state changes to repeat the cycle

Can anyone point me in the right direction here, there's a lot of information out there on this, but I can't seem to find anything useful from other's problems/questions.


Thanks to user Zaggler for the comment, I have now managed to get the control to create a new instance of itself on the
. But now I'm faced with a new problem of it only creating one new usercontrol, then it stops.

Here's the code I'm using:

UserControl Code

Public Class Alv_Product_Order_Control
Public OutString As String
Public Event CreateNew()
Dim CreateNewRaised As Boolean

Private Sub OutputString(sender As Object, e As EventArgs) Handles tbMaterial.TextChanged, tbCost.TextChanged,
cbDelivered.CheckedChanged, cbOrderPlaced.CheckedChanged

OutString = "¦¦" & tbMaterial.Text & "¦" & tbCost.Text & "¦"

If cbOrderPlaced.Checked = True Then
OutString = OutString & "Yes¦"
OutString = OutString & "No¦"
End If

If cbDelivered.Checked = True Then
OutString = OutString & "Yes¦"
OutString = OutString & "No¦"
End If

If tbCost.Text = "" Or tbMaterial.Text = "" Then
If CreateNewRaised = False Then
RaiseEvent CreateNew() 'Raise the event that's used to signal adding a new control to the layout
CreateNewRaised = True 'Create A Latched Boolean that cannot change again in the future
End If
End If
End Sub

Public ReadOnly Property Alv_Product_Order_Control As String
Return OutString 'Pass string back to main form
End Get
End Property

Main Form Code

Private Sub CreateSecondPOC() Handles POC1.CreateNew
FlowLayoutPanel1.Controls.Add(New Alveare_VB.Alv_Product_Order_Control)
End Sub

I'm guessing here that the problem is the
only handles the event for the the first

How do I create a new
, name it as POC2 and also add a handler to handle
and add another control?

Answer Source

You could change your handler to something like this

Private Sub CreateSecondPOC() Handles POC1.CreateNew
    Dim newPoc As New Alveare_VB.Alv_Product_Order_Control
    AddHandler newPoc.CreateNew, AddressOf CreateSecondPOC
End Sub

I'm not sure if you want to keep handling the event, even after it has been populated once, i.e. can it be depopulated, then repopulated, then raise the event again? Maybe you want to lock it once it's populated, but this isn't clear.

You could also keep all your POC controls in a container and only create a new one when they are all populated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download