开发者

About using arguments passed to the CheckedChanged event of Checkbox

How do I display the value of the "Text" property of a checkbox when it is clicked?

So with the CheckedChanged event, two arguments of type sender & eventArgs are passed.

How do I the same using these a开发者_如何学Pythonrguments?


Yes, but do you possibly also want to unset it if it's unchecked?

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CType(sender, CheckBox).Checked Then
        Label1.Text = CType(sender, CheckBox).Text
    Else
        Label1.Text = ""
    End If
End Sub


You have tagged with both C# and VB.NET, which are you using? Depending on this, you can use one of the following to get the CheckBox which fired the argument.

C#:

(CheckBox)sender

VB:

CType(sender, CheckBox)

You can then check the Text property of the object.


It seems to me that the answers suggested thus far are overkill in that the sub has the "Handles" qualifier... This means the ONLY time this will get called is when the Checkbox1 is modified. So you could use: Checkbox1.text directly.

The answers above are better suited for a more generic case where you want the SAME routine to HANDLE many checkboxes and so need to down_select to the appropriate ("calling") checkbox.

The subtlety here, Mr. Tejas, is that the NAME of the SUB (i.e. Checkbox1_CheckChanged) may or may not have anything to do with Checkbox1 depending upon the HANDLES phrase at the end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜