Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Summary
Great tips for .NET developers
Description

Proper use of Encoding
You should use Encoding.Default to get properly localized data. Encoding.ASCII should be used only if you're dealing with true ASCII (lower 128 or 255 with default ASCII encoding) which is rarely the case. If you need to do a two-way convert always use the Default Encoding which will use your machine's configured codepage to convert to and from Unicode.

An example where this might make a difference (two-way conversion) is for encryption/decryption. Your encryption class takes a parameter for the Plain text data, and another parameter for the encryption key. You need to use the Encoding.Default to ensure that you don't lose any characters in the encrypt/decrypt process.

Dim plainText As Byte() = Encoding.Default.GetBytes(data)
Dim key As Byte() = Encoding.Default.GetBytes(userKey)

from a solution provided by Rick Strahl in Message #1235288

Read an Excel Range
Try this method for reading a range from an Excel file:
    
Public Shared Function GetXLRange(ByVal pSheet As Excel.Worksheet, ByVal pCell1 As String, ByVal pCell2 As String) As Excel.Range
        Try
            Dim rng As Excel.Range
            rng = CType(pSheet.Range(pCell1, pCell2), Excel.Range)
            Return rng

        Catch ex As Exception
            'Process the error
            Return Nothing
        End Try
End Function

from a solution provided by Éric Moreau in Message #1185450

Closing a Browser Window
There is no code that you can put in the code-behind for shutting down the browser. There is a javascript command though - window.close(). You can put it in the onClick attribute of a button (or wherever).
        Dim popupScript As String = "<script language='javascript'>  window.close() </script>"
        Page.RegisterStartupScript("PopupScript", popupScript)

from a solution provided by Keith Payne in Message #1170098

Detecting Copy/Paste in a Control
The normal Copy/Paste windows commands work fine in any controls, such as a TextBox. But, what if you'd like to detect that something was pasted into a control, so that you could do something, such as validation, *before* displaying it?

You need to subclass the control and dig into the message queue. For example, subclass the TextBox and do something like this:

public class MyTextBox : TextBox
{
    private const int WM_PASTE = 0x302;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PASTE)
            OnSomethingPasted();

        base.WndProc(ref m);
    }

    protected virtual void OnSomethingPasted()
    {
        //
        // Override this and do something.
        //
    }
}
This is a simple example but should give you a starting point. In this example, we are letting the standard behaviour (ie. the paste operation) cascade - you could easily stop this.

from a solution provided by Neil Tonkin in Message #1212359

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers