Solved

Loop through and perform certain functions based on conditions?

Posted on 2006-07-06
6
219 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now