简体   繁体   English

如何在Visual C ++中的单击事件处理程序中发送2个以上的参数(delegate => thing isnt working)

[英]how to send more than 2 arguments in a click event handler in Visual C++ (delegate => thing isnt working)

this is what im doing right now !!! 这就是我现在正在做的!

//picture box
pct->Click += gcnew EventHandler(this,&fornow::Form1::Display);

private: System::Void Display(System::Object^ sender, System::EventArgs^ e){
    PictureBox^ pb = safe_cast<PictureBox^>(sender);
    pictureBox1->Image = pb->Image;
}

Apparently I cannot use delegates in C++/Cli im pretty new to this kind of coding !! 显然我不能在C ++ / Cli中使用代表这种编码的新手! So could anyone tell me how to send some more arguments to the eventhandler method after clicking so i can use it in the code there !!! 所以有人可以告诉我如何在点击之后向eventhandler方法发送更多参数,这样我就可以在代码中使用它!

Like I have a System::String , if I could send it like this may be i could write 就像我有一个System :: String,如果我能像这样发送它可能是我可以写的

private: System::Void Display(System::Object^ sender, System::EventArgs^ e, System::String^  s){
    Mat img = imread(s);
    imshow("",img);  
    waitKey(0);
}

im compiling it with /clr and im using trial version vs 2010 Visual C++ . 即时编译使用/ clr和im使用试用版vs 2010 Visual C ++。

this code resides in a buttonclick event handler... ive added that part of the code ... 这段代码驻留在buttonclick事件处理程序中......我添加了部分代码......

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    // array<System::String^>^ files = 
    //     System::IO::Directory::GetFiles(textBox1->Text,"*.*",
    //     System::IO::SearchOption::AllDirectories);


    pin_ptr<const wchar_t> wch = PtrToStringChars(textBox1->Text);
    wchar_t wch1[260];
    wcscpy(wch1,wch);
    list<basic_string<wchar_t>> dir;
    list<string> s;
    retrieve *r = new retrieve();


    dir = r->print(wch1,dir);

    for(list<basic_string<wchar_t>>::iterator itr = dir.begin(); itr != dir.end();itr++)
    {
        cout<<std::string(itr->begin(),itr->end())<<endl;
        s.push_back(std::string(itr->begin(),itr->end()));
    }

    filter *f = new filter();  
    s=f->filter1(s); // fills s with paths (strings)
    flowLayoutPanel1->Controls->Clear();

    for(list<string>::iterator i=s.begin();i!=s.end();i++)
    {
        std::string sn = std::string(i->begin(),i->end());
        System::String^ s = gcnew System::String(sn.c_str());

        PictureBox^ pct = gcnew PictureBox();
        if(checkBox1->Checked==true)
            pct->Image = Image::FromFile(s)->GetThumbnailImage(50,50,nullptr,System::IntPtr::Zero);
        if(checkBox1->Checked==false)
            pct->Image = Image::FromFile(s);

        pct->Size = System::Drawing::Size(100,100);
        flowLayoutPanel1->Controls->Add(pct);
        pct->Click += gcnew EventHandler(this,&fornow::Form1::Display);
        Mat img = imread(sn);
        imshow(sn,img);
        waitKey(0);
    }

Im sorry im really new to this kind of coding .. any suggestions will be helpful thanks !!! 对不起我对这种编码真的很新..任何建议都会有所帮助谢谢!

You can't add additional parameters to the click event. 您无法向click事件添加其他参数。 If you could, where would you expect the UI to get the string from? 如果可以,您希望UI从哪里获取字符串?

Given your comments on lambdas, I think what you're looking for is to have an event handler that is slightly different depending on which picture was clicked. 鉴于你对lambdas的评论,我认为你要找的是一个事件处理程序,根据点击的图片略有不同。 Is that correct? 那是对的吗? Something like this in C#: 在C#中有类似的东西:

// If you were writing C#, is this what you'd do?
pct1.Click += (s,e) => imshow(imread("one.jpg"));
pct2.Click += (s,e) => imshow(imread("two.jpg"));

There's no strict equivalent to that in C++/CLI. 没有严格的等同于C ++ / CLI中的等价物。 However, if you think about what the lambdas are doing, you can do that in C++/CLI. 但是,如果你考虑一下lambda正在做什么,你可以在C ++ / CLI中做到这一点。 The lambdas are turned into regular methods on the same class, and you can do that in C++/CLI. lambdas在同一个类中被转换为常规方法,您可以在C ++ / CLI中执行此操作。

pct1->Click += gcnew EventHandler(this,&fornow::Form1::Display1);
pct2->Click += gcnew EventHandler(this,&fornow::Form1::Display2);

void Display1(Object^ sender, EventArgs^ e)
{
    Display(safe_cast<PictureBox^>(sender), "one.jpg");
}

void Display2(Object^ sender, EventArgs^ e)
{
    Display(safe_cast<PictureBox^>(sender), "two.jpg");
}

void Display(PictureBox^ pb, String^ file)
    Mat img = imread(file);
    imshow(pb, img);  
    waitKey(0);
}

Alternatively, you could have a single event handler, which looked at which picture was clicked (the sender) to do its work. 或者,您可以使用单个事件处理程序,该处理程序查看单击了哪个图片(发件人)以执行其工作。

pct1->Click += gcnew EventHandler(this,&fornow::Form1::Display);
pct2->Click += gcnew EventHandler(this,&fornow::Form1::Display);

void Display(Object^ sender, EventArgs^ e)
{
    PictureBox^ pb = safe_cast<PictureBox^>(sender)
    String^ file;

    if(pb == this->pct1) file = "one.jpg";
    else if(pb == this->pct2) file = "two.jpg";
    else return;

    Mat img = imread(file);
    imshow(pb, img);  
    waitKey(0);
}

Pack the parameters into a ref class derived from EventArgs, then you can use the generic version of EventHandler like this 将参数打包到从EventArgs派生的ref类中,然后您可以像这样使用EventHandler的泛型版本

public ref class MyEventArgs:EventArgs
{
public:
    property String^ Parameter1;
}

public ref class MyClass
{
public:
    event EventHandler<MyEventArgs^> ^ MyEvent;
    void RaiseMyEvent(Object^ sender, String^ parameter1)
    {           
        MyEventArgs^ args=gcnew MyEventArgs();
        args->Parameter1=parameter1;
        MyEvent(sender, args);
    }
};

Your event handler methods would have the signature 您的事件处理程序方法将具有签名

void MyEventHandler(Object^ sender, MyEventArgs^ args)

and have access to the values passed to RaiseMyEvent via the properties of the args parameter. 并且可以通过args参数的属性访问传递给RaiseMyEvent的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM