activating button based on multiple fields

This is one of those questions where there would be a number of ways to achieve what I want. But I am also aware that there is probably a correct way. What I have is a few inputs that need to have a value before the button is activated. I could on change for each element required check if all other elements also have a value and if so then activate the button. But this would have to be done on each element and seems like overkill. I have also considered using a timer that does the checking but feels a bit like a work around to me.
Any suggestions on the best way to approach this?

What about creating a method which checks if the button should be activated, and activates it if it should be and disables it if it shouldn’t, and then you call that method in the change event of each of the elements?

Thanks Jason. I was thinking of that. But making it more generic such that I can pass in the fields to check and the button to activate.

Here’s a rough idea. Make a class, AutoValidator, to manage the set of controls and interaction and a TextField subclass to work with it. Then in the Window add an AutoValidator property and load it up with your controls in Open (Where you’ve changed your TextField supers).

This can be generalized, like providing a custom validation event, different target controls, etc. Also, this example code creates a cyclic link (AutoValidator references a AVTextField which references back) so, you’ll want to fix AVTextField to use WeakRef.

[code]Class AutoValidator

Property theButton As PushButton
Property theTFs() As AVTextField

Sub Constructor(targetButton As PushButton, ParamArray tfs As AVTextField)
theButton = targetButton
theTFs = tfs
for i As integer = 0 to theTFs.Ubound
End Sub

Sub doValidation()
dim isOn As boolean = true
for i As integer = 0 to theTFs.Ubound
if theTFs(i).Text.Trim = “” then
isOn = false
exit for i
if theButton.Enabled <> isOn then theButton.Enabled = isOn
End Sub

End Class

Class AVTextField Inherits TextField

Property refValidator

Sub linkValidator(ref As AutoValidator)
refValidator = ref
End Sub

Sub TextChange() //Event
End Sub

End Class

In Window…

Property validSet1

Sub Open()
validSet1 = new AutoValidator(PushButton1, TF1, TF2, TF3)
End Sub[/code]

You could probably also make this work without a TextField subclass, instead AutoValidator would use AddHandler on the event.

Personally, I would take the other approach - that of having the button activated as soon as any of the needed fields have values and have the button’s action event check to see if all of the required fields have values. If any are missing values, I’d put up a dialog telling the user which fields still need values. That way you only need need to validate entries when the button is pushed.

The way you are approaching it, the user might be confused why the button isn’t activated, not realizing that the field(s) need to have values.

Anyway, that’s my opinion. :slight_smile:

Remember encountering a web site that you’d input what you think you need to & press “send” or “submit” then it comes back & says “oh this field is required” ?
So you go back fill that in hit send and repeat the process
Thats “block mode” - all the input is sent at once & validated all at once
Old mainframe programs operated like that too

Now better ones don’t let you press send until all the required field are present. Good desktop programs do that as well.

Its preferable to

  1. highlight required fields somehow
  2. not enable the “input” or “submit” buttons until such time as the input is all present

Imagine if the Save dialog had the Save button enabled even when there is no file name entered.
You’d press save, with an empty name, and then it would come back and say “Please enter a file name”
Instead just not having the button enabled already indicates there’s something missing.
Now there may be additional measures to say what specifically is wrong - but not letting people make mistakes avoids mistakes from the outset.

Not letting people make mistakes is the best way to prevent them and to avoid doing all kinds of validation code