Reputation: 3
I have a vb.net
web application with multiple rows of controls. Each row has a textbox and two buttons, one to save the entered text and one to edit any existing text (textbox is disabled elsewhere if text is inside).
Clicking edit will disable all controls and enable the relevant save button and textbox with text you want to edit. I'm trying to re-enable these controls that were disabled and I'm using some code such as this after a successful save:
If String.IsNullOrEmpty(txtbox2.Text) = True Then
txtbox2.Enabled = True
btnSave2.Enabled = True
Else
btnEdit2.Enabled = True
End If
'enabling textbox and save button if textbox is empty
'or enabling edit button if text exists
If String.IsNullOrEmpty(txtbox3.Text) = True Then
txtbox3.Enabled = True
btnSave3.Enabled = True
Else
btnEdit3.Enabled = True
End If
These little if blocks are repeated for each row other than the one being edited (so 5 times total per sub). I've tried using for each loops to accomplish the same thing, but I don't know how or if there's a way to loop through, determine if a textbox has text and if so, enable edit button on that row, and if not re-enable that textbox as well as the save button on that same row.
I can easily get all the textboxes to enable or disable as desired with something like:
Dim ReactivateText() = {txtbox2, txtbox3, txtbox4, txtbox5, txtbox6}
For Each thing In ReactivateText
If TypeOf thing Is TextBox Then
Dim othertext As TextBox = thing
If String.IsNullOrEmpty(othertext.Text) = True Then
othertext.Enabled = True
End If
End If
Next
But even with another array variable with button controls or by adding the buttons to the variable above, I'm still having issues getting the buttons to enable or disable appropriately.
I'm still relatively new to vb.net
so I'm curious if anyone has any advice on a more elegant, programmatic way to accomplish this? The simple, original code above does actually work as intended though, as far as I can tell, so maybe I should leave well enough alone. But this one bit of code kinda sticks out to me as being overly simplistic compared to the rest of my application and I have to imagine there's a better way to accomplish this.
Upvotes: 0
Views: 314
Reputation: 6131
You do not need the conditional statements since you're assigning a Boolean value, just set it equal to(or not equal to) the condition, like this:
txtBox2.Enabled = String.IsNullOrEmpty(txtbox2.Text)
btnSave2.Enabled = txtBox2.Enabled
btnEdit2.Enabled = Not txtBox2.Enabled
In terms of not having to do this for each control, set up a collection to store the controls and then iterate through them like this:
Dim textBoxes() As TextBox = {txtBox2, txtBox3, txtBox4, txtBox5, txtBox6}
Dim saveBtns() As Button = {btnSave2, btnSave3, btnSave4, btnSave5, btnSave6}
Dim editBtns() As Button = {btnEdit2, btnEdit3, btnEdit4, btnEdit5, btnEdit6}
For index As Integer = 0 To textBoxes.Length - 1
textBoxes(index).Enabled = String.IsNullOrEmpty(textBoxes(index).Text)
saveBtns(index).Enabled = textBoxes(index).Enabled
editBtns(index).Enabled = Not textBoxes(index).Enabled
Next
Upvotes: 1