How do I loop through a multi-column listbox in WPF?

Posted on 2011-10-05
Last Modified: 2012-08-13
Hello Experts,

I am a beginner with WPF. I fear this may sound like a silly question.

I have a listbox with two columns. Each listbox item contains a horizontal stacked panel which in turn contains textblocks.

The listbox is empty, with each listbox item being added by the end-user through a couple of textboxes placed elsewhere. The first column accepts strings, and the second column accepts only percentages.

(I have attached a relevant portion of the event sub where a user is adding new rows.)

I would like to be able to check every time the above event is fired that the column of percentages does not exceed 100%. I have a couple of labels below the listbox itself where I would like to show the running total and the remainder. However, how can I loop through the second column of percentages to show the running total and remainder?

I would appreciate any sort of guidance towards a solution on this. Thank you very much.
Private Sub btnAddItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnAddSplit.Click


            Dim ListBoxItemName As New TextBlock
            ListBoxItemName.Text = Name.Text
            ListBoxItemName.Width = 170

            Dim ListBoxItemValue As New TextBlock
            ListBoxItemValue.Text = SplitValue.Text
            ListBoxItemValue.Width = 70

            Dim ListBoxStackPanel As New StackPanel
            ListBoxStackPanel.Orientation = Orientation.Horizontal

            Dim NewEntry As New ListBoxItem
            NewEntry.Content = ListBoxStackPanel



    End Sub

Open in new window

Question by:MrMoneypenny
    LVL 83

    Accepted Solution

    Why not validate the percentage value in the above code rather than looping through list? Assuming that SplitValue.Text is the percentage, do a simple > 100 check and show error message if it is. You can also do the total in the above code. Maintain a class level variable and add to it in the above code.

    Author Closing Comment

    Thanks for your tip, I willl set about doing just that.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    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.

    733 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

    24 Experts available now in Live!

    Get 1:1 Help Now