Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

How to associate a custom event function with multiple arguments to a managed type event?

$
0
0

Hi,

New to event driven programming. Please correct my misuse of terms.

In Visual C++ the code generator generates such event handler function:

private: System::Void foo(System::Object^  sender, System::EventArgs^  e)

And call the function like this:

this->foo->CheckedChanged += gcnew System::EventHandler(this, &Main::bar);

For some reason I want to have more than two arguments for an event handler function, so I can adapt this function to more events. I have already got here:

//before InitializeComponent() function
public: delegate void locker(System::Object^, array<System::Object^>^);
public: event locker^ lockCheckBoxes;

//somewhere in the code
private: System::Void lock_CheckBoxArray(System::Object^  sender, array<System::Object^>^  target){
/*
doing stuff here. e.g.:
if(sender->Checked)
  target->Checked=true;
else
  target->Checked=false;
*/
}

//finally in the InitializeComponent() function.
this->tab1lock->CheckedChanged += gcnew locker(tab1lock,tab1lockers);

This code gives the following errors:

1>foo\Main.h(556): error C3924: error in argument #2 of delegate constructor call 'foo::Main::locker':
1>          pointer to member function expected

So I changed the argument #2.

this->tab1lock->CheckedChanged += gcnew locker(tab1lock,&lock_CheckBoxArray);

This code gives the following errors:

1>foo\Main.h(556): error C2276: '&' : illegal operation on bound member function expression
1>foo\Main.h(556): error C3924: error in argument #1 of delegate constructor call 'foo::Main::locker':
1>          pointer to member function expected

It says for argument #1, a type foo::Main is required. So I changed the code to:

this->tab1lock->CheckedChanged += gcnew locker(this,&lock_CheckBoxArray);

This code gives the following errors:

1>foo\Main.h(556): error C2276: '&' : illegal operation on bound member function expression
1>foo\Main.h(556): error C3364: 'foo::Main::locker' : invalid argument for delegate constructor; delegate target needs to be a pointer to a member function

Now sure what to do now.


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>