Results 1 to 9 of 9

Thread: sender question

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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:
    1. MessageBox.Show("At least 1 checkbox must be checked", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
    2.             Dim MyCheck As New CheckBox
    3.             MyCheck = CType(sender, CheckBox)
    4.             MyCheck.Checked = True
    5.             MyCheck = Nothing
    is this efficient?

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I'm not smart enough to know whether this is more effecient or not, but you can also do it like this:
    VB Code:
    1. 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

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    more importaintly, if it DOES create an instance... does it destroy it as well?

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Not sure if this would be considered bad code or not, but this works just fine:

    VB Code:
    1. Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    2.         sender.Checked = True
    3.     End Sub

  6. #6
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95
    Try this:

    VB Code:
    1. Sub CheckedChanged(ByVal Sender As Object, ByVal e As System.EventArgs) Handles CBx1.CheckedChanged, CBx2.CheckedChanged, CBx3.CheckedChanged
    2.   If Not (CBx1.Checked Or CBx2.Checked Or CBx3.Checked) Then
    3.     MessageBox.Show("...")
    4.     CType(sender, CheckBox).Checked = True
    5.   End If
    6. 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? ? ? ! ! !

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    No need for expensive CTYPE operation. You will always be passed a checkbox, so
    no need for Ctype.
    VB Code:
    1. 'this only assigns a reference to
    2. 'the sending object
    3. 'but lets the compiler
    4. 'know that it will always
    5. 'be a checkbox
    6.  
    7. Dim myCheckBox As CheckBox = DirectCast(sender, CheckBox)
    8.  
    9. 'or
    10. DirectCast(Sender,CheckBox).Checked=True

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by nemaroller
    No need for expensive CTYPE operation. You will always be passed a checkbox, so
    no need for Ctype.
    VB Code:
    1. 'this only assigns a reference to
    2. 'the sending object
    3. 'but lets the compiler
    4. 'know that it will always
    5. 'be a checkbox
    6.  
    7. Dim myCheckBox As CheckBox = DirectCast(sender, CheckBox)
    8.  
    9. 'or
    10. DirectCast(Sender,CheckBox).Checked=True
    thanks for the input, ill use the directcast method

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Mike Hildner
    Not sure if this would be considered bad code or not, but this works just fine:

    VB Code:
    1. Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    2.         sender.Checked = True
    3.     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
  •  



Click Here to Expand Forum to Full Width