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

后端 未结 3 790
天涯浪人
天涯浪人 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
    
    0 讨论(0)
  • 2020-12-02 03:12

    You should add to your class UserControl1 (great name btw ;-) ) something like this for every string you want to access from another object, in this case the string of textBox1:

    public String FirstTextBoxText 
    {
       get { return this.textBox1.Text; }
    }
    

    Then you can say in your Form class:

     if (ctrl is UserControl)
     {
         UserControl1 myCrl = ctrl as UserControl1;
         // ...
         oldtextbox1.Text = still-textbox3.text + "," + myCrl.FirstTextBoxText;
     }
    

    It's still horrible code, but it will work.

    0 讨论(0)
  • 2020-12-02 03:13

    You can create a class level variable:

        private UserControl1 us1;
        private UserControl1 us2;
    
        private void button1_Click(object sender, EventArgs e)
        {
            int v;
            v = c++;
            panel1.VerticalScroll.Value = VerticalScroll.Minimum;
    
            if(us == null) 
            {
                //this is the first time the control is created
                us1 = new UserControl1();
                us1.Name = "us" + v;
                us1.Location = new Point(50, 5 + (30 * v));
                us1.Tag = btn;        
                panel1.Controls.Add(us1);
            }
            else if(us2 ==null)
            {
                us2 = new UserControl1();
                //whatever code you want to execute to change second one
                //you can access first control as us1.xxx
                panel1.Controls.Add(us2);
    
            }
            else
            {
               //3rd 4th etc...
            }
    
    
         }
    
    0 讨论(0)
提交回复
热议问题