Search code examples
c#winformsuser-controls

How to call a UserControl's methods from a Windows.Form


The form has a button and a panel with a UserControl which has a ListBox and a TextBox.

When I click the Windows.Form button it calls UserControl's Add()

listBoxTitles.Items.Add(metroTextBoxTitles.Text);
metroTextBoxTitles.Clear();

Which simply adds whatever the UserControl's TextBox.Text has to the UserControl's ListBox.

For some reason nothing happens when i click the button.
Why. Nothing on the UserControl can be changed or used? Or does it change but doesn't update/show what's going on?


Solution

  • So, what was happening was, when you create a UserControl and add it to the Window.Form the Form's designer already initiates that UserControl:

    private UserControl userControl1; 
    

    So in order to solve the issue i just needed to use the UserControl which the designer code created:

    usercontrol1.add();
    

    And everything is working perfectly.