shared checkbox_CheckChanged event

D

David A. Beck

I have several checkboxes in a group. Only two can be checked at any one
time. I made one _CheckChanged event to handle all of these checkboxes. By
setting the AutoPostback = True on them all I can total up the number of
boxes that are checked and set a nastygram if more than two are checked. I
would also like to uncheck the offending checkbox (the third one). Can
anyone tell me how to get the ID of the checkbox that raised the event?

David A. Beck
Private Sub cbxOBJ_Preserve_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbxOBJ_Preserve.CheckedChanged,
cbxOBJ_Investment.CheckedChanged, cbxOBJ_CapitalAppreciation.CheckedChanged,
cbxOBJ_TradingProfit.CheckedChanged, cbxOBJ_Speculation.CheckedChanged
 
J

Jeffrey Tan[MSFT]

Hi David,

Thanks for posting in this group.
In the shared CheckChanged event, I think there are 2 ways to determine the
sender checkbox:
1. Convert sender object to CheckBox, then refer to its ID property, like
this:
CheckBox cb=(CheckBox)sender;
Response.Write(cb.ID.ToString());

2. Compare the reference pointer, like this:
//CheckBox1, CheckBox2, CheckBox3, CheckBox4 are the 4 checkbox references.
if(sender.Equals(CheckBox1))
{
Response.Write("CheckBox1");
}

if(sender.Equals(CheckBox2))
{
Response.Write("CheckBox2");
}

if(sender.Equals(CheckBox3))
{
Response.Write("CheckBox3");
}

if(sender.Equals(CheckBox4))
{
Response.Write("CheckBox4");
}

Then, you can setup a static checkbox counter, if this counter exceeds 2,
then you can uncheck this checkbox.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

David A. Beck

Jeff, thanks so much. Your help let me put this code into the VB code-behind
module:

Dab
Private Sub cbxOBJ_Preserve_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbxOBJ_Preserve.CheckedChanged,
cbxOBJ_Investment.CheckedChanged, cbxOBJ_CapitalAppreciation.CheckedChanged,
cbxOBJ_TradingProfit.CheckedChanged, cbxOBJ_Speculation.CheckedChanged

Dim nCnt As Int32 = 0
Dim sID As String = ""
Dim cb As CheckBox = sender
'
sID = cb.ID
If cbxOBJ_Preserve.Checked Then nCnt += 1
If cbxOBJ_Investment.Checked Then nCnt += 1
If cbxOBJ_CapitalAppreciation.Checked Then nCnt += 1
If cbxOBJ_TradingProfit.Checked Then nCnt += 1
If cbxOBJ_Speculation.Checked Then nCnt += 1
If nCnt > 2 Then
lError.Text = "You are only allowed 2 Investment Objectives"
Session("Focus") = sID
cb.Checked = False
Else
lError.Text = ""
Session("Focus") = "rbAINC_Refused"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top