Is there a way to find out the brightness of a Color?
There is a very convenient Color.GetBrightness method that will tell you how close a Color is to black or white. This is useful when you want to use a bright or a dark color to draw based on whether the background Color is dark or bright.
I hate string processing. Is there an easier way to do string manipulation?
.NET has excellent support for Regular Expressions. Get the excellent ‘Regular Expressions with .NET ‘, e-book by Dan Appleman. It has all that you need to get started. Regular expressions really make string processing easier. There are expressions that you can find on the web for everything from validating dates to phone numbers.
I just want to write a simple text file. Is there some simple code for this?
It doesn’t get any simpler than this. In production code always make sure that you handle exceptions. using System.IO; …. // filePath has the complete path to the file StreamWriter writer = new StreamWriter(filePath); writer.Write(fileNewText); writer.Close();
I just want to read a text file into a string. Is there some simple code for this?
The following code reads a text file into a string object. It doesn’t get any simpler than this. In production code always make sure that you handle exceptions. using System.IO; …. // filePath should contain the complete path to a file. StreamReader stream = new StreamReader(filePath); fileText = stream.ReadToEnd(); stream.Close();
I have a delete key shortcut for my main menu. When my textbox is being edited, pressing delete activates this menu items instead of being processed by the TextBox. How can I get my TextBox to handle this delete
Subclass the TextBox and override its PreProcessMessage method. public class MyTextBox : TextBox { const int WM_KEYDOWN = 0x100; const int WM_KEYUP = 0x101; public override bool PreProcessMessage( ref Message msg ) { Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode; if((msg.Msg == WM_KEYDOWN || msg.Msg == WM_KEYUP) && keyCode == Keys.Delete) { return false; } return base.PreProcessMessage(ref msg); } }