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

C# - Converting String to Enumeration

Sometimes it is useful to convert a string to a value of an enumeration. It's extremely easy to convert an enumeration value to a string of course, with the ToString() method, however doing the vice versa of it doesn't seem as easy.

Searching on the net, you will find that most people create their own function, in which they will loop through all values in the enumeration, compare their string equivalent with a given string and if they are equal, the enumeration value is returned. But why do this when a method is there waiting for you on a sunbed? It is the static method Enum.Parse() which has 2 overloads.

The first overload takes the type of the enumeration and the string value, returning an object, which may often need to be cast to the type of enumeration.

Example:

enum Days {Sun, Mon, Tue, Wed, Thur, Fri, Sat };


private void main() {
  string s = "Mon";
  Days day = (Days)Enum.Parse( typeof( Days ), s );
}
The day variable will contain the enumeration value Mon.

If the given string does not exist in the enumerations, the method will raise an ArgumentException. If the string is null, the method will raise an ArgumentNullException.

Now what if in the above code, s was equal to "mon" instead of "Mon"? By default, the casing is regarded and thus, that would raise an ArgumentException. However you may use the second overload, which takes an extra boolean parameter to specify whether casing should be regarded or not.

private void main() {
  string s = "mon";
  Days day = (Days)Enum.Parse( typeof( Days ), s, true );
}
The third parameter specifies that the casing should be ignored, and thus that code is raise no exception.

All's well.

C# - Coordinates to Col/Row index

When using a DataGrid or a DataGridView control, have you ever had mouse events such as MouseDown or MouseClick, where you wanted to know in which cell (or part of the table) is the pointer pointing to? You have the X and Y coordinates passed in the MouseEventArgs parameter, but how would you translate them to column and row indices?

One way is to apply a long formula involving those coordinates, the size of the table, sizes of cells, headers, etc... But obviously, that's tedious for a C# developer. That's the primitive way (or the C way) of handling it.

Thanks to a workmate, I discovered the magic of the HitTestInfo class. An instance of this class will give you the row and column indexes amongst other information. To create the object, say in a MouseDown event, you do the following:

private void dataGridView1_MouseDown( object sender, MouseEventArgs e )
{
System.Windows.Forms.DataGridView.HitTestInfo h = dataGridView1.HitTest( e.X, e.Y );
}

A DataGridView object contains the method HitTest(), which, given the x and y coordinates, returns an object of type HitTestInfo with all the information you need. Note the namespace of the class: System.Windows.Forms.DataGridView. If you are using a DataGrid, the class would be System.Windows.Forms.DataGrid.HitTestInfo (2 different classes).

With our object created, we can get various useful information (much more useful than x and y).

HitTestInfo.Type gets what part of the table is at the coordinates (Ex. column header, cell, etc...)
HitTestInfo.ColumnIndex & HitTestInfo.RowIndex are self explanatory.

if ( h.Type == System.Windows.Forms.DataGridViewHitTestType.ColumnHeader )
MessageBox.Show( "You clicked on a column header." );
else
MessageBox.Show( h.Type.ToString() + '(' + h.ColumnIndex + ',' + h.RowIndex + ')' );

Note that if you are using a DataGrid, the HitTestInfo.Type returns an element from the enumeration System.Windows.Forms.DataGrid.HitTestType.