Copy File Name to the clipboard – Windows explorer context menu
I added to the utilities section of the blog a small utility that add a menu in the Windows Explorer context menu that copy the file name to the clipboard. It works for all kind of files and also for folders.
Click this link to learn more and download it.
The approach is really simple. I have created a small application in C#. If the application receive a argument, it call the Clipboard.SetText(args[0]) method. Done.
To add a entry in the context menu of the Windows Explorer, you need to add a registry key.
1-Add a key under HKEY_CLASSES_ROOT\*\shell\
2-Add a command subkey.
3-Add a new string value that contains the executable path in the value as follow: abc.exe “%L”
To be able to handle the folders, you need to add a other registry key under HKEY_CLASSES_ROOT\Folder\shell\
Accordion control
Using the Expander control that I posted a week ago, I created a Accordion control. It’s in C# and use WinForm.
The source code is available here.
Here is a image showing the Accordion:

In this example, there is 3 expander. Each expander use a Label control for the header and content section.
You can plug your own control easily.
A other rich panel
Window form Expander
I wrote a expander using WinForm in C#
The source code is available here.
The Expander is divided in two regions: Header and Content. They take a Control, so you can create your own header control and put any control that you want into the content region.
Here is a image of a Expander created using the Expander control:

The header and the content are basic Labels.
The code to create this expander is the following:
Expander expander = new Expander();
expander.Size = new Size(250, 400);
expander.Left = 350;
expander.Top = 10;
expander.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander, "Header", SystemColors.ActiveBorder, Properties.Resources.Collapse, Properties.Resources.Expand);
Label labelContent = new Label();
labelContent.Text = "This is the content part.\r\n\r\nYou can put any Controls here. You can use a Panel, a CustomControl, basically, anything you want.";
labelContent.Size = new System.Drawing.Size(expander.Width, 80);
expander.Content = labelContent;
this.Controls.Add(expander);
This sample use the ExpanderHelper class to create a header label.
You can create your own header control. The next sample create a Label and connect the Label.Click event to toggle the expander and change the background color depending on the expander state.
Expander expander = new Expander();
expander.Size = new Size(250, 120);
expander.Left = 350;
expander.Top = 230;
expander.BorderStyle = BorderStyle.FixedSingle;
Label headerLabel = new Label();
headerLabel.Text = "Click me";
headerLabel.AutoSize = false;
headerLabel.Font = new Font(headerLabel.Font, FontStyle.Bold);
headerLabel.TextAlign = ContentAlignment.MiddleLeft;
headerLabel.BackColor = SystemColors.ActiveBorder;
headerLabel.Click += delegate
{
expander.Toggle();
if (expander.Expanded)
headerLabel.BackColor = SystemColors.ActiveBorder;
else
headerLabel.BackColor = SystemColors.ActiveCaption;
};
expander.Header = headerLabel;
Label labelContent = new Label();
labelContent.Text = "You are not limited to use the ExpanderHelper to create your header. Here is a example with a custom code and custom click event handler that change the header backcolor when the expander state change.";
labelContent.Size = new System.Drawing.Size(expander.Width, 75);
expander.Content = labelContent;
this.Controls.Add(expander);
Expander State
The expander offer 2 events: StateChanging and StateChanged.
You can use the StateChanging to prevent the expander to collapse or to expand.
A other rich panel
C# Scripting
Adding scripting in a C# application is really easy.
In that post, I will show you how to incorporate C# into your C# program.
I have created a small Script class that allow you to define a function with parameters and a return type, provide code and execute it.
Usage
The code below define a new script that return the integer 3:
Script script = new Script("Test", "return 3;", typeof(int));
object ret = script.Execute();
The following example define a script taking a string as first argument and showing a MessageBox with a Hello :
string code = "MessageBox.Show(\"Hello \" + name);";
Script script = new Script("Test", code);
script.ReferencedAssemblies.Add("System.Windows.Forms.dll");
script.Using.Add("System.Windows.Forms");
script.AddParameter("System.String", "name");
script.Execute(new object[] {"Test"});
See the generated code
You can also see the generated code with the GenerateCode method:
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.4952
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
namespace ScripterNamespace10111921232181
{
using System;
using System.Collections.Generic;
using System.Text;
public sealed class ScripterClass
{
public static object ScripterMethod()
{
return 3;
}
}
}
Demo application
I have created a small application that allow you to enter code, compile, run it and see the output.
Close Form programmatically in c# in the constructor
Sometimes you instanciate a Form and you want to do some validations in the constructor. If the validation fails, you want to close the form. Calling Application.Exit() directly in the constructor won’t work.
You need to call the Application.Exit in the OnLoad method.
If you don’t want to see the form at all during the validation process, put the window invisible on startup so it won’t be showed.
Set the WindowState to minimized and ShowInTaskbar to false in the Designer.
If the validation succeeded, set the WindowState to normal or maximized and ShowInTaskbar to true.
RichPanel improved
You can check my new expander (with a header and content controls) here
I uploaded a new RichPanel with improvements.
– Design time Expanded property support.
– Prevent from disappearing while collapsing.
– Overriding events instead of writing an event handler inside control.
– Expanding mode now works also with DrawShadow=false (not necessarily set ShadowOffset=0).
– Using small (+) (-) icons which looks better.
– Changing default values for some properties which which looks better in my opinion.
– Added a HotIcon for the header (mouse hover)
Thanks to Mohammad Elmi for thoses improvements.
Stopwatch: Timing operations easily with precisions
The System.Diagnostics namespace define a lot of classes.
Stopwatch is one great class that allow to time operations with precision.
Usage is really simple. Create a Stopwatch instance and use the Start() and Stop() methods to start and stop the counter. Use the ElapsedMilliseconds,
Stopwatch timer = new Stopwatch();
timer.Start();
Thread.Sleep(10000);
timer.Stop();
Trace.WriteLine(timer.ElapsedMilliseconds);
Trace.WriteLine(timer.Elapsed);
Trace.WriteLine(timer.ElapsedTicks);
Output:
10005
00:00:10.0051010
25990391
You can use the Reset method to reinitialize the counter.
Here is the MSDN link to the Stopwatch class
Disable method step-into with debugger
The attribute DebuggerHiddenAttribute indicate to the Visual Studio debugger that the method is not debuggable, so you can’t step in.
[DebuggerHiddenAttribute]
public void DoThis()
{
}
Enhancing debugging experinence with Debugger Display Attributes
Have you ever wrote a class with a lot of properties/members and using the debugger was unefficient due to the among of stuffs showed and the lack of formatting of the informations?
Thanks to the attributes, you can decorate the members and properties to change the default apparence and behaviour in the debugger.
Let’s say that you wrote this simple class:
public class Contact
{
public Contact(string name)
{
this.Name = name;
}
public string Name { get; set; }
public List<string> Properties { get { return this.properties; } }
private List<string> properties = new List<string>();
}
Let’s use our newly created class:
static void Main(string[] args)
{
Contact c1 = new Contact("JF");
c1.Properties.Add("P1");
c1.Properties.Add("P2");
}
Hiding unwanted properties/members
You can see in the debugger that, the properties and Properties are in a way duplicated:
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private List properties = new List();
Note that DebuggerBrowsable attribute is defined in System.Diagnostics.
As stated in the MSDN documentation, DebuggerBrowsable constructor take a DebuggerBrowsableState enumeration values. The 3 values defined are:
-
•Never indicates that the member is not displayed in the data window. For example, using this value for the DebuggerBrowsableAttribute on a field removes the field from the hierarchy; the field is not displayed when you expand the enclosing type by clicking the plus sign (+) for the type instance.
•Collapsed indicates that the member is displayed but not expanded by default. This is the default behavior.
•RootHidden indicates that the member itself is not shown, but its constituent objects are displayed if it is an array or collection.
DebuggerDisplay
You can also change the value formatting of every field/properties.
Imagine that you have a class and you want to have Count = 3 written without having to expand the class. you can use the Debugger display attribute to acheive it:
[DebuggerDisplay("Count = {count}")]
class MyClass
{
public int count = 3;
}
rethrowing a exception without loosing the callstack
If you don’t want to loose the call stack when you receive a exception and you just use the w keyword as in the following example:
try
{
….
}
catch (Exception)
{
…
throw
}







