Category Archives: lists

Draw text wrapped to fit columns in C#

The post Draw aligned columns of data in C# shows one way to draw text in rows and columns. It uses the Graphics class’s MeasureString method to size the columns so they are big enough to display their data without … Continue reading

Posted in fonts, graphics, lists | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Make extension methods that pick random items from arrays or lists in C#

This example uses a simple but useful technique to let you pick random items from arrays and lists. The following code defines RandomElement extension methods that pick random items. public static class ArrayExtensions { // The random number generator. private … Continue reading

Posted in arrays, extension methods, lists | Tagged , , , , , , , , , | 1 Comment

Determine whether two lists contain the same sequences of objects in different orders in C#

The SequenceEqual method determines whether two IEnumerable sequences contain the same objects in the same order. There is a simple way to determine whether the lists contain the same objects in different orders: sort the objects and then call SequenceEqual. … Continue reading

Posted in LINQ, lists, miscellany, OOP | Tagged , , , , , , , , , , , , , | Leave a comment

Determine whether two lists contain the same sequences of objects in C#

The IEnumerable interface provides a SequenceEqual method that determines whether two IEnumerable sequences contain the same objects. Normally, it uses reference equality so it considers two objects equal only if they refer to the same instance. However, if the objects … Continue reading

Posted in lists, miscellany, OOP | Tagged , , , , , , , , , , , | 2 Comments

List Dictionary keys and values in C#

A Dictionary object’s Keys property is a KeyCollection containing the dictionary’s keys. Similarly, its Values property is a ValueCollection containing the dictionary’s values. These collections are slightly different from the List objects that you probably use in your code. In … Continue reading

Posted in algorithms, controls, lists | Tagged , , , , , , , , , , , , | Leave a comment

Make an owner-drawn ListBox that justifies columns in C#

This example shows how you can use an owner-drawn ListBox to left and right align values in columns. The following examples show different ways to align values in columns. Draw aligned columns of data in C# Format values in a … Continue reading

Posted in controls, fonts, graphics, lists, user interface | Tagged , , , , , , , , , , , , , , , , , , | Leave a comment

Draw aligned columns of data in C#

This example draws a series of rows with aligned columns either left or right justified. The program uses the following code to define the data it will draw and the row and column alignments. // The values. private string[][] Values … Continue reading

Posted in fonts, graphics, lists | Tagged , , , , , , , , , , , , , , , | 6 Comments

Iterate over the items in arrays and lists in C#

You can use a foreach loop to iterate over the items in arrays, lists, collections, and other objects that have a GetEnumerator method. These loops are often more convenient than for or while loops because you don’t need to worry … Continue reading

Posted in arrays, lists, syntax | Tagged , , , , , , , , , , , , | Leave a comment

Display database records without data binding in C#

Many programs use data binding to display database records. That method is easy (at least as far as the code you write is concerned), but it’s not very flexible. This example shows how you can display database records without data … Continue reading

Posted in controls, database, lists, user interface | Tagged , , , , , , , , , , , , , | 1 Comment

Initialize lists from databases without binding in C#

Note that this is a very manual way to initialize lists. Often you can display data more easily by using data binding. In this example, I added the Access database file Books.mdb to the project and set its “Copy to … Continue reading

Posted in controls, database, lists, user interface | Tagged , , , , , , , , , , , , | 1 Comment