Posts Tagged ‘C#’

C# line return character – .NET Framework answers

via C# line return character – .NET Framework answers

string s = "First line" + Environment.NewLine + "Second line";


How to write newline\tab character (\n, \t) in .resx file?

via How to write new line character \n in resx file?


Use Shift + Enter in the .resx resource editor.

Another solution for \n would be:

(In .resx file) with property MyMessage = “One line & # 13 ; NextLine”
(omit the whitespace for proper formatting)

You can add tab ( \t ) in the same manner:  & # 9 ;
Thanx, good ol’ ASCII codes 🙂

Hope this is helpful.

Type Conversion and Conversion Operators in C# | C# Help – C# Tutorials and Resources

public static implicit operator int(SomeType typ)


// code to convert from SomeType to int


via Type Conversion and Conversion Operators in C# | C# Help – C# Tutorials and Resources.

Wait for BackgroundWorker to complete

Wait for BackgroundWorker to complete.

[C#] How to get screen resolution/width/height?

It’s just a matter of using this one line of code:

var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var width  = screen.Width;
var height = screen.Height;

A quick noted post for later use since a google search on this topic not giving you the full answer right a way.

Showing a “waiting” box during a long execution

If working with WinForm/WPF application, you sometimes run into a need to show a “Please wait” box after user pressed a button which fires a long execution. The technique lies below it is as below.

The full sample codes can be downloaded here.

The original codes

Below is a sample code snippet handling the event when user press a button.

private void NoBackgrounding()
    this.StatusText = "Loading...";
    this.StatusText = "Loading... done.";

This button is in a simple form as below. The labels in red color are used to display the “waiting” box.

The problem

As in the sample handler, the execution take a long period of 10 seconds – the user will get a “not responding” state of the application. This seems to be not very friendly; a more lovely behaviour would be show a “please wait” box so as 1) to notice the user and 2) to get rid of “not responding” state.

This means we need to “move” the long execution to a background thread/worker and continue to finish the handler method. The background worker when finish will update the “waiting” box to “done”.

The codes provided with background

The the full codes to put the “long” codes in background is as below.

private void WithBackgrounding()
    using (var worker = new BackgroundWorker())
        worker.DoWork += (sender, e) =>
            this.StatusText = "Loading...";
        worker.RunWorkerCompleted += (sender, e) =>
            this.StatusText = "Done.";

Final factor to add

In your background worker, sometimes there’re calls to update UI elements. Then, the framework needs you to invoke a method to get that works. Here’s how to make a invoke-call.

private void BackgroundRaiseErrorFixed()
    var t = Environment.TickCount;
    while (true)
        if (Environment.TickCount - t > 2000)
    var method = new BackgroundInvokeDelegate(
    () =>
        var s = (Environment.TickCount - t).ToString();
        this.labelUIUpdating.Content = s;

Hope this article is helpful to you!

Nam Gi VU.


Code snippet to generate codes for a test class/method

Full details:

  1. Go to Edit – Intellisense – Insert code snippet
    or Ctrl – K – X
  2. Select Test
  3. Select Test Method/Test Class to insert the snippet for test class/method.
  4. Note: If hovering your mouse pointer on Test Method/Test Class you will get short-cut of the snippet in tool tips.

Get it quickly:
Start typing testc/testm and then tab twice to insert the snippet for test class/method.