C# – using Attributes

Attributes in C# is used to embed custom metadata about the code / class in the code. Microsoft has a very good example on how to write your own custom attribute class:Microsoft Custom Attribute Example. Here is an example of Custom attribute call Developer. You can use this attribute by using DeveloperAttribute or simply use […]

Using Obsolete Attribute in C# .NET

C# has a built-in attribute that you can apply to any type in your C# code to designate a method as obsolete. To do that, before your method declaration, simply put in the [Obsolete] Attribute. For example: [Obsolete] private void testfunction() { //some code here } After you set this attribute in your code, every […]

Simple Example of using Localization Strings Resource in C# Application

<span style="color: #333399;"><em><strong>Following is a simple example of using a Localization Strings resource in a C# .NET application.</strong></em></span>   //this will manually set the current culture to a specific culture //by setting the culture, .NET would know which Resource string file to use   Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-Hans");   //This will simply calls the strings resource […]

C#: Hide Mouse & Disable Mouse input

To disable mouse input & hide mouse cursor: – In your Form Class, implement the interface IMessageFilter Use the following code: //in class header public partial class FrmInputPrompt : Form, IMessageFilter // method for iMessageFilter & methods for disable/enable mouse. bool IMessageFilter.PreFilterMessage(ref Message m) { if (m.Msg == 0x201 || m.Msg == 0x202 || m.Msg […]

C#: force TextBox to only Enter Number

Normally, Textbox Control would allow any text. If you want to restrict it to only enter numbers, you don’t need to use a MaskEdit, you need to simply add a handler for the event KeyPress on the TextBox, and set e.Handled to true only when it’s number that is being key in. private void txtAnswer_KeyPress(object […]

Thread safe way to update UI elements in C#

//declare the delegate delegate void SetControlValueCallback(Control oControl, string propName, object propValue);   // the function that sets properties on the UI control private void SetControlPropertyValue(Control oControl, string propName, object propValue) { if (oControl.InvokeRequired) { SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue); oControl.Invoke(d, new object[] { oControl, propName, propValue }); } else { Type t = oControl.GetType(); System.Reflection.PropertyInfo[] […]