How to transfer the text from dynamically generated user control to a textbox

后端 未结 3 804
天涯浪人
天涯浪人 2020-12-02 02:52

I have a windows form in which I have a button1 and when that is clicked a UserControl that is added dynamically to the code is this:



        
3条回答
  •  被撕碎了的回忆
    2020-12-02 03:08

    I would do this with events.

    Create a class that inherits from EventArgs: (I prefer VB, you can traslate)

    Public Class ControlEventArgs
      Inherits EventArgs
    
      Public Property Value1 As String = String.Empty
      Public Property Value2 As String = String.Empty
      Public Property Value3 As String = String.Empty
      Public Property Value4 As String = String.Empty
    
    End Class
    

    Then in your Control add the event:

    Public Event ValueSubmittal As EventHandler(Of ControlEventArgs)
    

    In your Button2_Click handler:

    RaiseEvent ValueSubmittal(me, new ControlEventArgs With {.Value1=comboBox1.Text, .Value2 = comboBox2.Text, .Value3 = textBox1.Text, .Value4 = textBox2.Text}
    

    And in your form where you dynamically create the controls you need to hook up the event handler:

    AddHandler myNewControl.ValueSubmittal, AddressOf ValueSubmittalHandler
    

    And the ValueSubmittalHandler:

    Private Sub ValueSubmittalHandler(sender as Object, e As ControlEventArgs)
      formControl1.Text = e.Value1
      formControl2.Text = e.Value2
      '  etc...
    End Sub
    

提交回复
热议问题