SilverShotBee SilverShotBee - 22 days ago 5
Vb.net Question

Adding a User Control dynamically to a FlowLayoutPanel

I've created a UserControl and added it to a

FlowLayoutPanel
. 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
FlowLayoutPanel


The UserControl simply generates a string based on the text entered into two
TextBox
controls and the status of two
Checkbox
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
FlowLayoutPanel
, it is successfully passing the String and
CreateNew
property back.

The problems I am encountering are:


  1. How do I monitor to see if
    CreateNew
    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
    CreateNew
    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.

UPDATE 1

Thanks to user Zaggler for the comment, I have now managed to get the control to create a new instance of itself on the
FlowLayoutPanel
. 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¦"
Else
OutString = OutString & "No¦"
End If

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

If tbCost.Text = "" Or tbMaterial.Text = "" Then
Else
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
Get
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
CreateSecondPOC
only handles the event for the the first
POC1


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

Answer

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
    FlowLayoutPanel1.Controls.Add(newPoc)
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.

Comments