Tag Archives: C# programming

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

Use statement lambdas in C#

The example Use lambda expressions in C# showed how to use lambda expressions to concisely create an anonymous method that takes parameters and returns a value. The following code shows one of the lambda expressions that example used. private void … Continue reading

Posted in algorithms, graphics, variables | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Use lambda expressions in C#

Lambda expressions are basically just a shorthand notation for making an anonymous method. There are a couple of formats, the simplest of which has the following syntax. x => expression Here expression is an expression involving the variable x. For … Continue reading

Posted in algorithms, graphics, variables | Tagged , , , , , , , , , , , , , , | 1 Comment

Use anonymous methods in C#

My previous post Use the predefined Action, Func, and EventHandler delegate types in C# explains how to use the predefined generic Func, Action, and EventHandler delegate types to graph four functions F(x, y). It uses the following code to define … Continue reading

Posted in algorithms, graphics, variables | Tagged , , , , , , , , , , , , | 1 Comment