Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, October 21, 2011

Computing RGB composite Color Value in C#

If you still have to deal with the Win32Api in C#, you may need to get the old-style RGB composite color values from a .NET Color value. The ToArgb method doesn't work like you want, so it has to be computed:

  private int ConvertColorToRGB(Color col)
    {
    return (col.R) | (col.G << 8) | (col.B << 16);
    }
 
If you are using VB.NET, it still has an RGB function to call or:

   Public Function ConvertColorToRGB(col As Color) As Integer

        Return CInt(col.R) Or (CInt(col.G) << 8) Or (CInt(col.B) << 16)

    End Function

Thursday, July 14, 2011

Selecting Files to Open or Save in VB.NET

VB.NET has come a long way from the clunky Common Dialog control of VB6 for implementing a standard dialog that will let you browse to find a file to Open or Save. Here is an example of each:

Browsing to Open a File in VB.NET
      Dim fileDlg As New OpenFileDialog

fileDlg.Filter = "CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"
fileDlg.DefaultExt = "csv"

' could be omitted to use default, or a specific dir can be given
fileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

If fileDlg.ShowDialog() = DialogResult.OK Then

' Do something with the selected filename
Debug.Print(fileDlg.FileName)

Else

' the dialog was cancelled

End If

The OpenFileDialog object is used here, and I have only shown the most commonly used options. The Filter property lets you specify the which File Types will be selectable form the Open File Dialog (in a dropdown list). The value is set to a string that is a pipe (|) delimited list composed to a File Type Description and Filter Pattern. If you have more than one File Type, you can add another pipe, then another Description|Filter Pattern, and so on. If you leave the Filter property empty, then then Open File Dialog will just not have a drop down to select the File Type filter. Here are some examples:

"CSV File (*.csv)|*.csv"
"CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt"
"CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"

If you are using Filter with more than one File Type specified, you can also use DefaultExt to specify which File Type to use as the default. Just specify the extension as a string (with no dot). This property also will set the default file extension to use if you don't specify it in the filename (if typed in), so this property can also be used without the Filter property being set.

The InitialDirectory property will let you define which directory to open up in. You can set this property to an explicit path (like "c:\MyData") or you can use the special directories that are defined for you, like:
Environment.SpecialFolder.MyDocument
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Desktop

There is also a Title property that can be used to change the name of the Open File Dialog. In this example, it is not used and defaults to "Open File".

After the ShowDialog call returns OK, use the FileName property to get the selected filename.

Browsing to Save a File in VB.NET
   Dim fileDlg As New SaveFileDialog

fileDlg.Filter = "CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"
fileDlg.DefaultExt = "csv"

' InitialDirectory can be omitted to use default, or used to specify an explicit directory
fileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

fileDlg.OverwritePrompt = True

If fileDlg.ShowDialog() = DialogResult.OK Then

' Do something with the selected filename
Debug.Print(fileDlg.FileName)

Else

' the dialog was cancelled

End If

Saving a file is similar to opening a file. You use a SaveFileDialog object instead of a OpenFileDialog object. Many of the properties work the same way as with the OpenFileDialog such as Filter, DefaultExt, and InitialDirectory. I also show OverwritePrompt set to True (which is the default). This setting will cause the dialog to prompt you when you try to save over an existing file.

After the ShowDialog call returns OK, use the FileName property to get the filename.

If you want to select a Directory instead of a file, look at this post.

Wednesday, July 13, 2011

Setting all elements of an Array to Null in C# (like with memset in C++)

I sometime find it strange that things we take for granted in C++ are hard in C#. Probably I should say hard to find the equivalent for rather than hard to do. I routinely clear out my arrays in C++ with memset (setting each item to 0). To get the equivalent in C#, you can use the Array.Clear(...) method like this:
Array.Clear(MyArray,0,MyArray.Length);
Here is a more detailed example:

http://www.dotnetperls.com/array-clear

Tuesday, July 12, 2011

Preventing Flicker with GDI+ drawing in C#

In C#, the OnPaintBackground method is called before the OnPaint event and it draws the background color for the entire view. If you are already drawing the background in the OnPaint event, then this process will cause flicker. You can override the OnPaintBackground and just do nothing in it to prevent the flicker:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// do nothing
}

Tuesday, June 28, 2011

Convert char[] to String in c#

If you have something like:

char [] chArray = infile.ReadChars(10);

or

char[] chArray = new char[] {'t','e','s','t'};

and you want to convert it to a String, I always (incorrectly) try:

String str = chArray.ToString();

and it doesn't work (you get "System.Char[]").

You have to say something like this:

String str = new String(chArray);

Saturday, April 24, 2010

VB.NET and C# Comparison

Great posting that compares VB.NET and C#:

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html