加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB.NET学习--(1)

发布时间:2020-12-16 23:33:53 所属栏目:大数据 来源:网络整理
导读:在运行时创建事件处理程序 在Form1的Load事件中写入如下代码,把 ButtonClick 事件处理程序关联到 Button1.Click 事件。 AddHandler Button1.Click,AddressOf ButtonClick 确定所按下的组合键 对 ModifierKeys 属性和 Keys 枚举的值使用按位与运算符(在 Vis

在运行时创建事件处理程序

在Form1的Load事件中写入如下代码,把 ButtonClick事件处理程序关联到Button1.Click事件。

AddHandler Button1.Click,AddressOf ButtonClick 

确定所按下的组合键

对 ModifierKeys 属性和 Keys 枚举的值使用按位与运算符(在 Visual Basic 中为 And),确定所按下的组合键。(ModifierKeys 是 Control 类的共享成员),如下代码所示,响应了同时按下了Shift和Control组合键的情况

Private Sub Form1_KeyDown(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If (Control.ModifierKeys And Keys.Shift + Keys.Control) = Keys.Shift + Keys.Control Then

MessageBox.Show("你同时按下了Shift和Control组合键")

End if

End Sub

提示:我们需要在窗体属性中把 KeyPreview 属性设置为true,才能让响应按键的事件。

响应按钮单击

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click

Dim MyButton As Button = CType(sender,Button)

MyButton.Text = "Change my text"

End Sub

sender表示的是触发该事件的事件源,我们需要使用Ctype函数来转换为对应的类才能使用。

效果:点击窗体上的Button1控件,按钮的文字将变为"Change my text"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读