Visual C 中的事件处理

发布于 2024-07-09 08:38:07 字数 338 浏览 3 评论 0原文

有两个图片框,其中有两个不同的图像。

如果我单击一个图片框,其中的图像应该被清除。

更糟糕的是,两个图片框都只有一个公共事件处理程序。 我如何知道哪个图片框生成了该事件? 我很欣赏 C++-CLI 中的源代码,

我需要知道在函数内部要写什么:

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

}

编辑:问题是,当我尝试将 sender 转换为 picurebox 时,它会给出一个错误,指出类型无法转换。

There are two pictureboxes with two different images.

If I click on one picture box, the image in it should be cleared.

To make the matters worse, both of the picture boxes have only one common event handler.
How can I know which picturebox generated the event? I would appreciate source code in C++-CLI

I need to know what to write inside the function:

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

}

EDIT: The problem is that when I try to cast sender to picurebox, it gives an error saying that the types cannot be converted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只涨不跌 2024-07-16 08:38:07

你的演员阵容怎么样? 在大多数情况下,我会使用:(

PictureBox ^pb = safe_cast<PictureBox^>(sender);
if(pb != null) {
    // logic goes here
}

请注意,在 Josh 指出我的参考缺陷后,我已经更正了上面的代码。谢谢!)

如果可以转换,动态转换将为您提供正确的对象类型,如果不能转换,则为 null (它相当于 C# 中的“as”)

如果这确实给你一个空引用,那么也许你的发件人不是你想象的那样?

How are you doing the cast? In most cases like this I would use:

PictureBox ^pb = safe_cast<PictureBox^>(sender);
if(pb != null) {
    // logic goes here
}

(Note that I've corrected the above code after Josh pointed out my reference flaw. Thanks!)

the dynamic cast will give you the right object type if it can cast, or null if it cant (it's the equiv. of "as" in C#)

If this does give you a null reference, then perhaps your sender is not what you think it is?

浅听莫相离 2024-07-16 08:38:07

您可以使用发送者对象。 将其投射到图片框控件并将其与两个可用的图片框进行比较。

我的 Visual C++ 有点生疏,现在无法提供代码。

You can use the sender object. Cast it to a picture box control and compare it with the two available picture boxes.

My Visual C++ is a little bit rusty and can't provide code now.

萤火眠眠 2024-07-16 08:38:07

吉安纳卡基斯,
问题是,当我尝试将 sender 转换为 picurebox 时,它给出一个错误,指出类型无法转换。

kgiannakakis,
The problem is that when I try to cast sender to picurebox, it gives an error saying that the types cannot be converted.

彩扇题诗 2024-07-16 08:38:07

您确定发送者对象实际上是您假设的类型吗?

Are you sure the sender object actually is the type you assume it to be?

可可 2024-07-16 08:38:07

如果您正在尝试 Toji 提供的代码,那么您就会遇到问题 - 试试这个:

PictureBox ^pb = safe_cast<PictureBox^>(sender);

与 C# 不同,您不需要任何语法来表示托管堆对象,C++\CLI 会区分堆栈对象 (PictureBox pb)、指向堆对象的指针 (PictureBox *pb) 以及托管堆对象的句柄 (PictureBox ^pb)。 三者不是同一件事,有不同的寿命和用途。

If you're trying the code that Toji gave, then there's you're problem - try this:

PictureBox ^pb = safe_cast<PictureBox^>(sender);

Unlike C#, where you don't need any syntax to denote managed heap objects, C++\CLI differentiates between stack objects (PictureBox pb), pointers to heap objects (PictureBox *pb), and handles to managed heap objects (PictureBox ^pb). The three are not the same thing, and have different lifetimes and usages.

执笔绘流年 2024-07-16 08:38:07

你打算如何选角? 我通常会使用 dynamic_castsafe_cast

PictureBox ^ pb = dynamic_cast<PictureBox^>(sender);
if (pb != nullptr)
{
...
}

或者

try
{
    PictureBox ^ pb = safe_cast<PictureBox^>(sender);
    ...
}
catch(InvalidCastException ^ exp)
{
    // Handle a cast that went awry
}

从那里它应该非常简单......

How are you trying to cast? I'd generally use dynamic_cast or safe_cast:

PictureBox ^ pb = dynamic_cast<PictureBox^>(sender);
if (pb != nullptr)
{
...
}

or

try
{
    PictureBox ^ pb = safe_cast<PictureBox^>(sender);
    ...
}
catch(InvalidCastException ^ exp)
{
    // Handle a cast that went awry
}

It should be pretty straight-forward from there...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文