How to: Get "name" property of button from click event

G

Guest

Using .Net Compact Framework, I have an ImageButton control (derived from the
Button control) this is wired to a click event. The click event's receives
the standard arguments (sender, e). How is it possible from within the click
event function to get the "name" property of the control? For example:


ImageButton ImageButton1 = new ImageButton();
ImageButton1.Click += new System.EventHandler(ImageButton1_Click);
.... code
private void ImageButton1_Click(object sender, System.EventArgs e)
{
string name = ((ImageButton)sender).name???
}

Thanks,
 
S

sadhu

Doesn't your code work?

private void ImageButton1_Click(object sender, System.EventArgs e)
{
string name = ((ImageButton)sender).Name;
}
 
G

Guest

No. In the VS .NET 2003, i get the error: 'System.ImageButton' does not
contain a definition for 'Name'. I've tried this w/ more conventional
controls, like the Button control, and i get: 'System.Windows.Forms.Button'
does not contain a definition for 'Name'

??
 
C

Chris Dunaway

It doesn't look as if the ImageButton has a Name property. Have you
tried the ID property? Will that work for you?

Just a thought.
 
G

Guest

There is no "ID" property either. This is quite strange as in "design mode" i
can set such properties...
 
S

sadhu

Are you sure you are using Name (typed in the correct case)? Remember,
C# is case sensitive.

The Button class does have a Name property, in fact, if you read
through the InitializeComponent() method in your form class, you'll see
that it actually sets the Name to whatever you typed in the UI. The
Name property is defined in the Control base class, and so should be
available to all controls.

Regards
Senthil
 
G

Guest

Yes, the "Name" value is properly typed. I'm not an expert, but my
ImageButton class (which i downloaded from some spot on the Web) is not
derived from the Button class, but instead from the "Control" class. At
least, i think this is so. The code reads:

public class ImageButton : Control
{
// ... code
}

In the Compact Framework, there is no "ImageButton" as there is in the
complete .Net Framework. And, i believe that the actual image that appears on
the screen is derived from the system.drawing.image class.

Does this make it easier for anyone to answer my question? Thanks.
 
G

Guest

Yes, i guess this is the obvious solution. After reading a chapter on
properties and access attributes, i was able to make the change in 5 minutes.
For folks new to OOP like myself, it sometimes is necessary to point out the
obvious.

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top