Wil
Wil

Reputation: 10604

Is there a better way to do disable on checkbox?

I am working on an application that has a lot of checkboxes that start off unchecked, and each one has an associated textbox which is disabled until the checkbox is checked.

I am going through and currently one by one doing an "if checked, enable, if uncheced, disable" - however it is taking forever!

Is there a quicker way / method I should be using that will speed up this process?

I did not state the language as I am not sure it matters - I am programming in VB, however I just want a shove in the right (language independent) direction and I can research further from there.

Upvotes: 1

Views: 3353

Answers (7)

Dustin Hodges
Dustin Hodges

Reputation: 4195

Here is another try at an answer that I think is closer to what the OP wants. Create a check box/text box manager control to map check boxes to text boxes similar to what this telerik radajaxmanager control does. Then you register a check box with the manager and then register for each text box you want to enable/disable.

Object model would something like this:

Public Class CTManager
    CheckToTextMappingCollection Mappings
End Class

Public Class CheckToTextMappingCollection
    Inherits List(Of CheckToTextMapping)
End Class

Public Class CheckToTextMapping
    Public Property CheckBox As CheckBox
    Public Property IEnumerable<TextBox> AffectedTextBoxes

    Public Sub New(ByVal CheckBox As CheckBox)
        Me.CheckBox = CheckBox

        AddHandler CheckBox.CheckedChanged, AddressOf CheckBoxChanged
    End Sub

    Public Sub AddTextBox(ByVal TextBox As TextBox)
        AffectedTextBoxes.Add(TextBox)
    End Sub

    Public Sub CheckBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each tb As TextBox in AffectedTextBoxes
            tb.Enabled = CheckBox.Checked
        Next
    End Sub
End Class

**** Untested ****    

Doing it this way makes it more extensible (different rules for enabling/disabling could go in the event handler etc.) and separates the logic for handling this nicely into its own object.

Upvotes: 0

Jared Roberts
Jared Roberts

Reputation: 153

I would add a panel for each radio button textbox combo you have on your page. In the panel make sure that the checkbox and associated textbox are contained within it. Then in your .vb file of the form add the following. Make sure you add a handler for each checkbox's checkchanged event.

Private Sub chk_CheckedChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles _
    chk1.CheckedChanged, chk2.CheckedChanged
    Dim chk As CheckBox = CType(sender, CheckBox)
    For Each ctrl As Control In chk.Parent.Controls
        If ctrl.Name <> chk.Name AndAlso ctrl.GetType() Is GetType(TextBox) Then
            CType(ctrl, TextBox).Enabled = chk.Checked
        End If
    Next
End Sub

Upvotes: 0

Dustin Hodges
Dustin Hodges

Reputation: 4195

Why not just create a control that has a checkbox and textbox and handles the enabling/disabling of the textbox within that control?

Upvotes: 5

SLaks
SLaks

Reputation: 887415

You can make a function that takes a checkbox and a textbox and handles the checkbox's event to disable the textbox.

You can then simply call the function once for each pair.

For example: (in C#)

void BindCheckbox(CheckBox checkbox, Control targetControl) {
    targetControl.Enabled = checbkox.checked;
    checkbox.CheckedChanged += delegate { targetControl.Enabled = checbkox.checked; };
}

BindCheckbox(someCheckbox, someTextbox);

Upvotes: 6

Scott Chamberlain
Scott Chamberlain

Reputation: 127553

One method I have used for a similar issue is use the Name Property of the checkbox, name your text field something similar, and they can reference each other.

C# example

//assume the checkboxs are named cbxfield1, cbxfield2, ect. and the text box is named txtfield1, txtfield2, ect.

public void CheckBoxChecked(object sender, EventArgs e)
{
     var checkBox = (CheckBox)sender;
     var name = checkNox.Name.Substring(3);
     var textBox = (TextBox)containerWithTextFields.Controls["txt"+name];
     textBox.Enabled = checkBox.Checked;
}

Upvotes: 0

weiy
weiy

Reputation: 1177

You could have all the checkboxes call one single event handler, passing along some identifier. And in the event handler, disable/enable the associated textbox based on that identifier. (I'm assuming each checkbox/textbox pair are somehow named similarly)

Upvotes: 0

Robert
Robert

Reputation: 21388

If you can include them in the same containing element, such as <td> you do a generalized onchange for the checkbox. If not, just tweak the DOM transversal of the below code to find the correct textbox.

jQuery

$('input:checkbox').change(function() {
   //$(this).siblings('yourTextbox').attr('disabled',!this.checked);
});

Here's a fiddle, for demonstration purposes: http://jsfiddle.net/robert/WTAw8/

Edit I think I completely misunderstood the language you were looking for.... this is jQuery/Javascript based... so ignore this if you're not doing web based.

Upvotes: 0

Related Questions