|
-
Jul 23rd, 2004, 09:53 AM
#1
sender question
I have a 3 checkboxes and a sub that handles all 3 of their CheckChanged events. Basically I need to prevent them from unchecking ALL as at least 1 needs to be checked. When they uncheck the lasted checked box it will give a messagebox stating they need to select at least 1, and i want it to recheck the one they just unchecked. I imagine I would be able to reference it via the sender object passed in, but I am having a hard time figuring out how to reference this to the checkbox itself?
I got this code to work, but im not sure if I NEED to actually create a new object to accomplish this
VB Code:
MessageBox.Show("At least 1 checkbox must be checked", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
Dim MyCheck As New CheckBox
MyCheck = CType(sender, CheckBox)
MyCheck.Checked = True
MyCheck = Nothing
is this efficient?
-
Jul 23rd, 2004, 10:03 AM
#2
Frenzied Member
I'm not smart enough to know whether this is more effecient or not, but you can also do it like this:
VB Code:
CType(sender, CheckBox).Checked = True
What I don't know is if that's any different or not. I mean, does that act on the sender object itself, or does CType create a new object. If it does, I guess it would not be more effecient than your code, but it is less typing.
Mike
-
Jul 23rd, 2004, 10:42 AM
#3
more importaintly, if it DOES create an instance... does it destroy it as well?
-
Jul 23rd, 2004, 10:51 AM
#4
Frenzied Member
I *guess* it does create an object. MSDN says "Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface".
Also guessing (big help here, huh?) that once it goes out of scope, it's eligible for GC. In that case, there's no variable to set to Nothing.
-
Jul 23rd, 2004, 11:00 AM
#5
Frenzied Member
Not sure if this would be considered bad code or not, but this works just fine:
VB Code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
sender.Checked = True
End Sub
-
Jul 23rd, 2004, 11:18 AM
#6
Lively Member
Try this:
VB Code:
Sub CheckedChanged(ByVal Sender As Object, ByVal e As System.EventArgs) Handles CBx1.CheckedChanged, CBx2.CheckedChanged, CBx3.CheckedChanged
If Not (CBx1.Checked Or CBx2.Checked Or CBx3.Checked) Then
MessageBox.Show("...")
CType(sender, CheckBox).Checked = True
End If
End Sub
Mike Hildner's first reply was good, but he went backwards at this last reply becsue Option Strickt On disallows late binding.
Last edited by TLord; Jul 23rd, 2004 at 11:24 AM.
Do you think my life is easy?
Do you think it's good to win?
do you think it's nice to kill?
Do you think learning is a must?
Do you think computers are nothing?
Do you think this post is stupid?
Do ypu think we're really humen?
DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !
-
Jul 23rd, 2004, 11:29 AM
#7
I wonder how many charact
No need for expensive CTYPE operation. You will always be passed a checkbox, so
no need for Ctype.
VB Code:
'this only assigns a reference to
'the sending object
'but lets the compiler
'know that it will always
'be a checkbox
Dim myCheckBox As CheckBox = DirectCast(sender, CheckBox)
'or
DirectCast(Sender,CheckBox).Checked=True
-
Jul 23rd, 2004, 12:05 PM
#8
Originally posted by nemaroller
No need for expensive CTYPE operation. You will always be passed a checkbox, so
no need for Ctype.
VB Code:
'this only assigns a reference to
'the sending object
'but lets the compiler
'know that it will always
'be a checkbox
Dim myCheckBox As CheckBox = DirectCast(sender, CheckBox)
'or
DirectCast(Sender,CheckBox).Checked=True
thanks for the input, ill use the directcast method
-
Jul 23rd, 2004, 06:44 PM
#9
PowerPoster
Originally posted by Mike Hildner
Not sure if this would be considered bad code or not, but this works just fine:
VB Code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
sender.Checked = True
End Sub
This wont work with Option Strict On
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|