Results 1 to 5 of 5

Thread: [RESOLVED] [2008] Stop activecontrol checkbox from becoming checked

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Resolved [RESOLVED] [2008] Stop activecontrol checkbox from becoming checked

    Hi all..

    Let me start out by saying my skill with vb .net is somewhere between unexperienced and slightly experienced, so don't go to hard on me

    I have around 100 checkboxes on my form and when checked or unchecked they need do something depending on the state of the checkbox.

    Looking at my code below you can see after the else statement that if the condition isn't true, I want the checkbox to remain unchecked.

    Help me out here guys..I think I need to do something with an event handler?

    Code:
        Private Sub chkAir_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDustDevil.CheckedChanged, chkLightningBolt.CheckedChanged, 'etc etc
            chkboxstatus()
        End Sub
    
        Private Sub chkboxstatus()
            If ActiveControl.Tag = "c" Then
                txtCharSkillPts.Text = txtCharSkillPts.Text + txtSpellSkillPts.Text
                ActiveControl.Tag = "u"
            Else
                If txtCharSkillPts.Text >= txtSpellSkillPts.Text Then
                    txtCharSkillPts.Text = txtCharSkillPts.Text - txtSpellSkillPts.Text
                    ActiveControl.Tag = "c"
                Else
                    'don't allow checkbox to be checked
                End If
            End If
        End Sub
    Last edited by rhijaen; Feb 10th, 2008 at 02:04 AM.

  2. #2
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Stop activecontrol checkbox from becoming checked

    You can either make the checkbox's Enabled property False, or its Checked property False. Both should work, but in order to give a better solution you need to provide us with more info on EXACTLY what you want to do and when to do it.

  3. #3
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: [2008] Stop activecontrol checkbox from becoming checked

    Your exact problem is not clear to me but you've said you want youre checkbox to remain unchecked (strange but everthing goes nah?)

    Step 1 make a boolean on formlevel CheckChangeInprocess
    this boolean flags if you are already busy in a check changed event so you don't run the event triggering it again

    Watch this:

    Code:
    Dim CheckChangeInprocess as boolean
    
    Private Sub chkAir_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDustDevil.CheckedChanged, chkLightningBolt.CheckedChanged, 'etc etc
            
    if CheckChangeInprocess  then exit sub
         CheckChangeInprocess = True
         chkboxstatus()
         CheckChangeInprocess  = False
    End Sub
    
    Private Sub chkboxstatus()
            If ActiveControl.Tag = "c" Then
                txtCharSkillPts.Text = txtCharSkillPts.Text + txtSpellSkillPts.Text
                ActiveControl.Tag = "u"
            Else
                If txtCharSkillPts.Text >= txtSpellSkillPts.Text Then
                    txtCharSkillPts.Text = txtCharSkillPts.Text - txtSpellSkillPts.Text
                    ActiveControl.Tag = "c"
                Else
                    chkAir_CheckedChanged.Checked = False
                End If
            End If
     End Sub
    the boolean CheckChangeInprocess prevents triggering the code to run again when you set the checkbox.check to false
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2008] Stop activecontrol checkbox from becoming checked

    I'm not entirely sure what you mean but i think what you want to do is put this line in the Else statement:

    vb Code:
    1. DirectCast(ActiveControl, CheckBox).Checked = False

    However, this may lead to problems so be careful.

    I also notice you are doing maths on Strings... Shouldn't you be converting your values in the text property of the textboxes to Integers or Doubles (whichever suits).
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Stop activecontrol checkbox from becoming checked

    Oh sorry I didn't explain myself clearly I guess..

    In order to keep the code neater and not have hundreds of lines just for checkboxes, I have checkboxes grouped into groupboxes and am putting all the checkboxes in that group in the handles..

    This is the full sub..

    Code:
        Private Sub chkAir_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDustDevil.CheckedChanged, chkLightningBolt.CheckedChanged, chkWordofRecall.CheckedChanged, chkCallLightning.CheckedChanged, chkChainLightning.CheckedChanged, chkVortexofAir.CheckedChanged, chkNimbleness.CheckedChanged, chkElectricShield.CheckedChanged, chkStaticShield.CheckedChanged, chkInivisibility.CheckedChanged, chkSWGateway.CheckedChanged, chkHightowerGateway.CheckedChanged, chkMylanthiaGateway.CheckedChanged, chkTornado.CheckedChanged, chkHurricane.CheckedChanged, chkFogofIllusion.CheckedChanged, chkSuffocate.CheckedChanged, chkAirMasteryI.CheckedChanged, chkNimbusCloud.CheckedChanged
            RetrievespellDetails(ActiveControl.Text)
            chkboxstatus()
        End Sub
    So there is no checkbox named chkAir, just all the checkboxes are in the handle

    Quote Originally Posted by stimbo
    I'm not entirely sure what you mean but i think what you want to do is put this line in the Else statement:

    vb Code:
    1. DirectCast(ActiveControl, CheckBox).Checked = False

    However, this may lead to problems so be careful.

    I also notice you are doing maths on Strings... Shouldn't you be converting your values in the text property of the textboxes to Integers or Doubles (whichever suits).
    Thank you, this seems to work well.
    Last edited by rhijaen; Feb 9th, 2008 at 11:46 PM.

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