[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? :wave:
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
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.
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
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:
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).
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:
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.