0

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.

Community
  • 1
  • 1
  • I am not sure I am following your question. Yes it takes time to code all of that and set it, but you'll only have it once- to me it seems like a just get it done type thing. I guess you could iterate through all the different controls and their child elements, get the type of the control and then set it accordingly, but the function to do that would take some time as well. It would be more modular, as you could crawl through them all. Are you talking about something like that? – Nyra Nov 28 '14 at 20:46
  • what about making the button creating a new form and closing the current one? – Josh Part Nov 28 '14 at 20:49
  • @alykins 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. – вʀaᴎᴅᴏƞ вєнᴎєƞ Nov 28 '14 at 20:50
  • @JoshPart because I was told "No." when I asked. – вʀaᴎᴅᴏƞ вєнᴎєƞ Nov 28 '14 at 20:54
  • 2
    `hundreds, possibly thousands of controls on the form` <-- perfect time to reconsider your design. – Victor Zakharov Nov 28 '14 at 20:54
  • @Neolisk I never said the design was the greatest, but that's out of my hands. – вʀaᴎᴅᴏƞ вєнᴎєƞ Nov 28 '14 at 20:57
  • My point was not to blame your design. Rather to suggest the right way to handle this. If you keep this design, whatever solution you code from this point, will 99% likely need to be completely rewritten later. Unless of course you are planning to leave the company shortly, researching how far crappy code can get you, doing a home project for fun etc. – Victor Zakharov Nov 28 '14 at 21:53

1 Answers1

0

Two ways to handle this are:

  1. Like you have, just "manually" reset each control that need to be initialized, or

  2. Reset the form completely and populate it with the items that don't need to be reset.

I would generally prefer the first method, because it seems simpler and would likely be less prone to errors in future maintenance.

xpda
  • 15,585
  • 8
  • 51
  • 82
  • I went with option 1, but I'm wondering if there was a better way than the ones I tried. – вʀaᴎᴅᴏƞ вєнᴎєƞ Nov 28 '14 at 21:28
  • It would depend on the specific situation. In this scenario, I would tend toward the simplest to maintain. There's really not something you can do with 3 or 4 lines. My guess (with what I see) is that option 1 would be better. – xpda Nov 28 '14 at 21:38