Share via

PerformClick() on a Dynamically Created UserControls

raklali 236 Reputation points
2021-12-27T14:43:11.75+00:00

I have a myUserControl for a purpose which has Two Buttons and Two TextBox.
I create and Add objects of myUserControl as myUserControl1, myUserControl2,myUserControl3, and so on, Dynamically by clicking a Form1.BtnCreateUserControlInstance on Form1.
Thereafter, I want to trigger myUserControl1.Button1.PerformClick().

How Do I locate myUserControl2 or myUserControl3 dynamically and call its PerformClick()?

I tried reading about AddHandlers and all but am unable to comprehend. Kindly assist.

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Peter Fleischer (former MVP) 19,351 Reputation points
2021-12-27T17:25:10.1+00:00

Hi,
you can save all UserControls in List and then call PerformClick for selected UserControl. Try following demo:

Public Class Form30  
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
    Me.Height = 1200  
    Me.Width = 800  
    Me.Controls.AddRange(New Control() {ComboBox1, ComboBox2, BtnCreateUserControlInstance})  
    Me.cbBS1.DataSource = buttons  
    Me.cbBS2.DataSource = buttons  
    Me.ComboBox1.DataSource = Me.cbBS1  
    Me.ComboBox2.DataSource = Me.cbBS2  
  End Sub  
  
  Private WithEvents BtnCreateUserControlInstance As New Button With {.Text = "BtnCreateUserControlInstance", .Top = 5, .Width = 200}  
  Private WithEvents ComboBox1 As New ComboBox With {.Top = 5, .Left = 210, .DisplayMember = "Header", .Tag = "Button1"}  
  Private WithEvents ComboBox2 As New ComboBox With {.Top = 5, .Left = 350, .DisplayMember = "Header", .Tag = "Button2"}  
  Private index As Integer = 1  
  Private buttons As New List(Of myUserControl)  
  Private cbBS1 As New BindingSource  
  Private cbBS2 As New BindingSource  
  
  Private Sub BtnCreateUserControlInstance_Click(sender As Object, e As EventArgs) Handles BtnCreateUserControlInstance.Click  
    Dim uc As New myUserControl With {.Header = $"myUserControl{index}", .Top = index * 50, .Width = 400, .Height = 50}  
    Me.Controls.Add(uc)  
    Me.buttons.Add(uc)  
    Me.cbBS1.ResetBindings(False)  
    Me.cbBS2.ResetBindings(False)  
    index = index + 1  
  End Sub  
  
  Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged  
    Dim cb = TryCast(sender, ComboBox)  
    If cb Is Nothing Then Exit Sub  
    Dim uc = TryCast(cb.SelectedItem, myUserControl)  
    If uc Is Nothing Then Exit Sub  
    If cb.Tag.ToString = "Button1" Then  
      uc.Button1.PerformClick()  
    Else  
      uc.Button2.PerformClick()  
    End If  
  End Sub  
  
  Public Class myUserControl  
    Inherits UserControl  
    Public Sub New()  
      AddHandler Me.Load, AddressOf Uc_Load  
    End Sub  
  
    Private Label1 As New Label With {.Top = 0, .Left = 5, .Width = 100}  
    Friend WithEvents Button1 As New Button With {.Text = "Button1", .Top = 0, .Left = 120, .Width = 100}  
    Friend WithEvents Button2 As New Button With {.Text = "Button2", .Top = 0, .Left = 240, .Width = 100}  
    Private TextBox1 As New TextBox With {.Top = 25, .Left = 5, .Width = 150}  
    Private TextBox2 As New TextBox With {.Top = 25, .Left = 160, .Width = 150}  
    Public Property Header As String  
    Private Sub Uc_Load(sender As Object, e As EventArgs)  
      Me.Controls.AddRange(New Control() {Label1, Button1, Button2, TextBox1, TextBox2})  
      Label1.DataBindings.Add("Text", Me, "Header")  
    End Sub  
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
      TextBox1.Text = Now.ToLongTimeString  
    End Sub  
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
      TextBox2.Text = Now.ToLongTimeString  
    End Sub  
  End Class  
End Class  

Result:

160725-x.gif

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. raklali 236 Reputation points
    2021-12-30T16:40:20.437+00:00

    One more query, With use of DataBindings, Click() action is working fine but How do we Trigger MouseDown or MouseUp Event ?

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.