Solved

Get the previous state of CheckBox when clicked?

Posted on 2008-11-01
17
372 Views
Last Modified: 2010-05-19
You are probably thinking: if current value is Checked, then prevoius value is unchecked. Well, I use 3 state value, checked, unchecked and intermediate. I need to know when current value is unchecked, wheather prevous value was checked or intermediate.

Thanks,
Goran
0
Comment
Question by:Priest04
  • 7
  • 5
  • 3
  • +2
17 Comments
 
LVL 19

Accepted Solution

by:
elimesika earned 500 total points
ID: 22859186
HI

The simple way to solve it is to store the previous state in a variable and set it each time the checkbox changes state by implementing the CheckedChanged event for this control.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22859204
You can store the previous value in a variable  after you have done your comparisons within the checked state changed event handler.

// Fetch previous value from variable.
CheckState previousState = this.prevState;
 
// Do your stuff here.
 
// Add value to variable.
this.prevState = checkBox.CheckState;

Open in new window

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22859211
There is no direct way to get the previous state of the check box. You have to store the state in a variable in the CheckStateChanged event handler and use this value to identify the previous state
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 13

Expert Comment

by:numberkruncher
ID: 22859236
If you wanted to keep the previous value fairly self-contained, you could place it within the Tag property of your check box (again within the checked state changed event handler):
// Get previous value, or your default when there was no previous.
CheckState previousValue = theCheckBox.Tag ?? CheckState.Checked;
 
// Do processing here.
 
// Backup previous value.
theCheckBox.Tag = theCheckBox.CheckState;

Open in new window

0
 
LVL 18

Author Comment

by:Priest04
ID: 22859303
Yes, I am aware of storing the value in the class variable, although in my case It would require List<CheckState>, since I am dealing with approx 20 checkboxes. Tag property is used to link with the coresponding textbox.

This is the reason why I asked for a way (alternative), to make the code simpler as possible. This is the reason why I even considered inheriting a CheckBox, and exposing a property PreviousState.

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22859306
If you store your previous value within the control tag property, then there should be no need for a list of check states. Unless of course that was an integral part of your design.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22859312
I have just realized that in my previous snippet I neglected to cast from to CheckState.

If you needed to use the tag property for other purposes, you could create a wrapper data structure.
// Get previous value, or your default when there was no previous.
CheckState previousValue = (CheckState)theCheckBox.Tag ?? CheckState.Checked;
 
// Do processing here.
 
// Backup previous value.
// Each check box has its own unique tag.
theCheckBox.Tag = theCheckBox.CheckState;

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22860295
Here is a way to generate a BeforeChange() Event for your CheckBoxes:
(worked well for me in VB.Net 2005 Express)
Public Class Form1
 
    Private WithEvents mycb As MyCheckBox
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mycb = New MyCheckBox(Me.CheckBox1)
    End Sub
 
    Private Sub mycb_BeforeChange(ByVal sender As System.Windows.Forms.CheckBox, ByVal state As System.Windows.Forms.CheckState) Handles mycb.BeforeChange
        Debug.Print("Before: " & state.ToString)
    End Sub
 
    Private Sub CheckBox1_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckStateChanged
        Debug.Print("After: " & CheckBox1.CheckState.ToString)
    End Sub
 
    Private Class MyCheckBox
        Inherits NativeWindow
 
        Private cb As CheckBox
        Private Const WM_LBUTTONDOWN As Integer = &H201
 
        Public Event BeforeChange(ByVal sender As CheckBox, ByVal state As CheckState)
 
        Public Sub New(ByVal cb As CheckBox)
            Me.cb = cb
            Me.AssignHandle(cb.Handle)
        End Sub
 
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_LBUTTONDOWN
                    RaiseEvent BeforeChange(Me.cb, cb.CheckState)
 
            End Select
            MyBase.WndProc(m)
        End Sub
 
    End Class
 
End Class

Open in new window

0
 
LVL 18

Author Comment

by:Priest04
ID: 22862083
Idle_Mind, already been there, this only covers mouse clicks, value can be changed in code, too.

numberkruncher, as I have previously said, Tag property cannot be used for this purpose.

Creating custom control is what I need to do, but the problem with using private variable to hold the value is, users could be using other events like Click event, whicih is useful when you want to execute some code only when CheckState is changed with UI.

I believe having a 2nd intermediate variable could bring the desired result, but I was also looking for a way to implement BeforeCheckStateChanged event, that could have  argument with Cancel method, to cancel the change.
Still no clue how to do this.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22862110
If you wanted to use the Tag property for multiple purposes you could do something like:

I believe that you can cancel a change with the validating event.
public struct ComboTag
{
   TextBox textBox;
   CheckState prevState;
}
 
...
 
ComboTag tag = (ComboTag)myComboBox.Tag ?? new ComboTag();
tag.prevState = ...

Open in new window

0
 
LVL 18

Author Comment

by:Priest04
ID: 22862721
numberkruncher, you seem to not read what I am saying. I cannot know what (and if) will the Tag property be used for. As I said, I need to provide a CustomCheckBox user control, and Tag property is not an option here.
0
 
LVL 18

Author Comment

by:Priest04
ID: 22935480
The solution:


protected override void OnCheckedChanged(EventArgs e)
{
    mPrevState = mCurrState;
    base.OnCheckedChanged(e);
}
 
protected override void OnCheckStateChanged(EventArgs e)
{
    base.OnCheckStateChanged(e);
    mCurrState = this.CheckState;
}

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22935873
Hi Priest04,

Seems to me that you have implemented elimesika's suggestion made back here:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_23867969.html#22859186
0
 
LVL 18

Author Comment

by:Priest04
ID: 22935905
No, as you can see, there need to be a 3rd variable, that will be used as a bridge.

If I am not mistaken, what he has proposed is a normal situation where you would use a private variable to store some value for later reuse. This cannot be applied in this situation, thats the first I have tried before even posting here. He also suggest that this should be done in CheckedChanged event, and in this situation I must use 2 events, as it can be seen in above example.

If I am mistaken, please correct me, and I will award the points.
0
 
LVL 18

Author Comment

by:Priest04
ID: 22935926
Here is the code to illustrate it better.
CheckState _previousCheckState = CheckState.Unchecked;
CheckState intermediate;
 
public CheckState PreviousCheckState
{
    get { return _previousCheckState; }
}
 
protected override void OnCheckedChanged(EventArgs e)
{
    _previousCheckState = intermediate;
    base.OnCheckedChanged(e);
}
 
protected override void OnCheckStateChanged(EventArgs e)
{
    base.OnCheckStateChanged(e);
    intermediate = this.CheckState;
}

Open in new window

0
 
LVL 18

Author Comment

by:Priest04
ID: 22935975
I appologize, I misunderstood its advice. Altough the advice is 50% accurate, since I needed to use 22 events, because I needed to use OnCheckStateChanged to have indeterminate state, I also needed to emply  if I use OnCheckedChanged because it fires before OnCheckStateChanged, and if some user in the code uses only OnCheckedChanged, it woulg give false result.

Nevertheless. since I overlooked it I will assign all points to elimesika.

Thanks for pointing this out.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22936334
Glad you found a solution...

=)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question