Priest04
asked on
Get the previous state of CheckBox when clicked?
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
Thanks,
Goran
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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;
ASKER
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.
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.
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.
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.
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;
Here is a way to generate a BeforeChange() Event for your CheckBoxes:
(worked well for me in VB.Net 2005 Express)
(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
ASKER
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.
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.
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.
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 = ...
ASKER
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.
ASKER
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;
}
Hi Priest04,
Seems to me that you have implemented elimesika's suggestion made back here:
https://www.experts-exchange.com/questions/23867969/Get-the-previous-state-of-CheckBox-when-clicked.html#22859186
Seems to me that you have implemented elimesika's suggestion made back here:
https://www.experts-exchange.com/questions/23867969/Get-the-previous-state-of-CheckBox-when-clicked.html#22859186
ASKER
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.
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.
ASKER
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;
}
ASKER
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.
Nevertheless. since I overlooked it I will assign all points to elimesika.
Thanks for pointing this out.
Glad you found a solution...
=)
=)
Open in new window