Wednesday, October 15, 2008

Add any custom controls or Windows Forms controls to a ToolStrip

When we are in need to put some custom controls or Windows Forms controls to a ToolStrip and we think that we only have the options gave to us from the ToolStrip control, we need the help of the ToolStripControlHost Class. This class Hosts custom controls or Windows Forms controls.

Declare a ToolStripControlHost field. In the constructor of the Windows form and after InitializeComponent();, create a new ToolStripControlHost with your required controller in the argument. Then add the ToolStripControlHost to your ToolStrip Items.

It's that easy!!

  
private ToolStripControlHost toolStripControlHost;

public WindowsFormConstructor()
{
InitializeComponent();
toolStripControlHost = new ToolStripControlHost(dateTimePicker1);
toolStrip1.Items.Add(toolStripControlHost);
}

Saturday, October 11, 2008

Break Strings in Excel

So many times we needed to import an excel file to a database, receives an excel file filled with people full names, and we need to break it in two other columns regarding the first name and the last name, or even the two together.
This is the way to do it:

First String: =LEFT(A1,FIND(" ",A1,1))

Last String: =RIGHT(A1;LEN(A1)-FIND("*";SUBSTITUTE(A1;" ";"*";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")))))

All Except the First String: =RIGHT(A1;LEN(A1)-FIND(" ";A1;1))

When ever I have more useful excel string functions, I'll update this post.

Monday, October 6, 2008

c# ambiguity names between method and event

Maybe some of you have already got to a dead end with ambiguity names, when trying to implement an interface where a method of the interface and an event have the same name, and we won't be able to catch the event triggered by the class.
Ambiguity means that the object of the class doesn't know how to distinguish which to use, because they have the same name, and the compiler will use the method instead of the event.
To get ride of the warning and the error associated with that, given by Visual Studio 2008 "Ambiguity between method and non-method . Using method group.", we need to do some cast inbetween.

The necessary cast will be to the Class of the Interface:
((InterfaceClassName) ourObject).InterfaceEventsName_Event_ EventName += new InterfaceEventsName_EventNameEventHandler(MethodNameToCall);

.Net System.Diagnostics.DebuggerStepThrough Attribute

Sometimes when you are running your code in debug mode, you don't want to stop in some methods, maybe because you already know that this method will always return what you want, because is not so important for the job you have in hands, or some other reasons... That's where the DebuggerStepThrough comes in. The compiler will add the necessary metadata preventing the debugger to step into the method. Even if you have this attribute defined, you can stop in, if you define a breakpoint there.

public string Name {
[System.Diagnostics.DebuggerStepThrough]
get { return name; }
}

Performance of foreach vs. List.ForEach

"...when compiling without optimizations, List.ForEach is even faster than a standard for-loop! Of equal interest is to notice that (without compiler optimization) foreach is nearly as fast as a standard for-loop. So, if you're compiling your app without compiler optimizations, List.ForEach is the fastest way to go..."
"...by how must the compiler optimizes for-loops to get more than 50% savings. foreach-loops also get about 20% shaved off. List.ForEach does not receive as much optimization but the important thing to notice is that is still beats foreach-loops by a considerable margin. List.ForEach is faster than a standard for-each loop..."
Complete Article