Skip to main content

Posts

Showing posts with the label C#

TDD on .NET without Visual Studio

I've been long time user of the NUnit framework (I mean for ages) and I was quite happy with the overall experience. Good performance, lots of tests runners, great integration with Visual Studio, reasonable documentation, the list goes on and on... But lately I've been working mostly on mac and/or linux, and on that space, the experience wasn't that great. I still advocate for TDD (and any kind of automated tests for that matter) but I have to admit that doing TDD with NUnit outside Visual Studio (and without ReSharper support) was quite tedious. While NUnit comes with a console runner, I guess it's there just for CI Servers and not for developers to use. I mean you can use it, but the output is xml... and ... should I say more? ;) So with that problem at hand, I decided to write Contest , a small library that allows me to do TDD on .NET with just an editor and a console . Basic feature set but capable enough to get stuff done. And of course, easy to use from de con...

C# - How about using json files instead of app.config

I think we all agree that working with any kind of xml files is a bit of a pain in the ass. App.config is not an exception to that rule, so I'll show you a little trick that will help you to wipe out those nasty files replacing them for a more sane config file format with almost no changes to your code base.  Thanks to Json.Net, is really easy to read/write C# objects to json files, so if you are starting a new project, use a plain old C# object to store your settings and you are done. But what happens if you wanna replace the way you work with settings in an existing code base? I guess you will try to do it in a way that doesn't requires a whole lotta changes. Well this is one! Keep reading on github And of course, feel free to contact me if you have any question.

Loading large html pages using Awesomium.NET

While there is no limitation in the page size per se, you may experience some weird behavior (errors) when trying to load large html files using the Awesomium WebView LoadHTML method. I been using this framework for a week now and I’m more than satisfied, it’s a great piece of software, but the error message you get when a large page fails to load, it is a bit confusing “the uri is too long” …. I was working with two files, one of them was large, the other was small, but URL length was the same for both, let say foo.html and bar.html . When I load foo.html things work great, but when I try to load bar.html, boom, I got an error saying that something is wrong with URI’s length? here is where I went “WTF, both URIs have the same length!” ;) The thing is that the framework internally is using the .NET Uri class which have a limitation of 65K something characters, if the html that you are trying to load exceeds that limitation (not the url, the html) you will get that excep...

How to get windows.external to work when using Awesomium.NET

Note: There is an updated version of this post right here  (with source code and all stuff) I been doing some experiments with the Awesomium  web browser framework, and after a lot of failing attempts, I finally got windows.external calls working. Awesomium  is a windowless port of Chromium/WebKit. For WinForm developers, an alternative way to embed web pages in your winforms or WPF apps without having to use the WebBrowser control. For a long time, I been using the IE embedded browser to show reports (webpages) in winform apps, with let say…, acceptable results ;), but the thruth is there is a lot of goodness out there into the wild with better support for css, html 5, etc. So I decided to install Awesomium.NET and give it a try. While the API is pretty intuitive, I had some trouble trying to call C# methods from Javascript, a fairly easy thing to do with the classic webbrowser control. This is how things work when using embedded IE Browser ...

WinForms, How to enable sorting for a datagridview that uses linq queries as data source

How to enable sorting for a datagridview that uses linq queries as data source it is a frequently asked question on the web. The short answer is you cannot, but let dig a little deeper. Why the sorting functionality works as expected when you use it on a grid binded to a DataTable and miserably fails when you bind it to an IEnumerable<DataRow>?  A bit of research later… Easy, the DataTable class provides its own implementation for sorting rows whereas IEnumrable<T> does not. I faced this problem building the power grid control, because (to make paging records easy) internally I was using linq queries. As soon as I realized that the datagrid sorting was broken, I started to look on the web an easy way to solve this problem (other than implement my own sorting mechanisms, of course). I found a couple of alternatives but this one  convinced me. This class has a neat way to solve generic sorting with a minimal impact on performance. I’ve tested it sorti...

WinForms, how to implement conditional format for DataGridViews (part 2)

In the previous post I showed you how to implement conditional format functionality for datagridviews using C#, working on conditions that we already know at compile time, which means that those conditions will be written by programmers, not end users. In this post I’ll show you how to deal with user defined conditional formats, which is a more useful thing to do if what you want to provide this kind of functionality. With this new feature the users are going to be able to write conditions in an Excel fashion (formula like, if you will) To implement this feature I’ve upgraded the project that we been using for this series to .NET Framework 4.5 in order to use the latest version of the Roslyn compiler. The Roslyn compiler is the backbone of this feature. If you are interested in Roslyn, I made a series on How to build a DSL on top of Roslyn  that covers some basic stuff of this great piece of software. As in the Roslyn DSL series I'll rely on source to source translati...

WinForms, how to implement conditional format for DataGridViews

Being able to apply conditional formats to datagridviews is a must nowadays, users of our app probably make heavy use of this feature on Excel spreadsheets, and they want it to be present on any "grid alike" screen. I’ll show you two ways to implement this feature the first one is using pre-built formats, or formats specified in C# at compile time (better performance) and  the second one that allows users to add their own conditional formats with formulas written in an excel fashion . In the last case the performance is not good enough for prod., but I’ll be sanding rough edges and hopefully make it work in a near future. To work on this feature, I'm going to use the same datagrid I used in the previous post, which you can download from here . The whole functionality is already implemented, so I'll just show you how to use it. The only thing you have to do is register the conditional format and the component will take care of the rest.  To register a...

WinForms, paging the DataGridView the right way

I know this may sound like old history, but in the enterprise world there is still a lot of WinForms development. Just a couple of days ago, I had to implement a custom DataGridView capable to work over a butt load of data (100K+ records) and keep responses times acceptables. I thought paging will be a good way to go, and as WinForms is pretty old nowadays, I supposed it will be easy to find a couple examples on the web. While in fact I found examples, all of them were incompletes and/or they wouldn't perform well in real world apps... So I decided to roll my own component and post it online. Hopefully, someone else will find it useful ;). The bread and butter of this solution relies on LINQ and deferred execution. As LINQ takes care of all complicated work, it was quite easy to implement. This component also supports conditional format, sorting and some search capabilities, but in this post I will concentrate on paging only (I'll cover the rest of the features in f...

How to create MS Word documents from Office templates using C#

The OpenXML SDK allows you to do pretty much anything you want with office files such as Excel, Word, etc… While many people like this library, I found it complex, unintuitive and poorly documented, not to mention the awful xml format that uses under the hood to represent the documents, styles, etc. So I decided not to use it and build my own solution. If you, like me, don’t like that library, you will find in this post an alternative approach to build word documents from templates using c#. A neat trick to work with Office is to use the macro recorder to understand how things work. The macro recorder allows you to start a macro, do something by hand, stop it, and then take a look at the generated VBA code. Once you do this, you are pretty much set. This is how it looks the template I’am going to use. Note: save the file as a Word template (.dotx) This is the code to create Word documents from C#: By running the code, you should get a document that looks...

Printing html using the embedded web browser control

In this post I’ll try to answer some questions about the web browser control and provide some workarounds for known issues involved in the printing process. I'm assuming that you have some experience with the web browser control and basic knowledge of COM and hosting APIs. So I’m not going to cover those topics. At the bottom of this page I’ve added the links to download a small library I wrote that takes care of printing HTML and a demo app so you can try it out without having to write any code by yourself. Using the code The HtmlPrinter class will allow you to print html from an URL or just passing the html as string, you can also specify the title and the number of copies you want to print. The code may look something like this: Now that we know how to use the API let get answer some questions. Why my app crashes when I try to print multiple copies of a page? Well, apparently when you send a lot of print commands to the web browser control, ther...

Deferred execution in C#

A lot of times, fellow developers ask me about yield return keyword and what this keyword is for. This keyword allows us to iterate over enumerable collections in a more efficient way. Basically what does is tells the compiler do not execute this code unless is strictly necessary (for this reason it’s also called lazy evaluation). I wrote a little sample that shows the keyword in action. The purpose of this code is to convert a DataTable into a Collection of objects. Maybe the algorithm is not the best way to accomplish the job but clearly shows how yield return works. To see the advantages of lazy evaluation we first should take look at how the code will work if we don’t use it. Here I convert a DataTable that contains four rows into a collection of objects and take the first element from the result. This version of convert works eagerly. And the result is: As we can see this in the output window, this version of the convert method walks thru the whole rows collection converting ea...

How to copy event handlers from one control to another

Today at work, one of my partners in crime were working on a crazy customer requirement and ask me if I think that would be possible to copy event handlers from one control to another at run time and keeps things working correctly. I said yes, I think it’s possible and must be easy too, let’s take a look… After a couple of minutes, we figure out that the Control class doesn´t expose any public method or property to get access to the collection of delegates attached to the control’s events. I did a fair amount of web research and found a lot of partial solutions but none that fit our needs. So I decide to roll my own solution, post it, and may help some who is facing the very same problem. Normally when we are working with events, we hook them up with handlers, writing a piece of code like this: textBox1.TextChanged += (s, e) =>  MessageBox .Show( "Hi there from handler 1" );   It’s also common to attach more th...