本文介绍了将对象转换为按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下点击事件处理程序 //点击事件 public void ProcessClick(对象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 如果回复小组,请不要给我发邮件 没什么。但是,作为替代方案,你可以考虑这个: 按钮按钮=发送者为按钮; if(按钮!= null) { //做你的东西 } 这可以防止出现转换错误的可能性。顺便说一句,为什么当按钮有很多其他标识属性时,使用标签 ? - 有10种人那些懂二元的人和那些没有b $ b的人。 http://code.acadx.com (拉针回复) I have the following Click event handler//Click eventpublic void ProcessClick (Object aoSender, EventArgsaoArgs){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 解决方案There''s nothing wrong with the code you posted - are you sure thecompiler is complaining about that line?--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me tooNothing. 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 tagwhen a button has so many other identifying properties?--There are 10 kinds of people. Those who understand binary and those whodon''t. http://code.acadx.com(Pull the pin to reply) 这篇关于将对象转换为按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 13:20