Solved

Get the previous state of CheckBox when clicked?

Posted on 2008-11-01
17
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:numberkruncher
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 18

Author Comment

by:Priest04
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad you found a solution...

=)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now