Solved

Loop through and perform certain functions based on conditions?

Posted on 2006-07-06
6
232 Views
Last Modified: 2010-04-23
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
Comment
Question by:JasonWinn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Shakti109
ID: 17052512

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
 
LVL 3

Author Comment

by:JasonWinn
ID: 17052563
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
 
LVL 34

Expert Comment

by:Sancler
ID: 17053216
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:JasonWinn
ID: 17053823
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
 
LVL 3

Author Comment

by:JasonWinn
ID: 17053828
       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
 
LVL 8

Accepted Solution

by:
Shakti109 earned 500 total points
ID: 17054581

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question