Detect Form Control Changes
Can anyone help me?
I am looking for a way to detect when a control has been changed from its initial value, also for it to detect if it is changed back again to that original value. This is to be used to set the enabled property of my Apply Button?
Any help greatly appreciated.
Re: Detect Form Control Changes
Re: Detect Form Control Changes
Also, in order for it to "change back to its original value", that value needs to be maintained somewhere so it knows what to change back to. How are you doing that?
Re: Detect Form Control Changes
The controls used are:-
TextBox's, ComboBox's, CheckBox's, ListBox's, OptionButton's
I have got the initial values in a UDT > 'User.Settings' which are initialy populated from a database.
I just didn't want to really put the relevant code into all the change/click events as there are between 50 - 150 different options, so was wondering if it was possable to have an external sub to do the checking? I have already made as many as i can into Control Arrays to save code.
Re: Detect Form Control Changes
Also this is only one setting screen. There is also one for the program settings, and one for task settings.
Re: Detect Form Control Changes
Even an external sub has to be called from somewhere, at sometime, in order to run.
The only way to detect an immediate change would be to code the control.
If detecting the change as soon as it happens is not necessary for your program, then you could do the check in one sub and call it at some point.
At what point this check sub would be run would have to be determined by you with respect to your application requirements.
Re: Detect Form Control Changes
To check if a control has changed, you need to have some kind of boolean flag.
In the Click/Change event set the flag and in your 'cmdApply' set the default values if needed.
VB Code:
Option Explicit
Dim IsText1Changed As Boolean
Private Sub Form_Load()
Text1.Text = "Get value from the UDT"
End Sub
Private Sub Text1_Change()
IsText1Changed = True
End Sub
Private Sub cmdApply_Click()
If IsText1Changed Then
Text1.Text = "Get value from the UDT"
End If
End Sub
Re: Detect Form Control Changes
Is there no way of calling all of the control_Change/Click Subs from one place? with subclassing or something?
Faling That I might have to go for a wizard style approach so the apply is not there until the end
Re: Detect Form Control Changes
Quote:
Originally Posted by Izzyonstage
Faling That I might have to go for a wizard style approach so the apply is not there until the end
Given what you have said so far, this would seem to be the best approach.
Re: Detect Form Control Changes
oh well, that will teach me for trying to be cleaver. I have been doing this for nearly aweek now. Darn. Time to follow the yellow brick road then.
Thanks for the help.
Re: Detect Form Control Changes
Are you familiar with building wizards?
I have project example if you would like it.
Re: Detect Form Control Changes
sure that would be great. After all Oz is a bit bright for me at this time of the year.
I have got a VERY basic idea for one already but maybe there might be something you have got there to make things a bit easier in the long run.
Oh Well, I was just hoping that i could mimic windows settings where you only need the apply button when there is actually a change to make.
Maybe i should just move to .net I am told that has the ability already built in.
1 Attachment(s)
Re: Detect Form Control Changes
Re: Detect Form Control Changes
Quote:
Originally Posted by Izzyonstage
Is there no way of calling all of the control_Change/Click Subs from one place?
In VB6, no. In VB.Net it's trivial.
1 Attachment(s)
Re: Detect Form Control Changes
In VB6, yes ;) - sadly not as trivial as VB.Net though (example attached):
In your form:
VB Code:
Implements IChangeHandler
Private oControls() As ControlWrapper
Private Sub Form_Load()
Dim ctl As Control, N As Long
ReDim oControls(Me.Controls.Count)
For Each ctl In Me.Controls
If IsValidControl(ctl) Then
Set oControls(N) = New ControlWrapper
Set oControls(N).Callback = Me
Set oControls(N).Wrapped = ctl
N = N + 1
End If
Next ctl
If N > 0 Then ReDim Preserve oControls(N - 1)
End Sub
Private Sub IChangeHandler_ControlChanged(ByVal oObj As Control)
' Your Code
Debug.Print oObj.Name
End Sub
Private Function IsValidControl(ByRef oCtl As Control) As Boolean
IsValidControl = (TypeOf oCtl Is TextBox) Or (TypeOf oCtl Is ComboBox) Or (TypeOf oCtl Is ListBox) _
Or (TypeOf oCtl Is OptionButton) Or (TypeOf oCtl Is CheckBox)
End Function
In a class module (ControlWrapper):
VB Code:
' _Change
Private WithEvents mTextBox As TextBox
' _Click
Private WithEvents mComboBox As ComboBox
Private WithEvents mListBox As ListBox
Private WithEvents mCheckBox As CheckBox
Private WithEvents mOptionButton As OptionButton
Private mCallback As IChangeHandler
Public Property Set Callback(newObj As IChangeHandler)
Set mCallback = newObj
End Property
Public Property Get Callback() As IChangeHandler
Set Callback = mCallback
End Property
Public Property Set Wrapped(newObj As Control)
Select Case True
Case TypeOf newObj Is TextBox
Set mTextBox = newObj
Case TypeOf newObj Is ComboBox
Set mComboBox = newObj
Case TypeOf newObj Is ListBox
Set mListBox = newObj
Case TypeOf newObj Is CheckBox
Set mCheckBox = newObj
Case TypeOf newObj Is OptionButton
Set mOptionButton = newObj
End Select
End Property
Public Property Get Wrapped() As Control
Set Wrapped = mWrapped
End Property
' Handled Events
Private Sub mTextBox_Change()
If Not mCallback Is Nothing Then mCallback.ControlChanged mTextBox
End Sub
Private Sub mCheckBox_Click()
If Not mCallback Is Nothing Then mCallback.ControlChanged mCheckBox
End Sub
Private Sub mComboBox_Click()
If Not mCallback Is Nothing Then mCallback.ControlChanged mComboBox
End Sub
Private Sub mListBox_Click()
If Not mCallback Is Nothing Then mCallback.ControlChanged mListBox
End Sub
Private Sub mOptionButton_Click()
If Not mCallback Is Nothing Then mCallback.ControlChanged mOptionButton
End Sub
and then in the Interface:
VB Code:
Public Sub ControlChanged(ByVal oObj As Control)
End Sub
pretty nifty I reckon.
(props to penagate whose code gave me the idea)
Re: Detect Form Control Changes
Brilliant bushmobile, you are a life saver. gonna try it now. will post back my results
I knew there would be some way to do it
Re: Detect Form Control Changes
Hi Again, All went rather smoothly. Brilliant. I should have thought of that myself, not too long ago i use a similar method to mimic .net base calsses and derived classes.
Just one thing though, If the controls are part of a control array it don't work properly, i get an error saying the event is not set correctly. I am presuming that this is because for a control array there is the (Index as Integer) on the event calls as well.
How should i go about adding this availability for the controls aswell?
Re: Detect Form Control Changes
ahh, I knew I was missing something.
I don't know how to include control arrays in this too (and I haven't got the time to do the research to find out). I guess that's why they're no control arrays in .Net