本文介绍了转换事件处理程序的Sender参数以读取控件的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Borland C ++ Builder 6.0编写Form应用程序。我放置了2个 TImage 控件,并生成了 OnClick 事件处理程序,如下所示:

I am writing a Form application using Borland C++Builder 6.0. I have put 2 TImage controls and I have generated the OnClick event handler as shown below:

void __fastcall TForm1::Image1Click(TObject *Sender)
{
   AnsiString imageName;

   TImage *image;

   // How can I get the image name via the *Sender ?
   // How can I convert *Sender into TImage
   image = (TComponent)*Sender;

   imageName = image->Name;
}

我已经分配了相同的 OnClick TImage 控件上的$ c>事件。

I have assigned the same OnClick event on both of my TImage controls.

我想要实现的是拥有一个事件处理程序读取被点击的 TImage Name

What I want to achieve is to have one event handler that reads the Name of the TImage which is clicked.

据我所知,这可以通过 TObject * Sender 参数完成,但是我不明白如何转换 Sender 转换为 TImage

As far as I know, this can be done through the TObject *Sender parameter, but I cannot understand how I can convert the Sender into a TImage.

推荐答案

一个简单的类型转换就足够了,但是您的语法是错误的。改为尝试以下操作:

You are on the right track that a simple type-cast will suffice, but your syntax is wrong. Try this instead:

void __fastcall TForm1::Image1Click(TObject *Sender)
{
   TImage *image = (TImage*)Sender;
   // alternatively:
   // TImage *image = static_cast<TImage*>(Sender);

   AnsiString imageName = image->Name;
}

这篇关于转换事件处理程序的Sender参数以读取控件的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:42