将对象转换为按钮 [英] Casting of Object to Button

查看:85
本文介绍了将对象转换为按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下点击事件处理程序


//点击事件

public void ProcessClick(对象aoSender,EventArgs

aoArgs)

{

MessageBox.Show(" Clicked on" +((Button)

aoSender).Tag.ToString() );

}

当我编译时我得到以下错误

"错误CS0029:无法隐式转换类型''对象''

到''System.Windows.Forms.Button''


我在这里做错了什么?

谢谢

解决方案




您确定该行发生错误吗?

-

Miha Markic - RightHand .NET咨询&开发

miha at rthand com
www.rhand.com


" br" <一个******* @ discussions.microsoft.com>在消息中写道

news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>我有以下Click事件处理程序

// Click事件
public void ProcessClick(Object aoSender,EventArgs
aoArgs)
{<消息框.Show(" Clicked on" +((Button)
aoSender).Tag.ToString());
}

当我编译时得到以下错误
错误CS0029:无法隐式转换类型''对象''
到''System.Windows.Forms.Button''

我在做什么错了吗?
谢谢



br< an ******* @ discussion.microsoft.com>写道:

我有以下点击事件处理程序

//点击事件
public void ProcessClick(Object aoSender,EventArgs
aoArgs)
{
MessageBox.Show(" Clicked on" +((Button)
aoSender).Tag.ToString());
}

及时我编译我得到以下错误
错误CS0029:无法隐式转换类型''对象''
到''System.Windows.Forms.Button''

什么我在这里做错了吗?




您发布的代码没有问题 - 您确定

编译器在抱怨那条线?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


br写道:

我是什么在这里做错了吗?




没什么。但是,作为替代方案,你可以考虑这个:


按钮按钮=发送者为按钮;

if(按钮!= null)

{

//做你的东西

}


这可以防止出现转换错误的可能性。顺便说一句,为什么当按钮有很多其他标识属性时,使用标签




-

有10种人那些懂二元的人和那些没有b $ b的人。

http://code.acadx.com

(拉针回复)


I have the following Click event handler

//Click event
public void ProcessClick (Object aoSender, EventArgs
aoArgs)
{
MessageBox.Show ("Clicked on " + ((Button)
aoSender).Tag.ToString());
}
and when I compile I get the following error
"error CS0029: Cannot implicitly convert type ''object''
to ''System.Windows.Forms.Button''

What am I doing wrong here?
Thanks

解决方案

Hi,

Are you sure that error occurs at that line?
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"br" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

I have the following Click event handler

//Click event
public void ProcessClick (Object aoSender, EventArgs
aoArgs)
{
MessageBox.Show ("Clicked on " + ((Button)
aoSender).Tag.ToString());
}
and when I compile I get the following error
"error CS0029: Cannot implicitly convert type ''object''
to ''System.Windows.Forms.Button''

What am I doing wrong here?
Thanks



br <an*******@discussions.microsoft.com> wrote:

I have the following Click event handler

//Click event
public void ProcessClick (Object aoSender, EventArgs
aoArgs)
{
MessageBox.Show ("Clicked on " + ((Button)
aoSender).Tag.ToString());
}
and when I compile I get the following error
"error CS0029: Cannot implicitly convert type ''object''
to ''System.Windows.Forms.Button''

What am I doing wrong here?



There''s nothing wrong with the code you posted - are you sure the
compiler is complaining about that line?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


br wrote:

What am I doing wrong here?



Nothing. However, as an alternative you might consider this:

Button button = sender as Button;
if (button != null)
{
// do your thing
}

This prevents the possibility of a casting error. BTW, why use the tag
when a button has so many other identifying properties?

--
There are 10 kinds of people. Those who understand binary and those who
don''t.

http://code.acadx.com
(Pull the pin to reply)


这篇关于将对象转换为按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆