0

I have a usercontrol named as myUserControl.

1. myUserControl has a button.
2. The Constructor of myUserControl initializes a variable.
    public partial class myUserControl : Usercontrol
    {
        public string test;


        public myUserControl ()
        {
            InitializeComponent();

            test = "This is test";

        }
}

my Form1 has a flowlayout pannel.On my Form1.Load I add my myUserControl to a FLowLayoutPanel

 private void Form1_Load(object sender, EventArgs e)
        {
            myUserControl muc = new myUserControl();
            myFlowLayoutPannel.Controls.Add(muc);

        }

now in Form1, i have a FlowlayoutPannel showing my myUserControl without an issue, what i want is when i click on the button of myUserControl, a Messagebox to be shown with the value of Test variable which defined in myUserControl.

I hope my idea is clear, of course, this is just an example to explain my question and your answer will help me to do something more.

San Nabaty
  • 13
  • 5
  • You need to expose the `text` variable outside the control, via a property. Then in the form use this property to read the value. – John Alexiou Aug 11 '19 at 19:58
  • Frankly, it's not clear what you actually need help with. You've already made the field `public`; bad design, but at least it allows the parent form to get the value at any time it wants. Then you want to get that value when a button is clicked. Since you're going around making implementation details `public` anyway, you can just make the button's field in the user control public, and the form can subscribe to its `Click` event. But, if you want to do it the right way, see the marked duplicate. Your user control will take the place of the second form in that Q&A. – Peter Duniho Aug 11 '19 at 19:59
  • If you want to notify the parent From that a Button of an UC has been clicked, create a public event (in the UC) and raise raise it when the Button is clicked. You can then pass the value of the variable in a custom `EventArgs` class or let the Form access the `(sender as myUserControl).Test` public field. The former is a better method, IMO. You could also use a public property instead of a field. – Jimi Aug 11 '19 at 19:59

1 Answers1

0
  1. You could add the button to the user control form using the Visual Studio Designer.
  2. Add an event handler for button click or simply double click on the button in the designer: a Button1_Click event handler will be generated.
  3. Define a event using EventHanlder, that the user control will raise after the button is clicked.

    public partial class myUserControl : UserControl
    {
        public string test;
    
        public event EventHandler<UserEventArgs> SomebuttonClicked;
    
        public myUserControl()
        {
            InitializeComponent();
            test = "This is test";
        }
    
        private void UserControl1_Load(object sender, EventArgs e)
        {
    
        }
    
        private void Button1_Click(object sender, EventArgs e)
        {
            SomebuttonClicked(sender, new UserEventArgs
                {
                    SomeVariable = test
                }
            );
        }
    }
    
    public class UserEventArgs : EventArgs
    {
        public string SomeVariable { get; set; }
    }
    
  4. In your main form class, register to the event of the user control.

    myUserControl1.SomebuttonClicked += MyUserControl1_SomebuttonClicked;
    
  5. Finally, add the event handler function to the form:

    private void MyUserControl1_SomebuttonClicked(object sender, UserEventArgs e)
    {
        MessageBox.Show(e.SomeVariable);
    }  
    
EylM
  • 5,967
  • 2
  • 16
  • 28