Resharper

Resharper – Surround with Quotes (“”)

Here is an option that is very handy, but seems to be turned off by default.   Have you ever had a bit of code or text that you wanted to throw into a string, surrounded by quotes?  Well there is a context action available in Resharper that makes a context action available.  This works in the C# section, not sure if a similar option is available in other contexts.

Resharper-Surround-With-quotes

Once this is enabled, you can highlight some text in your code, pull up the context action menu (Alt-Enter) and you’ll see the option.

Resharper-Surround-Context-Menu

Advertisements
.NET, C#, Tips

IEnumerable to Comma Delimited String

A few years ago I blogged about how to take a delimited string and convert it to a IEnumerable.  Today we ran across the opposite of this in that we wanted to take a list and convert it to a delimited string.   This can come in handy for a number of situations so it’s worth taking a look.

Given:

List<int> values = new List<int> {10, 20, 25};

In older versions of .NET (prior to 4.0 I believe) the easiest way was to do something like the following:

string newString = string.Join(",", values.ToArray());

However, starting in 4.0, the Join has some additional overloads that make this just crazy simple:

string newString = string.Join(",", values);

If you have a list of strings, there is still some benefit to converting this to an array. There is another overload of Join that allows you to be more selective in what you are converting.

Given a list of strings like the following:

List<string> strings = new List<string> {"a", "b", "c", "d", "e", "f"};

We can look at the last overload of; String.Join(delimiter, string[], startIndex, count)

This gives us a delimited string, starting at the element in the array at startIndex and going for the number of count.

string delimited = string.Join(",", strings.ToArray(), 2, 2);

will return “c,d”

I love how the framework provides these methods, often with multiple overloads to give us a lot of power with very little code. Enjoy!

.NET, Resharper

Resharper – Navigate to Type Goodness

In my opinion the navigation improvements made by Resharper are where much of my productivity gains come into play.  When I can quickly find and navigate around my project, I can stay more focused on solving the problem at hand rather than trying to fumble through my project structure remembering where things are.

One of my favorites lately is Go To Type of Symbol (Ctrl-Shift-T in Resharper keymap, Ctrl-Shift-F11 in the Visual Studio keymap).  At it’s simplest, put your cursor on a variable and hit the keystroke.

Photo1

If there is only one match, it simply takes you to the definition of the type (in your source or object browser if not).  Simple, but quick and easy.

The fun begins when there is more than one type.   The handiest example of that is when I have a List<> variable.   Then the Go To Type brings up not only the IList<> ( which I realize may be of limited usage), but it also brings up the type in the list (which is normally what I want to get at).

photo2

This get’s me quickly to what I’m looking for.

One more shortcut in my arsenal to move me quickly around my project.  Enjoy!

.NET, Code, Infragistics, WinGrid

Infragistics WinGrid – Tips and Tricks #1 – Right Click Row Select

I’ve been working with Infragistics tools, specifically the UltraWinGrid for 10 years or more (worked with the Sheridan grid before that, anyone remember those tools?), and outside of the Infragistics forums there seems to be very little in the way of information on this very powerful and complex tool.  Perhaps I can leverage my experiences with these tools into a nice little series of posts on things I’ve discovered over the years.

I came upon a situation where I wanted to right click on a grid, have it select the row under the mouse and bring up a context menu to act on that selected row.  As another requirement I wanted it to only act upon data rows.

I came up with the following extension method.

  1. First get the element under the mouse and determine if it’s a row element.
  2. If it’s a row, is it a datarow?
  3. Make it the active row.
  4. if the row is not already selected, select it and deselect any other row.
  5. if the row is already selected, then do nothing else special with the selection properties.
  6. if we’ve clicked somewhere not a datarow, I clear any selected rows in the grid.
21 public static void RightClickRowSelect(thisUltraGrid grid, Point mouseLocation)  
22 {  
23     UIElement element = grid.DisplayLayout.UIElement.ElementFromPoint(mouseLocation);  
24     var row = element.GetContext(typeof(UltraGridRow)) asUltraGridRow;  
25     if (row != null && row.IsDataRow)  
26     {  
27         grid.ActiveRow = row;  
28         if (!row.Selected)  
29         {  
30            grid.Selected.Rows.Clear();  
31            row.Selected = true;  
32         }  
33     }  
34     else  
35     {   
36        grid.Selected.Rows.Clear();  
37     }  
38  }

Then to use this, I simply call it in the MouseDown event on the grid as follows.

    1     privatevoid grdLabsMouseDown(object sender, MouseEventArgs e)
    2     {
    3       if (e.Button == MouseButtons.Right)
    4       {
    5         grdLabs.RightClickRowSelect(e.Location);
    6       }
    7    }

Pretty basic really, but knowing how to determine what element is under the mouse is a critical tool in your belt when dealing with the WinGrid and opens up the door for a lot of advanced features.