Wrox Programmer Forums
|
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2011, 02:30 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 27-3 column

I'm working through the Chapter 27 exercises, and on 3. what are the Items settings that define the data for the ListView columns? I can't seem to find a reference to this. thanks again for all your help.
 
Old June 30th, 2011, 04:12 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Do you mean how does the program set the item values or how does it prepare the ListView to hold them? The ListView control is pretty useful but it's a bit awkward to use.

To prepare the control at design time, select the control and select its Columns property. Click the ellipsis on the right to open the ColumnHeader Collection Editor. Use it to make the columns.

To create items at design time, select the Items property and click the ellipsis on the right. Add an item and set its Text. Then click the SubItems property (in the Data section) and click its ellipsis. There you can add the sub-item values.

To create an item at run time, you add it to the Items collection. that collection's Add method returns a new ListViewItem. You then add the sub-items to that object's SubItems collection. I use the following method to make this easier.

Code:
// Make a row in the ListView.
private void MakeCar(string name, int maxSpeed, int horsepower, decimal price)
{
    ListViewItem item = carListView.Items.Add(name);
    item.SubItems.Add(maxSpeed.ToString());
    item.SubItems.Add(horsepower.ToString());
    item.SubItems.Add(price.ToString("C"));
}
I hope that helps. If I've missed your question, let me know.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old June 30th, 2011, 04:32 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, you are on track with your answer. The question I have is regarding the syntax of the appropriate fields within Items and SubItems. So, in the Items property, the text is entered similar to the name of the column? And with the subitem, Data section, the actual name of the object is entered, maxspeed, horsepower?
 
Old June 30th, 2011, 04:55 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Well, normally your code can access the items and sub-items by index. You can't say listView1.Items["FirstName"] = "Rod" or whatever. Instead you need to add a new item using Items.Add and then use the returned object;s SubItems.Add method to add the subitems. It's a bit awkward.

Each of the items and sub-items has a Name property that you can set and then later use to find an item. For example, this code sets the first item's Name to "A":

listView1.Items[0].Name = "A";

Now you can refer to it like this:

Console.WriteLine(listView1.Items["A"].Text);

Similarly you can set and get a sub-item's Name:

listView1.Items["A"].SubItems[0].Name = "Test";
Console.WriteLine(listView1.Items["A"].SubItems["Test"].Text);

Oddly at design time the Items collection editor doesn't support the Name property and the SubItems collection editor seems to display it but doesn't seem to store the values you enter for use at run time so they aren't very useful unless you populate the values at design time.

You can take other approaches. For example, you can assign names to the columns and then look in the Columns collection to see their names. Similarly you can look at a column's Text property to see what it says. Once you find the column you want, you may be able to use its index to access sub-items.

As I said before, it's a bit awkward.

Do you have a particular scenario in mind?
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old June 30th, 2011, 05:06 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, I think I may have sent you off in a wrong direction. I'm referring to configuring the columns using the Properties of Listview box for the Form1.cs[Design]. I built the form, but when I click on the columns they aren't sorting. I suspect I have the control properties set incorrectly.
 
Old June 30th, 2011, 05:32 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I think I see. The sorter class for those exercises can look at the items' subitems. For example:

if (item1.SubItems[0].Text < item2.SubItems[0].Text) ...

You'll want to use different indexes in the array depending on which column you are using to sort. I.e. instead of [0] you may want to use [1] or [2] or [SortColumn] where SortColumn is a property that tells you which column to use in sorting. I think the ColumnClicked event handler has a parameter that tells you which column was clicked.

Let me know if I'm still headed in the wrong direction. (And remember you can download the solution and peek if you need to.)
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old June 30th, 2011, 06:36 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'd gladly take a peek. For some reason, Designer mode for the form isn't an option under View when I download the book's version of the project.
 
Old July 1st, 2011, 05:04 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I figured it out. For the repetitive lines of code, I have been copying and pasting sections like this
CarComparer sorter = new CarComparer(CarComparer.SortType.ByMaxSpeed);
List<Car> sortedCars = new List<Car>(Cars);
sortedCars.Sort(sorter);
carListBox.DataSource = sortedCars;

And then changing the variables. It looks like Visual Studio is a little more finicky about recognizing code entered this way than I expected. Once I re-entered some of the variables, the columns sorted. thanks for your help again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 27 Java Novice BOOK: Java Programming 24-Hour Trainer by Yakov Fain 0 May 15th, 2011 08:26 AM
HTTPModule Chapter 27 sherbug BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 4 August 21st, 2010 02:03 PM
Chapter 27: Modules And Handlers - Listing 27.2 Nikhil BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 February 4th, 2010 07:25 AM
where are the source code for Chapter 27 ? jamesxd BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 1 June 20th, 2008 08:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.