0

is it possible to adding a click method to a class in dll from another project?
I want to create a class (Class1) in a class library and build a dll from it.
I will use that class in a project with references the dll.

This is my class (Class1)

public class Class1
{
    public ImageMap map = null;
    public Class1(Form f)
    {
        map = new ImageMap();

        map.RegionClick += f.RegionMap_Clicked;
    }
}

and this is my form (Form1) in another project.

public partial class Form1 : Form
{
    Class1 c = null;

    public Form1()
    {
        InitializeComponent();

        c = new Class1(this);
    }

    void RegionMap_Clicked(int index, string key)
    {
        MessageBox.Show(key);
    }
}

This is my first time asking here. So, sorry if my english is bad.

Kuro13
  • 105
  • 1
  • 9
  • Yes, this is possible. Being in a DLL doesn't change anything. What's the question? Have you *tried* it? – Jonathon Reinhart Dec 19 '13 at 08:17
  • Yes, i already try this. but its not working. the DLL can't be build. It doesn't recognize Form1 because it is build in another solution. – Kuro13 Dec 19 '13 at 08:23
  • Are you trying to add the event handler from inside the DLL or from the project containing Form1? – Darkzaelus Dec 19 '13 at 08:28
  • The problem is that what you're trying to do requires a [circular reference](http://stackoverflow.com/questions/3719730/how-to-deal-with-circular-references) between your application and your DLL. – Jonathon Reinhart Dec 19 '13 at 08:33
  • i want to add the event which is declared in Form1 to an object in Class1 in DLL. – Kuro13 Dec 19 '13 at 08:34
  • Oh yes, i think i need that circular reference stuff. So, basically i have to create another project connecting this 2 project? or is there a way to create a pointer to the event handler and passing the pointer to the class? – Kuro13 Dec 19 '13 at 08:43
  • Have you considered using delegate? – Janne Matikainen Dec 19 '13 at 08:46

3 Answers3

0

Yes it's possible, don't forget to make your handler public:

public void RegionMap_Clicked(int index, string key)
{
    MessageBox.Show(key);
}
Tony
  • 7,345
  • 3
  • 26
  • 34
  • i can't passing Form1 to Class1 constructor parameter because it is create in another project. – Kuro13 Dec 19 '13 at 08:32
  • It's not a problem, have you added a reference to assambly with Form1? http://msdn.microsoft.com/en-us/library/7314433t(v=vs.90).aspx – Tony Dec 19 '13 at 08:44
0

You should do this

public class Class1
{
    public ImageMap map = null;
    public Class1(Form1 f)
    {
        map = new ImageMap();

        map.RegionClick += f.RegionMap_Clicked;
    }
}

then

public partial class Form1 : Form
{
    Class1 c = null;

    public Form1()
    {
        InitializeComponent();

        c = new Class1(this);
    }

    public void RegionMap_Clicked(int index, string key)
    {
        MessageBox.Show(key);
    }
}

and of couse you shold add using for this assembly.

I think now it will be working well

  • Thank you for the answer, but this can't be used because the Class1 and the Form1 is in the different solution. So, the Form1 can use the Class by "using the.dll" but the class can't use Form1 as a parameter. – Kuro13 Dec 19 '13 at 08:45
  • Do you mean the different solution is app and Form1 in the exe file? Right? So you can move Form1 code into a new solution as dll and used in app and with Class as dll. – Vladimir Kniazkov Dec 19 '13 at 09:01
0

Class1 can be made independent from Form1:

public class Class1
{
    public ImageMap Map = null;

    public Class1()
    {
        this.Map = new ImageMap();
    }
}

And Form1 uses Class1 however it likes:

public partial class Form1 : Form
{
    private Class1 c = null;

    public Form1()
    {
        InitializeComponent();

        this.c = new Class1();
        this.c.Map.RegionClick += this.RegionMap_Clicked;
    }

    private void RegionMap_Clicked(int index, string key)
    {
        MessageBox.Show(key);
    }
}

Thus only the Form1 project needs a reference to the Class1 project.

Max Yakimets
  • 1,195
  • 7
  • 12