Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Loop through and perform certain functions based on conditions?

Hello everyone,
I have the following code below. basically, I have a listcheckbox and a listbox. If Item 1 is selected, then I have it set to save to a file, if item 2 is selected, then it saves to a different file, and if both items are selected then its saved to another seperate file.

This works when I first run the program, if I select "item 1" and then hit my cmd button, it will save to that first file. If I change the checkbox to "item 2" and hit the cmd button, it still just save to the first file, instead of the file that is supposed to be saved when "item 2" is saved.

I am figuring I need some kind of loop in here, but I am not sure how to set it up where it loops through, and then saves to different files depending on what was selected.

Thanks!

        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:1 - 5201 Under $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")
            Dim fullNameNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode.RemoveAll()
            ReportClassFilterNode.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName As XmlNode = xDoc.CreateElement("FullName")
                newName.InnerText = item.ToString()
                ReportEntityFilterNode.AppendChild(newName)
            Next

            Dim newName1 As XmlNode = xDoc.CreateElement("FullName")
            newName1.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode.AppendChild(newName1)


            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")


        End If


        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
            Dim fullNameNode1 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode1.RemoveAll()
            ReportClassFilterNode1.RemoveAll()


            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName2 As XmlNode = xDoc.CreateElement("FullName")
                newName2.InnerText = item.ToString()
                ReportEntityFilterNode1.AppendChild(newName2)
            Next


            Dim newName3 As XmlNode = xDoc.CreateElement("FullName")
            newName3.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode1.AppendChild(newName3)

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")

        End If

        If wagesCheckBox.CheckedItems.Count() = 2 Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
            Dim fullNameNode2 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode2 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            ReportEntityFilterNode2.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName4 As XmlNode = xDoc.CreateElement("FullName")
                newName4.InnerText = item.ToString()
                ReportEntityFilterNode2.AppendChild(newName4)
            Next



            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")

        End If
0
JasonWinn
Asked:
JasonWinn
  • 3
  • 2
1 Solution
 
Shakti109Commented:

There are several possibilities with this one.

First, when you do something like :

 If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20")

This is CasE SENsITIVE....As in :

If your listbox contains :  Labor costs-job Related:2 - 5205  Over $20
It will never evaluate to true as it is case sensitive and the two do not match.

Check your listbox contents very carefully (cut-n-paste if you are'nt sure, so they match).

Does the routine fail if you select both elements?
0
 
JasonWinnAuthor Commented:
Hi Shakti,
Everything has been cut and pasted and works.

It does not fail, if I select one, then run it, and it does not fail if I select  both and run it.

If I load the program, and select "A" it does not fail
If I load the program, and select "B" it does not fail
If I load the program, and select "A" + "B" it does not fail

it fails when:
1) Load Program and Select "A"
2) hit Cmd button (still working at this point)
3) then i clear "A" (after running the report)
4) Select "B" and run the report (it runs the report as if "A" is selected, and not "B") <--- does not work at this point

the problem is pin-pointed at step 4. It run's the report on whatever was previously selected, regardless of what is currently selected when I run the report the 2nd time.
0
 
SanclerCommented:
I'm not sure, but it looks like there may be some confusion between "selected" and "checked".  The first two "if" statements operate with a check on what is the .SelectedItem.  The last "if" statement does a test on .CheckedItems.  The description at the start of your post seems to refer interchangeably to "selecting" and "changing the checkbox".

Roger
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JasonWinnAuthor Commented:
Hey guys,
I have figured out the problem, and cleared up a few things. I have changed it to a listbox so its not so confusing as well.

What is happening is that:

If Item 1 is selected then save itema.xml (works)
if item 2 is selected then save itemb.xml (works)
if item 1 + 2 is selected then save itemboth.xml (sorta works, it saves itemboth.xml and itema.xml) I need it to save just itemboth.xml and ignore the other subs.

The problem is that both item's are selected, and that 3rd If/Else works, but since "Item a" is still selected (since both items are selected), it also saves itema.xml. Is there a way to make it just save itemboth.xml and ignore the if/else for item a and item b if both are selected.

Thanks
0
 
JasonWinnAuthor Commented:
       Dim xDoc As XmlDocument
        xDoc = New XmlDocument

        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:1 - 5201 Under $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")
            Dim fullNameNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode.RemoveAll()
            ReportClassFilterNode.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName As XmlNode = xDoc.CreateElement("FullName")
                newName.InnerText = item.ToString()
                ReportEntityFilterNode.AppendChild(newName)
            Next

            Dim newName1 As XmlNode = xDoc.CreateElement("FullName")
            newName1.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode.AppendChild(newName1)


            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")


        End If


        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
            Dim fullNameNode1 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode1.RemoveAll()
            ReportClassFilterNode1.RemoveAll()


            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName2 As XmlNode = xDoc.CreateElement("FullName")
                newName2.InnerText = item.ToString()
                ReportEntityFilterNode1.AppendChild(newName2)
            Next


            Dim newName3 As XmlNode = xDoc.CreateElement("FullName")
            newName3.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode1.AppendChild(newName3)

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")

        End If

        If wagesCheckBox.SelectedItems.Count = 2 Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
            Dim fullNameNode2 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode2 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            ReportEntityFilterNode2.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName4 As XmlNode = xDoc.CreateElement("FullName")
                newName4.InnerText = item.ToString()
                ReportEntityFilterNode2.AppendChild(newName4)
            Next



            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")

        End If
0
 
Shakti109Commented:

The most simple way is to re-order your if-then selections, and add in an exit sub after each. LIKE :

        If wagesCheckBox.SelectedItems.Count = 2 Then

           xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
            Dim fullNameNode2 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode2 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            ReportEntityFilterNode2.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName4 As XmlNode = xDoc.CreateElement("FullName")
                newName4.InnerText = item.ToString()
                ReportEntityFilterNode2.AppendChild(newName4)
            Next

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
        exit sub
        End If

       If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
            Dim fullNameNode1 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode1.RemoveAll()
            ReportClassFilterNode1.RemoveAll()


            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName2 As XmlNode = xDoc.CreateElement("FullName")
                newName2.InnerText = item.ToString()
                ReportEntityFilterNode1.AppendChild(newName2)
            Next


            Dim newName3 As XmlNode = xDoc.CreateElement("FullName")
            newName3.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode1.AppendChild(newName3)

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
        exit sub
        End If


        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:1 - 5201 Under $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")
            Dim fullNameNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode.RemoveAll()
            ReportClassFilterNode.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName As XmlNode = xDoc.CreateElement("FullName")
                newName.InnerText = item.ToString()
                ReportEntityFilterNode.AppendChild(newName)
            Next

            Dim newName1 As XmlNode = xDoc.CreateElement("FullName")
            newName1.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode.AppendChild(newName1)


            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")

        exit sub
        End If

There are other ways to do this, rolling everything into one if-then-else condition set for example (in which you will still have to re-order the conditions such that the "combined" condition is first), but simply adding the "combined" condition as the first check, and exiting the sub they are part of is the most direct.

If you have other work to do AFTER the checks in the same subroutine, then you can :
1) move the if-then checks to a function, and exit the function after each call.
2) move the if-then checks to another subroutine and leave it the way it is.
3) change the "exit sub" to a goto marker within the same subroutine AFTER all of the checks (as in goto ChecksCompleted, etc).
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now