C # User Control - How to determine the object containing the control data object

I am creating a C # WinForms user control. There will be times when a user control needs data from the form in which it is contained. How do I get a user to control the form containing him that he needs data?

Thanks!

+4
source share
5 answers

You can subscribe to the event raised in UserControl.

Your archiving dictates where and when you need to subscribe to a data event. We cannot answer this without knowing a bit more about how you add control at runtime or development time. For each case, a small conclusion is required. In terms of adding your control at runtime, you can do something similar to the following:

// Your sample control public class MyUserControl : Control { public event EventHandler<EventArgs> INeedData; public Data Data {get; set;} private class DoSomething() { if(INeedData!=null) INeedData(this,null); } } ... // Your Form, in the case that the control isn't already added. var _myUserControl = new MyUserControl(); private void Form1_Load(object sender, EventArgs e) { _myUserControl.INeedData += new EventHandler<EventArgs>(MyUserControl_INeedData); this.Controls.Add(myUserControl); } void MyUserControl_INeedData(object sender, EventArgs e) { _myUserControl.Data = SomeData; } 
+4
source

Create an event in the user control where the event arguments are edited. Let the form attach a handler to this event, which updates these fields. Use these fields in the OnEvent method.

[unverified] for example.

 public delegate void NeedsUserDataEventHandler(object sender, NeedsUserDataEventArgs args); public class NeedsUserDataEventArgs : EventArgs { public UserData UserData { get; set; } } // In Control public event NeedsUserDataEventHandler NeedsUserData; public void OnNeedsUserData(NeedsUserDataEventArgs args) { NeedsUserDataEventHandler handler = NeedsUserData; if (handler != null) handler(this, args); // store your data somewhere here } // In Form public override void OnLoad(object sender, EventArgs args) { control.NeedsUserData += ControlNeedsUserData; } public override void OnClosed(object sender, EventArgs args) { control.NeedsUserData -= ControlNeedsUserData; } public void ControlNeedsUserData (object sender, NeedsUserDataEventArgs args) { args.UserData = // set whatever here } 
+1
source

Create a custom event in the user control and paste the form into it. If you need special event arguments, you can also create them.

In user management:

 //Define your Custom Event argument public class MyEventArgs : EventArgs { //Define some fields of your custom event argument private int m_SomeValue = 0; //Define some properties of your custom event argument public int SomeValue { get { return m_SomeValue; } set { m_SomeValue = value; } } } //Define the event handler data type public delegate void MyEventHandler(object sender, MyEventArgs e); //Define the object which holds the outside event handlers public event MyEventHandler SomeEvent; //Define the function which will generate the event public virtual void OnSomeEvent(MyEventArgs e) { if (SomeEvent != null) SomeEvent(this, e); } . . //Then later in the control . { //We need new data //Create the event arguments MyEventArgs newEvent = new MyEventArgs(); //Set the values newEvent.SomeValue = 17; //Call the event generating function OnSomeEvent(newEvent); } 

In your form, just use something like:

 myControl.SomeEvent += new MyEventHandler(handlerName); 

Since your event is publicly available, you should also see it in the Properties window of your control.

You can come up with an event using metadata attributes, but I leave it to you to discover them.

+1
source

Seems a bit vague for me, but:

Make this an event in the containing WinForm, so that every time some data is ready, all subscribers can be notified in the one-to-many model; or make it an event in a signed control in a one-to-one model in which it calls a container method that retrieves such data?

0
source

It depends on when this data needs to be transferred to UserControl. Do events in the Form occur that will force data to be moved to UserControl? If so ... just grab your instance at this point and pull the data into UserControl via a public property.

If this is the case when the events are not used, or the form in one form or another "receives data", and then reveals the event on the form, such as ...

 public event DataHandler ReceivedData; 

... and allow UserControl or any other container to register for the event and receive data through your custom event arguments. Clicking an event in the UserControl and forcing the form to commit on the UserControl seems to be the opposite, since the form is the data initiator.

0
source

Source: https://habr.com/ru/post/1332360/


All Articles