4 Replies - 416 Views - Last Post: 14 November 2012 - 11:28 AM Rate Topic: -----

#1 RoyLittle0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Resetting cascading List boxes on change from preceding

Posted 14 November 2012 - 04:43 AM

Hi,

I have 8 List boxes that update from an xml file, at the moment they populate when they are clicked and the following List boxes update (this is a bit like a family tree) so i can populate all list boxes by clicking on them individually, but if i change one at the top of the tree then only the next one down update what i need is when it is clicked all following boxes go blank out

Example, at the moment all list boxes are populated after being clicked on, if i then click on List box 2, this will update list box 3 only and 4 to 8 remain the same, what i need is for 4 to 8 to go blank.


Solution 1, List Box
Solution 2, List Box
Solution 3, List Box
Solution 4, List Box
Solution 5, List Box
Solution 6, List Box
Solution 7, List Box
Solution 8, List Box



    'Private Sub - for Solution 2 list box, select Name from the choices in Root 2 Section 2 of the xml, based on the
    'value selected in Section 1 of the xml, each Solution boxes value changes based on the previouse Solution boxes value
    Private Sub Solution1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution1.Click

        Dim Root2 =
        (
            From a In doc...<Section1>
            Where a.@Name = Solution1.Text
            From l In a.<Root2>.<Section2>
            Select l.@Name
        ).ToArray

        Solution2.DataSource = Root2

    End Sub

    'Private Sub - for Solution 3 list box
    Private Sub Solution2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution2.Click
        Dim Root3 =
        (
            From a In doc...<Section1>
            Where a.@Name = Solution1.Text
            From l In a.<Root2>.<Section2>
            Where l.@Name = Solution2.Text
            From b In l...<Section3>
            Select b.@Name
        ).ToArray

        Solution3.DataSource = Root3

    End Sub
    'Private Sub - for Solution 4 list box
    Private Sub Solution3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution3.Click
        Dim Root4 =
        (
            From c In doc...<Section2>
            Where c.@Name = Solution2.Text
            From l In c.<Root3>.<Section3>
            Where l.@Name = Solution3.Text
            From d In l...<Section4>
            Select d.@Name
        ).ToArray

        Solution4.DataSource = Root4

    End Sub
    'Private Sub - for Solution 5 list box
    Private Sub Solution4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution4.Click
        Dim Root5 =
        (
            From f In doc...<Section3>
            Where f.@Name = Solution3.Text
            From l In f.<Root4>.<Section4>
            Where l.@Name = Solution4.Text
            From g In l...<Section5>
            Select g.@Name
        ).ToArray

        Solution5.DataSource = Root5

    End Sub
    'Private Sub - for Solution 6 list box
    Private Sub Solution5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution5.Click
        Dim Root6 =
        (
            From h In doc...<Section4>
            Where h.@Name = Solution4.Text
            From l In h.<Root5>.<Section5>
            Where l.@Name = Solution5.Text
            From j In l...<Section6>
            Select j.@Name
        ).ToArray

        Solution6.DataSource = Root6

    End Sub
    'Private Sub - for Solution 7 list box
    Private Sub Solution6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution6.Click
        Dim Root7 =
        (
            From k In doc...<Section5>
            Where k.@Name = Solution5.Text
            From l In k.<Root6>.<Section6>
            Where l.@Name = Solution6.Text
            From m In l...<Section7>
            Select m.@Name
        ).ToArray

        Solution7.DataSource = Root7

    End Sub
    'Private Sub - for Solution 8 list box
    Private Sub Solution7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution7.Click
        Dim Root8 =
        (
            From n In doc...<Section6>
            Where n.@Name = Solution6.Text
            From l In n.<Root7>.<Section7>
            Where l.@Name = Solution7.Text
            From o In l...<Section8>
            Select o.@Name
        ).ToArray

        Solution8.DataSource = Root8

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Resetting cascading List boxes on change from preceding

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: Resetting cascading List boxes on change from preceding

Posted 14 November 2012 - 09:07 AM

You're really fighting yourself with all this repetitive code. Its just pure brute force coding. If you had 100 listboxes would you copy/paste the handlers 100 times and make changes to make each unique?

Most of this repition is caused by using lots of individual variables (Solution1, Solution2, root1, root2) and so on.

If you instead used collections you could loop through them in a smarter and more dynamic way.

When the third listbox in the List<ListBox> changes then you loop through from that point to the end resetting. This way you only need a couple methods that are more dynamic and this 100+ lines of code could become 25 or less.

Keep in mind that you can have all your listbox's .Click event go to a single handler and still know which listbox was clicked because it is passed into the method as the parameter sender. sender *is* the listbox that was clicked on.
Was This Post Helpful? 0
  • +
  • -

#3 RoyLittle0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Resetting cascading List boxes on change from preceding

Posted 14 November 2012 - 09:20 AM

Thanks for the reply, i am very new to coding as you can probably see, in fact i only started learning this 7 days ago.

I have trawled the internet looking for code examples of what i am trying to do and this code was the closes to what i needed, so to be honest i don't know any better, can you explain what you mean, obviously is simple persons terms, i am very keen to learn this, so any help would be appreciated.

Any sample code would also be appreciated.

What i am trying to achieve is a flow chart or family tree type of thing where every question has a yes or no answer and that answer in-turn has two answers yes or no, but the answers cant be hard coded and would probably need to be rewritten many time for different applications, hence why i am using an xml file

View PosttlhIn`toq, on 14 November 2012 - 09:07 AM, said:

You're really fighting yourself with all this repetitive code. Its just pure brute force coding. If you had 100 listboxes would you copy/paste the handlers 100 times and make changes to make each unique?

Most of this repition is caused by using lots of individual variables (Solution1, Solution2, root1, root2) and so on.

If you instead used collections you could loop through them in a smarter and more dynamic way.

When the third listbox in the List<ListBox> changes then you loop through from that point to the end resetting. This way you only need a couple methods that are more dynamic and this 100+ lines of code could become 25 or less.

Keep in mind that you can have all your listbox's .Click event go to a single handler and still know which listbox was clicked because it is passed into the method as the parameter sender. sender *is* the listbox that was clicked on.

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: Resetting cascading List boxes on change from preceding

Posted 14 November 2012 - 09:46 AM

Please don't be offended when I say I'm not investing a lot of time trying to teach you how to build your specific application when you haven't invested any time in actually learning C#.

We have lots of tutorials here on first learning the language. You can't just read a weeks worth of material then think you can start architecting solutions.





My standard beginner resources post


You have to take some responsibility for your own education. There are millions of noobies and only a hundred core members here. We simply can't hand-hold every rookie through their every question. We can't maintain thousands of threads for "What is a constructor?", "What does 'variable' mean?" and so on.

That's why we have a tutorials section and a learning C# series of articles.

I strongly urge you to not look at this site as your home for a free teacher/tutor to hold your hand through every introductory question because you don't want to buy a book. We don't do that. We help people advance their coding skills and help with code they have already written but are having trouble with. We don't do free program architecture/logic-design for those that can't figure it out (IE: We don't do your thinking for you.) But you have give us something to work with. At least read a couple books and a few on-line articles.

First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

Then later you can start architecting your own simple stuff. Build a calculator. Build a DVD library program. Etc. Stuff that doesn't involve the complexity of a game. Then move up to games.



There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Oct 2012
Spoiler



Intermediate:
OOP design patterns


Microsoft Events (webcasts and podcasts)
The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Separating data from GUI - PLUS - serializing the data to XML
WPF version (WPF-MVVM data binding)
Passing values between forms/classes
Decouple your multi-threaded work from the GUI so forms don't hang

Working with environmental variables
'Why do we use delegates?' thread
And everyone always wants to connect to a database, right out of the gate so Database tutorials right here on DIC

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Everyone:
Debugging tutorial
Debugging tips
Debugging in detail
Great debugging tips
It still doesn't work, article

I urge you to work through the C# learning series here on DIC, and to work a self-teaching book from cover to cover before you even think about designing your own applications.
Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5 Has many, many great, real-world tips that I use all the time.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio
Free editions of Visual Studio 2010
Student editions of Visual Studio


Windows Presentation Foundation:
Create Animations Programmatically

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, please and so on like this guy:

    Spoiler


[/spoiler]

This post has been edited by tlhIn`toq: 14 November 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 RoyLittle0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Resetting cascading List boxes on change from preceding

Posted 14 November 2012 - 11:28 AM

Ok, point taken.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1