Visual C 中的事件处理
有两个图片框,其中有两个不同的图像。
如果我单击一个图片框,其中的图像应该被清除。
更糟糕的是,两个图片框都只有一个公共事件处理程序。 我如何知道哪个图片框生成了该事件? 我很欣赏 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的演员阵容怎么样? 在大多数情况下,我会使用:(
请注意,在 Josh 指出我的参考缺陷后,我已经更正了上面的代码。谢谢!)
如果可以转换,动态转换将为您提供正确的对象类型,如果不能转换,则为 null (它相当于 C# 中的“as”)
如果这确实给你一个空引用,那么也许你的发件人不是你想象的那样?
How are you doing the cast? In most cases like this I would use:
(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?
您可以使用发送者对象。 将其投射到图片框控件并将其与两个可用的图片框进行比较。
我的 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.
吉安纳卡基斯,
问题是,当我尝试将 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.
您确定发送者对象实际上是您假设的类型吗?
Are you sure the sender object actually is the type you assume it to be?
如果您正在尝试 Toji 提供的代码,那么您就会遇到问题 - 试试这个:
与 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:
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.你打算如何选角? 我通常会使用
dynamic_cast
或safe_cast
:或者
从那里它应该非常简单......
How are you trying to cast? I'd generally use
dynamic_cast
orsafe_cast
:or
It should be pretty straight-forward from there...