Tuesday, November 7, 2017

How to get all objects from WINDOW (added from another classes)

Leave a Comment

I am working with specific program. It has "main window" (a.k.a. "form") where another extensions(protected dll, without access to their source code) can add some objects (texts, labels and etc..) in that window.

How can I access all the objects which is added on that "form" ?

here i example of typical extension:

.... namespace ProgramName.Extensions {      public class MyExtensionName : ProgramName.ExtensionBase{           //here goes typical codes to add things to "form" window,like:          Draw.Arrow(MainFormObject, ArrowColor, ArrowWidth)      }  } 

what code I can use to access all "add"-ed elements , from other, anonymous/inaccessible classed? Is there any "reflection" or any solution?

3 Answers

Answers 1

Adding some objects on a form using a dll requires that the form itself be referenced as an accessible member using something like:

 Assembly assembly = Assembly.LoadFile("C:\\test.dll"); Type type = assembly.GetType("test.dllTest"); Form form = (Form)Activator.CreateInstance(type); 

Then add:

form.Controls.Find(); 

If it is the current form, then on Load method use:

 private void Form_Load(object sender, EventArgs e)     {       this.Controls.Find("name",true)       // OR       foreach(Control c in this.Controls)         if(c.Name == Name)             //Do something...     } 

Answers 2

You can try this

foreach (Control ctrl in this.Controls) {     if (ctrl .GetType().GetProperty("Text") != null)     {         // code here                 } } 

Answers 3

protected dll, without access to their source code

  • These controls sound like COM/OLE/ActiveX:
  • You can access them, as long as you find the code wherein the Main form(The host program) they are maintained.
  • You can try search those controls' keywords in your project code to locate the relevant codes.
  • Including those DLLs into your project reference would provide you the chance to inspect keywords from DLLs.
  • Search the usage of these DLLs and find the code place you want to work with and do your things there.

Example how to get keywords out of DLLs:

enter image description here

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment