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

Posted on 2011-10-05
Medium Priority
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

CodeCruiser earned 2000 total points
ID: 36919303
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

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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

809 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