Tag Archives: C#

Make a context-sensitive AcceptButton in C#

You’ve probably seen applications, often on web sites, where the action that a form takes when you press Enter depends on the control that has focus at the time. For example, many login pages have a place for you to … Continue reading

Posted in controls, user interface | Tagged , , , , , , , , , | Leave a comment

A little light holiday reading

Here are a few book books that make excellent holiday reading! (Actually they make good reading any time. ;-)) The C# Helper Top 100 Interview Puzzles Dissected C# 24-Hour Trainer, 2nd Edition Beginning Software Engineering Essential Algorithms: A Practical Approach … Continue reading

Posted in books | Tagged , , , , , , , , , , , | Leave a comment

Initialize a TreeView control to display a directory hierarchy in C#

This example adds the following LoadFromDirectory extension method to the TreeView control. // Initialize the TreeView from a directory, // its subdirectories, and their files. public static void LoadFromDirectory(this TreeView trv, string directory, int folder_img, int file_img) { DirectoryInfo dir_info … Continue reading

Posted in controls, directories, extension methods, files, user interface | Tagged , , , , , , , , , , , , , | Leave a comment

Cast arrays from one reference type to another in C#

Every C# programmer knows that you can use a cast operator to convert one data type into another. For example, if the Employee class inherits from the Person class, then the following code creates an Employee object and then makes … Continue reading

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

Use a timer to make a form TopMost or BottomMost in C#

The example Make a form TopMost or BottomMost in C# uses one method for making a form bottommost. It overrides the form’s WndProc method and looks for the message WM_WINDOWPOSCHANGING. When it finds that message, the program uses the SetWindowPos … Continue reading

Posted in API, controls, forms, user interface | Tagged , , , , , , , , , , , , , , , , , | 2 Comments

Make a form TopMost or BottomMost in C#

Keeping a form on top of other windows is simple. Just set the form’s TopMost property to true. Keeping a form below other windows is harder. To do that, the program must override its WndProc method, look for messages that … Continue reading

Posted in API, controls, forms | Tagged , , , , , , , , , , , , , , , , , | 1 Comment

Make a StatusLabel display its text even if it doesn’t fit in C#

If the text displayed in a StatusLabel is too long to fit in the containing StatusStrip control, then by default the StatusLabel doesn’t display anything. I don’t know what the rationale behind that it is, but it makes the StatusLabel … Continue reading

Posted in controls, user interface | Tagged , , , , , , , , , , , , | Leave a comment

Generate letters with given frequencies in C#

This example generates random letters with the frequencies they have in normal English text. The following code shows how the example gets started. // The letter frequencies. See: // http://en.wikipedia.org/wiki/Letter_frequency private float[] Frequencies = { 8.167f, 1.492f, 2.782f, 4.253f, 12.702f, … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , | Leave a comment

Make a text file a project resource in C#

This is handy for including a large amount of text as a project resource instead of putting the text directly in the code or including a file in the program’s installation package. Open the Project menu and select Properties. Open … Continue reading

Posted in files | Tagged , , , , , , , , | Leave a comment

Make a class define and raise events in C#

This example uses a BankAccount class to manage a bank account (and to show how to define and raise events). If the program tries to remove more money from the account than its current balance, the BankAccount object raises an … Continue reading

Posted in classes, events | Tagged , , , , , , , , , , , , | 4 Comments