Blog

posts tagged with csharp

NotificationFormTraceListener Assembly

0 Comments
By Fons Sonnemans, 26-nov-2007

Download TraceForm.zip (VS2005 Solution, C# 2.0)

Introduction

During development, you can use the output methods of the Debug class to display messages in the Output window of the Visual Studio 2005 integrated development environment (IDE). For example:

' Visual Basic
Trace.WriteLine("Hello World!")
Debug.WriteLine("Hello World!")

// C#
Trace.WriteLine("Hello World!");
Debug.WriteLine("Hello World!");

Each of these examples will display "Hello World!" in the Output window when the application is run in the debugger.

The .NET TraceListners monitor trace and debug output. You use TraceListners to redirect this output to a specific medium. There are 3 TraceListners available: DefaultTraceListener, EventLogTraceListener and TextWriterTraceListener.

The NotificationFormTraceListener is a special TraceListner which allows you to monitor the output in a special TraceForm which can be activated using a Notification icon.



Sample: TraceForm used to monitor SQL statements

Usage

The NotificationFormTraceListener can be used in a Windows application. You have to create one and add it to the (static) Listeners collection of the System.Diagnostics.Trace class.

Don't forget to Dispose the listener when you exit the application. This is necessary because an extra Thread is used for the NotificationFormTraceListener. This Thread doesn't stop automatically.

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
staticvoid Main()
{
    // Initialize the TraceListener
    ReflectionIT.Diagnostics.NotificationFormTraceListener
        m_listner=new ReflectionIT.Diagnostics.NotificationFormTraceListener();

    // Add it to the Listeners collection
    System.Diagnostics.Trace.Listeners.Add(m_listner);

    // Run the MainForm
    Application.Run(new TraceFormTest());

    // Dispose all TraceListeners
    foreach (TraceListener l in Trace.Listeners){
        l.Dispose();
    }
}
Sample: static Main

ContextMenu
The ContextMenu of the NotificationIcon makes it possible to Open the TraceForm, set the 'Always on Top' option and Exit.


Add Tracing using the Configuration file

You can add TraceListeners from your code but you can also add them by editing the configuration file that corresponds to the name of your application. Within this file, you can add a listener, set its type and set its parameter, remove a listener, or clear all the listeners previously set by the application.

<configuration>
    <system.diagnostics>
        <traceautoflush="true"indentsize="4">
            <listeners>
                <addname="myListener"
type="ReflectionIT.Diagnostics.NotificationFormTraceListener, ReflectionIT.Diagnostics"
                     initializeData="Buffy.NET Demo Trace"/>
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>
Sample: BuffyDemo.exe.config

Conclusion

TraceListers can help you to debug your code. The NotificationFormTraceListener helps you to monitor the Trace and Debug output in more convenient way.

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

Thanks to:

  • Damien Pitman who reported and solved a threading problem (bug).
Tags: CSharp

READ MORE

ConvertSelectedTextToAssociatedLabel Macro

0 Comments
By Fons Sonnemans, 09-okt-2007

I have created a new Visual Studio 2005 Macro which can be used to convert a literal text to an asp:Label that is associated to the next control. You first have to select the text and then Run the ConvertSelectedTextToAssociatedLabel Macro. In the example below the text Age: is selected.

The result is an asp:Label with the correct Text and AssociatedControlID attributes.

Download Zipfile

READ MORE

My First XNA Game

0 Comments
By Fons Sonnemans, 27-mei-2007

I finally found some time to play with XNA. I learned a lot from Rob Miles at TechEd 2006. He has some great samples on his website. Rob will also be presenting about this subject on the DevDays 2007 in the Netherlands. Fun is guaranteed.

My first game is a remake of a game I build 20 years ago on my Commodore 64. You must move around a bouncing ball using your joystick. The ball may not collide with the flying birds. It took me only a few hours to build and it was great fun.

XNA - Drawing Text in XNA

READ MORE

Updated my DAL article!

0 Comments
By Fons Sonnemans, 17-mei-2006

I have updated my DAL article which I wrote late 2002. This article was one of my first aricles and needed an update. The download project now also contains a .NET 2.0 project. It uses the new TraceSource class for tracing purposes. I hope you like it.

Tags: CSharp, SQL

READ MORE

Hooray!

0 Comments
By Fons Sonnemans, 19-feb-2006

I have just received mail from Pearson VUE informing me that have passed the Beta Exam 70-528 TS: Microsoft .NET Framework 2.0 - Web-Based Client Development (called 71-528 while it's in Beta). I also received a free Voucher for a next exam, thanks guys.

On Friday I will try the Beta Exam 70-551 UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework. One week later I will try the Beta Exam70-552 UPGRADE: MCAD Skills to MCPD Windows Developer by Using the Microsoft .NET Framework . Wish me luck.

Tags: CSharp, Framework

READ MORE

New VS2005 Macros

0 Comments
By Fons Sonnemans, 18-nov-2005

Three years ago I have written some useful Visual Studio 2003 macros: SortCode and CreateProperty. With the introduction of VS2005 it was time to upgrade them.

The new SortCode macro also supports VB.NET 2005. Totally new is the EncapsulateAllNonPrivateFields macro. It creates properties get/set for all non private (public, protected, internal) fields. By using the CodeDom I managed to supports VB.NET and C#.

Example

After running the marco the 3 fields are private and there are 3 extra properties.

READ MORE

Avalon and ClickOnce demo

0 Comments
By Fons Sonnemans, 23-sep-2005

I have build my first Windows Presentation Foundation (WPF) application. It is a remake of my WinForms Minesweeper game. Go to the ClickOnce publisher page and install Avalon Minesweeper.

Make sure you have the Microsoft Pre-Release Software WinFX Runtime Components - September Community Technology Preview (CTP) installed on your computer.

To run this sample you must add 'http://www.mydotnetplayground.nl/WpfMinesweeper' to your Trusted Sites in Internet Explorer.

UPDATE 1-1-2006: Moved to my own provider and updated to the December CTP.

Tags: WPF, CSharp

READ MORE

C# 3.0 looks like Smalltak

0 Comments
By Fons Sonnemans, 14-sep-2005

Many years ago I programmed in Smalltalk (Enfin which later became ObjectStudio). I have always liked it although it was not very programmer friendly (no IntelliSense). I have just wachted this C# 3.0 Language Enhancements in action video. The shown Extension Methods feature realy looks like the Secondary Class files of Smalltalk. Nice to see them back, they where very handy.

In the following example I have added the 'IsPrime()' method to the 'Int32' type. The 'this' keyword in front of the 'number' parameter of the IsPrime() method did the real trick. This makes it an Extension Method.

class Program {
   &nbspstaticvoid Main(string[]args){
   &nbsp   &nbspConsole.WriteLine(5.IsPrime());// true
   &nbsp   &nbspConsole.WriteLine(9.IsPrime());// false
   &nbsp   &nbspConsole.WriteLine(23.IsPrime());// true
   &nbsp}
}

staticclass Extensions {

   &nbsppublicstaticbool IsPrime(thisintnumber){
   &nbsp   &nbspif(number==1||number==2||number==3){
   &nbsp   &nbsp   &nbspreturntrue;
   &nbsp   &nbsp}
   &nbsp   &nbspif((number%2)==0){
   &nbsp   &nbsp   &nbspreturnfalse;
   &nbsp   &nbsp}
   &nbsp   &nbspintsqrt=(int)Math.Sqrt(number);
   &nbsp   &nbspfor(int t =3; t <=sqrt; t = t +2){
   &nbsp   &nbsp   &nbspif(number% t ==0){
   &nbsp   &nbsp   &nbsp   &nbspreturnfalse;
   &nbsp   &nbsp   &nbsp}
   &nbsp   &nbsp}
   &nbsp   &nbspreturntrue;
   &nbsp}
}
Tags: CSharp

READ MORE

My Favorite Visual Studio 2005 and .NET 2.0 features

0 Comments
By Fons Sonnemans, 05-aug-2005
  • C# 2.0
  • Class Designer
  • Unit Testing
  • Code Coverage
  • FXCop Integration
  • Debugging: DataTips, Visualizers and Viewers
  • Refactoring
  • Improved IntelliSense
  • Code Snippets
  • Profiles
  • Strongly-typed resource class generator
  • Improved (not perfect) Windows Forms controls

READ MORE

Binary Compatiblity

0 Comments
By Fons Sonnemans, 22-jul-2005

Microsoft has released a free LibCheck tool that allows you to compare two versions of an assembly, and determine the differences. The tool reports the differences as a combination of 'removed' and 'added' APIs. The tool is limited to looking only at APIs (i.e, it can't check for behavioral changes), and only compares public differences, or changes which are deemed to be 'breaking'. The tool can be used to quickly determine what has changed between one version of your assembly and another, and can help ensure that you won't introduce any breaking changes to clients of your assembly. Instructions and intended use of the tool are described in the 'libcheck tool specification' document with the zip file.

This was a feature I always was missing. VB6 had this, VS.NET didn't. Thanks MS.

READ MORE

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.