Enable Button When One Of Many Controls Is Changed

  • 12 years ago

    I have a Textbox, CheckBox, NumericUpDown, and Button on my form.

    The button's Enabled Property is set to False and this button is a Save Button.

    The data for the textbox, checkbox, and numericupdown is loaded from a textfile.

     My question is this....

     After the data is read from the file and the textbox and numericupdown has its data, and the checkbox is either set to True or False, can I group these 3 controls into a collection or some other grouping and when any one of them is changed, the save button's Enabled property is set to True.

  • 12 years ago

    No, there is no "official" way to do it. You can handle all its events by calling some save sub, which will button.PerformClick, or, if you plan to behave such manner in wider range in project, you can easily create some class, which, for example inherited from arraylist, will for every Overrides Sub Add(control) assign handler (e.g. AddHandler control.TextChanged, AddressOf Me.TextBoxes_TextChanged) depending on control.gettype.lowestbasetype (or just type, if you are not creating some extra controls with inheriting). This inherited collection will then have Public Event ValueChanged(sender as object, field as control) which will be Raised in all such event handlers.

    Pseudo sample:

    Class evtCollector

    Inherits ArrayList

    Public Event ValueChanged(Sender, Field)

    Private Sub TextBoxes_TextChanged(sender As Object, args As ...) ---HANDLER
     RaiseEvent ValueChanged(Me, sender)
    End Sub

    ...another HANDLERS for different controls you use

    Public Overrides Function Add(c as control) As Integer
    If c.GetType.Equals(TextBox)
     AddHandler c.TextChanged, AddressOf Me.TextBoxes_TextChanged
    Else
     AddHandler c.ValueChanged, AddressOf Me.NumericUDs_ValueChanged
    ..
    MyBase.Add(c)
    End Function

     

    After such implementation you simply create Withevents group1, group2 ... As evtCollector
    and in sub new of form you fill them... and handle its event

  • 12 years ago

    figured this one out as soon as you pointed out the handlers to me

    Private SaveControlData ( )

  • 12 years ago

    figured this one out as soon as you pointed out the handlers to me

    Private SaveControlData ( ) 

  • 12 years ago

    figured this one out as soon as you pointed out the handlers to me

    Private SaveControlData ( ) Handles

  • 12 years ago

    figured this one out as soon as you pointed out the handlers to me

    Private SaveControlData ( ) Handles Textbox1.TextChanged, Checkbox1.CheckState, _

    NumericUpDown.ValueChanged

    Button1.Enabled = True

     End Sub

  • 12 years ago

    poetic solutionStick out tongue

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill