The C # program loads the dynamic library written in the same C #. Accordingly, the dll performs only one function. A simple dialog box appears, the same form, on which there is one button. How to make it so that when you click on the button, the program received information that an event occurred or what other action. Time press may not be known.
- dll you written? In the sense that you have access to its code and can you change it or not? - Alexey Losev Nov.
- We will write everything ourselves. The algorithm of the interaction itself is not clear. - serand
- As I understand it, you need to implement a plug-in system. Search by MEF / MAF terms - there is a lot of documentation. Also read Richter - in one of the chapters he described a simple implementation. - Alexander Petrov
2 answers
This is called an event. I make two forms, you can spread them across different assemblies, you can in one application, the logic will not change, you will only need to add a link from the main application to the dll in the case of two assemblies and in the class code of the main form you must write using using the namespace from assembly.
So, in steps:
- We create the second form (I have it Form2) on which I place TextBox and Button. Appearance form:
Double click on the button to go to the code and write the following (note the announcement of the event):
public partial class Form2 : Form { public Form2() { InitializeComponent(); } public event Action<String> OnSendText; private void button1_Click(object sender, EventArgs e) { OnSendText?.Invoke(textBox1.Text); textBox1.Text = ""; } } - I place the ListBox and the button on the main form of the application, the look will be like this:
Double click on the button and write the following code:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(); form.OnSendText += Form_OnSendText; form.Show(); } private void Form_OnSendText(string obj) { listBox1.Items.Add(obj); } } - We start, press the button of the first form, enter the text in the second form, press the button on the second form and see that the text appeared on the first form, although the second one knows absolutely nothing about it:
- Thanks for the answer. Subscribe to the event, I also thought about it. But "you only in the case of two assemblies will need to add a link from the main application to the dll and in the class code of the main form register using using the namespace from the assembly." That's what I have and not Docking. I can’t get around a little until I can. The point is that the dll will load over the network. There are no problems. How do I connect it with the first application? "How to prescribe via using? Or I don’t understand something yet. How is the main application sign to the event of another? In this case, to this dll - serand
- You can use the framework for plugins, as you were advised above, or look here: losev-al.blogspot.com/2009/06/blog-post_23.html - Alexey Losev
- Thanks, I will consider the material on the link. - serand
- Very interesting. If I have any questions, I can ask you some questions. Now I’ll look more and more closely. You can ask questions where? - serand
- If by the code that is in response, then here. If the code that the link can be a separate topic here, and you may answer not only me. Well, or in the comments on the link that I gave you. - Alexey Losev Nov.
In Dll I create class MyForm
public class MyForm { public event Action<String> OnSend; public void Launch() { ClassLibrary1.FormTest frm = new ClassLibrary1.FormTest(); frm.OnSendText += Form_OnSendText; frm.Show(); } private void Form_OnSendText(string obj) { OnSend?.Invoke(obj); } } The FormTest form has a button and a text field. When loading the dll, we call the Launch method where we do the described actions on the code. The main application has a form, a button and a box.
namespace WindowsFormsApplication1 { public partial class Form1 : Form { ListBox list; Button button; public Form1() { InitializeComponent(); list = new ListBox(); list.Location = new Point(5, 10); list.Size = new Size(275, 225); list.Name = "list"; this.Controls.Add(list); button = new Button(); button.Location = new Point(207, 237); button.Name = "button"; button.Size = new Size(75, 23); button.Text = "Нажать"; button.Click += new EventHandler(this.button_Click); this.Controls.Add(button); } private void button_Click(object sender, EventArgs e) { Assembly a = Assembly.Load("ClassLibrary1");//dll расположенная в папке с приложением object obj = a.CreateInstance("MyForm"); Type t = a.GetType("MyForm"); MethodInfo mi = t.GetMethod("Launch"); mi.Invoke(obj, null); EventInfo mi1 = t.GetEvent("OnSend"); } private void OnSendText(string obj) { list.Items.Add(obj); } } } How can I not understand how I can subscribe to an event in the dll. What are some thoughts on this? There is no experience at all.


