I am currently working on a Project where one of the subForms has a tab control with about 7 tabs in it. in each of these tabs there are a number of SplitContainers
, TableLayoutPanels
, and FlowLayoutPanels
. Interspersed there are a multitude of TextBoxes
, DateTimePickers
, and CheckBoxes
. I am looking to add a single button that has one job, "Reset the form... but not completely."
I'm trying to figure out what would be the most efficient way to do this. So I've put together what I've tried with so far and my simplified coding examples. I've only changed the content of the code below, not the structure of how it works. There are just way to many elements on the form to list them here in a friendly format.
My first thought was to just set all the values individually like the following, but that's really cumbersome to code and it just adds fluff with all the repeated stuff:
Private Sub resetBtn_Click(sender As Object, e As EventArgs) Handles resetBtn.Click
fooBox.Text = String.Empty
barBox.Text = String.Empty
otherBox.Text = String.Empty
hammerBox.Text = "Hammer Time!"
fooPicker.Value = DateTime.Now
barPicker.Value = DateTime.Now
otherPicker.Value = DateTime.Now
somwherePicker.value = DateTime.Now.Date.AddHours(17) ' 5'o clock somewhere...
fooCheck.Checked = True
barCheck.Checked = True
otherCheck.Checked = True
soupCheck.Checked = False ' No soup for you!
End Sub
I could do a recursive For loop that would find each type of item and set the values excluding the items that need a default value, but that seems like a lot of extra work during execution mostly because there are hundreds, possibly thousands of controls on the form that have generated members and even more that don't. Here is how I had my for loop:
Private Sub resetBtn_Click(sender As Object, e As EventArgs) Handles resetBtn.Click
Dim excludes As New Dictionary(Of String, Object) From {{"hammerBox", "Hammer Time!"}, {" somwherePicker", DateTime.Now.Date.AddHours(17)}, {"soupCheck", False}}
SearchSubControls(Me, excludes)
End Sub
Private Sub SearchSubControls(parentCtrl As Object, exclude As Dictionary(Of String, Object))
Try
For Each ctrl As Object In parentCtrl.Controls
If exclude.ContainsKey(ctrl.Name) Then
Select Case True
Case TypeOf ctrl Is TextBox
ctrl.Text = exclude(ctrl.Name)
Case TypeOf ctrl Is DateTimePicker
ctrl.Value = exclude(ctrl.Name)
Case TypeOf ctrl Is CheckBox
ctrl.Checked = exclude(ctrl.Name)
End Select
Else
Select Case True
Case TypeOf ctrl Is TextBox
ctrl.Text = String.Empty
Case TypeOf ctrl Is DateTimePicker
ctrl.Value = DateTime.Now
Case TypeOf ctrl Is CheckBox
ctrl.Checked = True
End Select
End If
If ctrl.Controls.Count <> 0 Then
SearchSubControls(ctrl, exclude)
End If
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
So I was doing some searching and I found this question for Python. I was wondering if there was a similar equivelent in VB? I haven't been able to find anything so far. If it doesn't exist could it be possible to implement or would things like the compiler need to be modified? I'm really hoping it could be done with something as simple as this:
Private Sub resetBtn_Click(sender As Object, e As EventArgs) Handles resetBtn.Click
fooBox.Text, barBox.Text, otherBox.Text = String.Empty
fooPicker.Value, barPicker.Value, otherPicker.Value = DateTime.Now
fooCheck.Checked, barCheck.Checked, otherCheck.Checked = True
hammerBox.text ="Hammer Time!"
somwherePicker.value = DateTime.Now.Date.AddHours(17)
soupCheck.Checked = False
End Sub
Or is there a better method with how to do this that I'm not aware of?
Side Note: C# code is cool as long as there is a VB counterpart.
EDIT
This is from the comments but thought I'd edit the question. I've already completed the task, I just typed it all out by hand. I actually just got the names of all the items I needed from the designer file hammered them all out. That seemed to be the least error prone and most efficient option I could come up with and just about anyone after me can look at it and go, "oh, I know what this is doing". This question is more of a followup to see what type of options are out there.