?
Solved

Get the previous state of CheckBox when clicked?

Posted on 2008-11-01
17
Medium Priority
?
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +2
17 Comments
 
LVL 19

Accepted Solution

by:
elimesika earned 2000 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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 86

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 86

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 86

Expert Comment

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

=)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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