如何在控件事件中操作控件本身

sheyouyou1 2009-04-05 03:20:54
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox1 .TextLength ==0)
{
if (e.KeyChar ==(char)48)
{
e.Handled = true;
}
}
if ((e.KeyChar < (char)48 || e.KeyChar > (char)57) && e.KeyChar != (char)8)
{
e.Handled = true;
}

}
比如上面的代码我不想在事件中指明textBox1,而是用事件所对应的对象来操作要怎么写?
...全文
62 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
小弟万元户 2009-04-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sheyouyou1 的回复:]
谢谢xitangzi 刚刚自己也弄出来了,前面 (TextBox)sender.老是不行
[/Quote]

怎么不行?那不是强制转换吗?出什么错吗?
小弟万元户 2009-04-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ojlovecd 的回复:]
sender就是引发事件的对象
[/Quote]
Right
sheyouyou1 2009-04-05
  • 打赏
  • 举报
回复
谢谢xitangzi 刚刚自己也弄出来了,前面 (TextBox)sender.老是不行
我姓区不姓区 2009-04-05
  • 打赏
  • 举报
回复
sender就是引发事件的对象
fengjian_428 2009-04-05
  • 打赏
  • 举报
回复
看看在KeyPressEventArgs e 里面取不取得到
小弟万元户 2009-04-05
  • 打赏
  • 举报
回复

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (((TextBox)sender).TextLength ==0)
{
if (e.KeyChar ==(char)48)
{
e.Handled = true;
}
}
if ((e.KeyChar < (char)48 || e.KeyChar > (char)57) && e.KeyChar != (char)8)
{
e.Handled = true;
}

}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧