Results 1 to 18 of 18

Thread: Detect Form Control Changes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Unhappy 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.

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Detect Form Control Changes

    What control is it ?
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    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:
    1. Option Explicit
    2. Dim IsText1Changed As Boolean
    3.  
    4. Private Sub Form_Load()
    5.     Text1.Text = "Get value from the UDT"
    6. End Sub
    7.  
    8. Private Sub Text1_Change()
    9.     IsText1Changed = True
    10. End Sub
    11.  
    12. Private Sub cmdApply_Click()
    13.     If IsText1Changed Then
    14.         Text1.Text = "Get value from the UDT"
    15.     End If
    16. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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.

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Detect Form Control Changes

    Are you familiar with building wizards?

    I have project example if you would like it.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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.

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Detect Form Control Changes

    Sure, no problem.
    Attached Files Attached Files

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Detect Form Control Changes

    In VB6, yes - sadly not as trivial as VB.Net though (example attached):

    In your form:
    VB Code:
    1. Implements IChangeHandler
    2. Private oControls() As ControlWrapper
    3.  
    4. Private Sub Form_Load()
    5.     Dim ctl As Control, N As Long
    6.     ReDim oControls(Me.Controls.Count)
    7.     For Each ctl In Me.Controls
    8.         If IsValidControl(ctl) Then
    9.             Set oControls(N) = New ControlWrapper
    10.             Set oControls(N).Callback = Me
    11.             Set oControls(N).Wrapped = ctl
    12.             N = N + 1
    13.         End If
    14.     Next ctl
    15.     If N > 0 Then ReDim Preserve oControls(N - 1)
    16. End Sub
    17.  
    18. Private Sub IChangeHandler_ControlChanged(ByVal oObj As Control)
    19.     ' Your Code
    20.     Debug.Print oObj.Name
    21. End Sub
    22.  
    23. Private Function IsValidControl(ByRef oCtl As Control) As Boolean
    24.     IsValidControl = (TypeOf oCtl Is TextBox) Or (TypeOf oCtl Is ComboBox) Or (TypeOf oCtl Is ListBox) _
    25.                   Or (TypeOf oCtl Is OptionButton) Or (TypeOf oCtl Is CheckBox)
    26. End Function
    In a class module (ControlWrapper):
    VB Code:
    1. ' _Change
    2. Private WithEvents mTextBox As TextBox
    3.  
    4. ' _Click
    5. Private WithEvents mComboBox As ComboBox
    6. Private WithEvents mListBox As ListBox
    7. Private WithEvents mCheckBox As CheckBox
    8. Private WithEvents mOptionButton As OptionButton
    9.  
    10. Private mCallback As IChangeHandler
    11.  
    12. Public Property Set Callback(newObj As IChangeHandler)
    13.     Set mCallback = newObj
    14. End Property
    15.  
    16. Public Property Get Callback() As IChangeHandler
    17.     Set Callback = mCallback
    18. End Property
    19.  
    20. Public Property Set Wrapped(newObj As Control)
    21.     Select Case True
    22.         Case TypeOf newObj Is TextBox
    23.             Set mTextBox = newObj
    24.         Case TypeOf newObj Is ComboBox
    25.             Set mComboBox = newObj
    26.         Case TypeOf newObj Is ListBox
    27.             Set mListBox = newObj
    28.         Case TypeOf newObj Is CheckBox
    29.             Set mCheckBox = newObj
    30.         Case TypeOf newObj Is OptionButton
    31.             Set mOptionButton = newObj
    32.     End Select
    33. End Property
    34.  
    35. Public Property Get Wrapped() As Control
    36.     Set Wrapped = mWrapped
    37. End Property
    38.  
    39. ' Handled Events
    40. Private Sub mTextBox_Change()
    41.     If Not mCallback Is Nothing Then mCallback.ControlChanged mTextBox
    42. End Sub
    43.  
    44. Private Sub mCheckBox_Click()
    45.     If Not mCallback Is Nothing Then mCallback.ControlChanged mCheckBox
    46. End Sub
    47.  
    48. Private Sub mComboBox_Click()
    49.     If Not mCallback Is Nothing Then mCallback.ControlChanged mComboBox
    50. End Sub
    51.  
    52. Private Sub mListBox_Click()
    53.     If Not mCallback Is Nothing Then mCallback.ControlChanged mListBox
    54. End Sub
    55.  
    56. Private Sub mOptionButton_Click()
    57.     If Not mCallback Is Nothing Then mCallback.ControlChanged mOptionButton
    58. End Sub
    and then in the Interface:
    VB Code:
    1. Public Sub ControlChanged(ByVal oObj As Control)
    2. End Sub
    pretty nifty I reckon.

    (props to penagate whose code gave me the idea)

    Attached Files Attached Files

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Exclamation 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?

  18. #18
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width