VB.NET 单选按钮处理程序代码运行两次 [英] VB.NET Radio Button Handler Code running twice

查看:34
本文介绍了VB.NET 单选按钮处理程序代码运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB.NET 中有一组 RadioButtons.我想创建一个函数来处理所有这些.我的代码如下.

I have a group of RadioButtons in VB.NET. I would like to create one function that will handle all of them together. My code is below.

    Private Sub employmentStatusChanged(ByVal sender As System.Object,_
          ByVal e As System.EventArgs) Handles rbtnEmployed.CheckedChanged, _ 
          rbtnUnemp.CheckedChanged, rbtnStudent.CheckedChanged, rbtnRetired.CheckedChanged
    If rbtnEmployed.Checked Then 
        Dim employ As New Employer
        employ.ShowDialog()
    ElseIf rbtnStudent.Checked Then
        Dim stud As New Students
        stud.ShowDialog()
    ElseIf rbtnRetired.Checked Then
        Dim employ As New Employer
        employ.ShowDialog()
    End If
End Sub

这个功能在我第一次点击按钮时工作正常.当我单击不同的按钮时,问题就出现了.它为第一个按钮的更改状态(从选中到未选中)触发一次,然后为第二个按钮(未选中到选中)触发一次.

This function works fine the first time I click a button. The problem comes when I click on a different button. It fires once for the first button's changed state (from checked to unchecked) and then again for the second button (unchecked to checked).

关于如何阻止这种情况发生的任何想法?提前致谢!

Any ideas on how to stop this from happening? Thanks in advance!

推荐答案

你不能.然而,sender 是被点击的单选按钮.只需检查它的状态.如果 sender.Checked 为 False,则从事件处理程序返回.

You can't. However, sender is the radio button that was clicked. Just check the state of that. If sender.Checked is False, return from the event handler.

这篇关于VB.NET 单选按钮处理程序代码运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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