Switch On The Code RSS Button - Click to Subscribe
Oct
2

WPF Tutorial - Getting the DoubleClick Event

In WinForms, everything that derived from System.Windows.Forms.Control had the MouseDoubleClick event - and not suprisingly, in WPF, everything derived from System.Windows.Controls.Control also has a MouseDoubleClick event. But there is a key difference - in WinForms, almost everything that gets displayed on the screen is derived from System.Windows.Forms.Control in some way - while in WPF, there are a number of items you can put on the screen that do not actually derive from System.Windows.Controls.Control. So today we are going to take a look at how to get double clicks when you are not derived from System.Windows.Controls.Control.
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | No Comments »

Sep
23

C# Snippet Tutorial - The ?? Operator

Just the other day I came across a C# operator that I found particularly useful and decided to share it with everyone here at SOTC - the ?? operator. The briefest explanation is this: ?? is used a lot like a conditional (:?), except it will return the thing on the left if it’s not null, or else it will return the thing on the right.
 
Read More »

Posted in C#, All Tutorials | No Comments »

Sep
16

WPF Tutorial - Using An ItemsPanel

One of the most useful things about WPF is how flexible all of the built in controls are. Don’t like how the tabs look on a tab control? Restyle them! Need to have images inside your combo box? Sure, no problem! This flexibility extends deep into a number of controls, and today we are going to take a look at controlling layout in an ItemsControl by setting the ItemsPanel.
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | No Comments »

Sep
9

C# Dialogs, Part 2 - Custom Dialogs

Ok, so in Part 1 we went over the MessageBox that C# offers. As simple and easy as it is to use MessageBox, sometimes you need something more customized to your needs. Although MessageBox is extremely simple to use, it is almost as easy to create your very own dialog and use whatever you want.
 
Read More »

Posted in C#, All Tutorials | 2 Comments »

Aug
28

C# Tutorial - Object Finalizers

Recently, in a tutorial about Weak References in C#, we talked a bit about garbage collection and how the garbage collector works in .NET. I figured since we already started addressing that stuff, there is no reason not to delve deeper. And so, today we are going to take a look at how object finalizers work in C#.
 
Read More »

Posted in C#, All Tutorials | 2 Comments »

Aug
27

C# Dialogs, Part 1 - MessageBox

We have all seen them, and most of the time they mark something bad. They are dialog boxes and not only have they been the bane of all computer users since the GUI was invented, but they are really a key to any software project. Whether you are making a simple text editor, or you programming a time viewing machine, chances are you at some point going to need a dialog box. Luckily, with C# adding one is super easy.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

Aug
13

C# Tutorial - Weak References

We all know (hopefully) that C# is a garbage-collected language. In general, what this means is that we as programmers don’t need to free our own memory - the garbage collector will free that memory for us once it is no longer being referenced. Now, of course, garbage collection is a lot more complicated than that, and writing a good garbage collector is actually a relatively hard problem. And the fact that writing a perfect garbage collector is probably impossible is the reason why things like C#’s Weak Reference object exist.
 
Read More »

Posted in C#, All Tutorials | 2 Comments »

Aug
6

C# Tutorial - Switch Statement Games

Today, we are going to take a look at Switch on the Code’s namesake - the switch statement. More specifically, we are going to take a look at some of the lesser known behaviors of the switch statement in C#, hopefully giving you some new syntactic sugar to use in the process.
 
Read More »

Posted in C#, All Tutorials | 4 Comments »

Jul
29

The C# Preprocessor - An Overview

Did you know that C# actually does have a preprocessor? Probably the most common C# preprocessor directive that you see is the #region directive, and that doesn’t even have any impact on the code. Now the preprocessor for C# is nowhere near as powerful as the ones for C and C++ (for instance, no macros), but it does let you do a couple of handy things.
 
Read More »

Posted in C#, All Tutorials | 4 Comments »

Jul
28

C# Snippet Tutorial - Comparing IP Addresses

Comparing two IP Addresses is something that has caused me frustration on more than one occasion. Simply put, I always assume the default == operator will tell me if the addresses contained in the objects are equal, and as always, it does not.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | No Comments »

Jul
25

C# Snippet Tutorial - The Web Browser Control

Ok, so you may be thinking to yourself “Why do I need a browser control when I have Firefox or IE?”. Well, considering it has all the power of Internet Explorer, you can use it for lots of things. You can use it to show a website in your application or even just load in an HTML file from the local computer. The greatest thing is that it is quite easy to use and control.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | No Comments »

Jul
22

WPF Snippet Tutorial - Aligning ListView Items

WPF is powerful. So powerful in fact, that sometimes it’s hard to find styles and settings to make it do what you want. Aligning ListView items was one such example for me. This snippet tutorial will show you how to use a style to vertically and horizontally align the contents of ListView cells.
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | 3 Comments »

Jul
21

WPF Tutorial - Using MultiBindings

If you’ve touched WPF at all, you’ve probably realized that bindings are an extremely important piece of the framework. We’ve talked about WPF bindings a number of times here at Switch On The Code (most recently in a post where we talked about Binding Converters) - and yet we have just barely scratched the surface of the world of bindings. Today, we are going to take a look at MultiBindings.
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | No Comments »

Jul
16

WPF Tutorial - How To Use A DataTemplateSelector

DataTemplates are an extremely powerful part of WPF, and by using them, you can abstract all sorts of display code. However, there are times when they fall short - and initially when I was learning WPF I was disappointed by that. For instance, you only get to set one DataTemplate on an items control, and while that made sense, it felt limiting. What if I wanted to use different templates depending on the content of the item? Do I have to build all that logic into a single data template?
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | 1 Comment »

Jul
15

WPF Tutorial - Binding Converters

I’m going to come right out and say it, binding converters are one of the nicest pieces of WPF I’ve run across so far. Simply put, they provide a translation between your binding source and destination. The most common use I’ve found is when I’m binding to user interface elements, because on many occasions I don’t store something in a data member that can just be stuck on the screen. This tutorial will provide an introduction to what binding converters are and how to use them.
 
Read More »

Posted in WPF, C#, All Tutorials | 1 Comment »

Jul
9

C# Tutorial - Using The Built In OLEDB CSV Parser

So, a while ago, one of the other writers here wrote a small tutorial on parsing simple CSV files in C#. It mostly just showed off the string split method, and only worked on really simple CSV files - no quoted fields, etc. Well, we got a comment asking about that, so today I sat down thinking I would write up a more robust parser. But as I read through the RFC that describes CSV files, I thought to myself, am I suffering from NIH (not invented here) syndrome? Do I really need to write a full CSV parser?
 
Read More »

Posted in C#, All Tutorials | 13 Comments »

Jul
1

C# Tutorial - Method Attributes And Reflection

Today we are going to take a look at a deep and yet often underused part of C# - method attributes. First we are going to go through what they are and take a look at the built in attributes, and look at how to poke at them through reflection (which we have taken a quick look at before, in Some Notes On Invoking). By the end of the tutorial, through, we will be making our own custom attributes - and we will be using those attributes to make our code more dynamic and extensible.
 
Read More »

Posted in C#, All Tutorials | 3 Comments »

Jun
25

Working With The WPF Dispatcher

Proper use of threads can greatly increase the responsiveness of your WPF applications. Unfortunately, you can’t update any UI controls from a thread that doesn’t own the control. In .NET 2.0, you used Control.Invoke(). Now, we’ve got something similar but more powerful - the Dispatcher. This tutorial will explain what the Dispatcher is and how to use it.
 
Read More »

Posted in WPF, C#, All Tutorials | 3 Comments »

Jun
24

WPF Tutorial - Using WPF In WinForms

A little while back, we did a tutorial on how to embed WinFoms controls inside of WPF application (you can read all about it here). Today, we are going to flip that on its head - we are going to take a look at how to embed WPF controls inside of a WinForms application.

 
Read More »

Posted in WPF, C#, All Tutorials | 5 Comments »

Jun
20

C# Tutorial - XML Serialization

A while ago we posted a tutorial on how to serialize objects to a binary file. While this is very useful, unfortunately the resulting file is not very human readable. In this tutorial, I’m going to demonstrate how to serialize your own objects to and from an XML file.
 
Read More »

Posted in C#, All Tutorials | 15 Comments »

Jun
6

Silverlight 2 and Twitter - A Simple Example

It might be a surprise to see me (The Fattest) writing about Silverlight, but I have had some fun playing around with it recently. So I decided I would write up a quick tutorial on how to build a Silverlight widget that shows the last five tweets (messages) from twitter. Now be forewarned I am not a C# expert, two other people at SOTC are, nor am I a Silverlight expert. I do however really enjoy playing around on twitter (The Fattest’s Twitter Account) and with RIAs so I think I am qualified enough. :)

 
Read More »

Posted in Silverlight, C#, All Tutorials | 1 Comment »

May
20

WPF Tutorial - Using WinForms In WPF

So we all know that WPF is awesome, but it is still a very young framework. And so, sometimes, it doesn’t have everything that we might want or desire. For instance, there are a number of controls in WinForms that don’t exist in WPF - and they come in handy once in a while. What we are going to look at today is how to use WinForms controls inside of WPF - a very easy and almost painless process, in my opinion.
 
Read More »

Posted in WPF, C#, All Tutorials | 1 Comment »

May
13

C# Snippet Tutorial - Get File Listings

Ok, so can you even begin to tell me how many files you have on your computer? I can’t either. There are so many reasons you could need to get file listings in C#, and thanks to the System.IO namespace, doing so is just about as easy as including the namespace itself. Really all you need to do is create a variable, then a loop.

 
Read More »

Posted in C#, All Tutorials | 2 Comments »

May
12

Getting Image Metadata with C#

Everyone has images. Maybe you’re a photographer, a graphic artist, or you simply had a really awesome vacation, but chances are you have a lot of them. What you may or may not know is that every image contains extra information about itself: when it was taken or created, the size of the image, and sometimes even details such as the focal length and type of lens used. Almost every image has some sort of extra data, called metadata, and using C# we can write a function or even a class to help us get this metadata from the image.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

May
8

C# Snippet Tutorial - Dictionary Collections

So, we have all used arrays, and even sometimes a nice HashTable to get things sorted and organized. There are so many uses for an array, and even more for a nice HashTable. But, have you ever noticed that a HashTable is all objects? The Key in each Key/Value Pair is an Object, along with the value itself. But what happens if you don’t need something so generic as an object? Well, you can get really specific and really crazy with a collection called Dictionary.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 2 Comments »

May
7

Visual Studio - How To Create Item Templates

Visual Studio actually has a very powerful wizard and template system, which when used correctly, can automate large amounts of tedious work. Today we are going to take a look at how to create Item Templates for Visual Studio, which is actually extremely simple to do.
 
Read More »

Posted in Visual Studio, C#, All Tutorials | 1 Comment »

May
6

C# Snippet Tutorial - Custom List Sorting

The List is one of my favorite additions to the .NET framework. It has built-in mechanisms to perform efficient sorting and searching. In this snippet tutorial, I’m going to demonstrate how to customize the sorting routines used by the List class.
 
Read More »

Posted in C#, All Tutorials | 6 Comments »

May
2

C# Snippet Tutorial - Modify a Cell’s Selected Text

In this short tutorial, I’m going to show you how to change the text selection in a DataGridViewCell. There’s lots of reasons why someone would want to do this, like if someone enters something incorrectly, maybe you want to select all the text so they can enter something else.
 
Read More »

Posted in C#, All Tutorials | 2 Comments »

Apr
29

C# Tutorial - Binding a DataGridView to a Collection

This tutorial is kind of a follow-up to my previous tutorial about binding a DataGridView to an Access database. In this tutorial, I’m going to demonstrate how to bind a DataGridView to a .NET collection.
 
Read More »

Posted in C#, All Tutorials | 11 Comments »

Apr
28

Taking Some Screenshots with C#

Here at Switch on the Code we do some pretty neat things with all sorts of code, and all this requires a ton of screenshots to help explain all the craziness. But besides having that good ol’ fashion “Print Screen” button, we can actually use C# to take a screenshot for us. The best part of all is that it is only a couple lines of code. So lets just go ahead and dive right into it.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

Apr
25

WinForms Tutorial - Manage Your Own Double Buffering

Double buffering in WinForms, especially .NET 2.0 and up, is really nice. It gets rid of almost all flicker on your control, and all you have to do to turn it on is flip a simple property (you set the property DoubleBuffered to true. However, by making it so easy, .NET took away a lot of control over the double buffering process - and so in a select few instances, it is better to manage the double buffering of your control yourself. Today, I am going to walk through how to do that.
 
Read More »

Posted in C#, All Tutorials | 6 Comments »

Apr
22

C# Snippet Tutorial - Using StringBuilder

Everyone loves strings, you use them all the time in writing code (especially if you are writing any code that needs to interact with the user). And C# makes it really easy to work with strings - the useful operators just work (like ‘+’ and ‘+=’) and every object has the extremely useful ToString function call. In fact, strings are so easy to work with in C#, one might almost say that they are too easy to work with.
 
Read More »

Posted in C#, All Tutorials | 5 Comments »

Apr
17

Building a Simple CSV Parser in C#

So you are sitting around and you somehow have 100 Comma Separated Value files (CSV) and you are not quite sure exactly what the best way to read them is. Well, if you are using Visual Studio and C#, you are in quite a bit of luck, because you can read a CSV file quite easily. With one very small function you can spit out a list of values, separated conveniently by rows and columns. Then you can take this list and use it however you want, perhaps in a DataTable or GridView object.
 
Read More »

Posted in C#, All Tutorials | 11 Comments »

Apr
11

C# Tutorial - Extension Methods

Today, we are going to take a look extension methods, which is a language feature introduced to C# in .NET 3.0. Extension methods are a piece of syntactic sugar that allow you add new functionality to a class that you don’t have direct access to. Sound cool yeah? Cause it is.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

Apr
9

WinForms - Painting On Top Of Child Controls

If you have worked with WinForms and ever needed to do some custom painting, I’m sure you have had the following happen. You write up some painting logic, and run the program, only to find that all or part of the painting is not visible. You scratch your head for a moment and then go “Duh! There’s another control on top of what I’m trying to paint!”, and then go off and try and figure out a different solution. Today I’m going to show you a way of getting around this problem in certain situations.
 
Read More »

Posted in C#, All Tutorials | No Comments »

Apr
3

WinForms - Accessing Mouse And Keyboard State

Generally, Windows programs are very event based systems - a user presses a key, the key down event is fired with the information on what key was pressed, and the application reacts accordingly. There are sometimes cases, however, when the program needs to know the current state of the mouse/keyboard, but we don’t have a handy event that with info about keys down or mouse buttons pressed. Now you could always maintain this state yourself in some sort of data structure and updating it on key/mouse up and down events - but that is a lot of work, and, as it turns out, unnecessary. There are already functions built into .NET that let you access most (but not all) of this info - and in the case where there is not a built in function, the answer is just a simple interop away! And that is what we are going to take a look at today.
 
Read More »

Posted in C#, All Tutorials | 2 Comments »

Mar
18

WinForms - Using Custom Cursors With Drag & Drop

A week or so ago, a comment was left on the WinForms Custom Cursors tutorial asking about how to change the cursor during a drag and drop operation. This is a great question, because, well, its not at all obvious how to do. The first time I needed to do it, it took me quite a while to find out how - and I had to piece the answer together from a number of different sources.
 
Read More »

Posted in C#, All Tutorials | 6 Comments »

Mar
17

.NET 3.5 Adds Named Pipes Support

Leave it to Microsoft to make all my hard work worthless. A while ago, I posted a tutorial on how to use named pipes in C# and .NET. Back then it took a lot of hard work and a lot of Windows API calls to get named pipes integrated into a .NET 2.0 application. Now, thanks to .NET 3.5, named pipes are as easy as importing System.IO.Pipes.
 
Read More »

Posted in C#, All Tutorials | 4 Comments »

Mar
14

WPF Tutorial - How To Use Custom Cursors

So a while back, I did a tutorial on how to do custom cursors in WinForms. In that post I said that at some point in the future, I would write a post on how to do custom cursors in WPF - and here we are! A lot of the code used today is based off of the code from that previous tutorial, so if you haven’t read it, I would go do so before you continue.
 
Read More »

Posted in WPF, C#, All Tutorials | 1 Comment »

Mar
13

Creating a Textured Box in XNA

Microsoft has undoubtedly made some top notch developer tools over the years. Besides Visual Studio, .Net Languages, and the new Sliverlight, Microsoft has also jumped into gaming. We all know and love the Halo series, but what is slightly lesser known are the XNA code libraries. Microsoft has made some pretty sweet games, but with the still fresh XNA you have the chance to grab the reins and make your own games.
 
Read More »

Posted in XNA, C#, All Tutorials | 3 Comments »

Mar
7

WPF Tutorial - Introduction To Dependency Properties

So I was going to dive right in and do a part 2 on the WPF ListView tutorial from last week, but as I was writing the code I realized that a lot of it relies on some new and very different constructs that WPF provides to developers. Two of these are deep enough topics on their own that I thought it would be a good idea to give an introduction to them before I dived back into the ListView stuff. So today we are going to talk about Dependency Properties, and in a future tutorial I will talk about how binding works in WPF.
 
Read More »

Posted in WPF, C#, All Tutorials | 5 Comments »

Mar
4

Introduction to LINQ - Simple XML Parsing

Every programmer has had to parse an XML file. If you’re trying to tell me you haven’t, then you’re lying and you just don’t remember it. You probably shouldn’t even call yourself a programmer.

If you don’t remember the history of reading an XML document, here’s the Cliffs Notes version. In the beginning there was line-by-line parsing. It was hard, error prone, and usually sucked. After a little while of this, light starting shining onto our dark corner of the world. .NET introduced the XmlTextReader class, and things were good - for a while. And then, as if Microsoft heard the prayers of a million programmers, LINQ was integrated into .NET 3.0 and again, things are good.
 
Read More »

Posted in LINQ, C#, All Tutorials | 4 Comments »

Feb
28

WPF Tutorial - Using The ListView, Part 1

Many of the controls in WPF have a downright dizzying array of capabilities and features, due in large part to the composibility of all the components. The ListView control is a great example of this - the possibilities are almost endless. This series of tutorials on the ListView will hopefully make the space of possible options seem not quite as daunting.
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | 8 Comments »

Feb
22

C# Snippet Tutorial - Using the FileSystemWatcher Class

There are several reasons why an application might want to monitor for changes to specific files or folders. .NET’s FileSystemWatcher class makes doing this incredibly easy. This C# snippet tutorial will introduce you to the FileSystemWatcher class and walk you through the basic usage.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 1 Comment »

Feb
18

WPF Tutorial - Creating A Custom Panel Control

The world of WPF gives you an extreme amount of flexibility right off the bat - with things like styles, control templates, and the composability of almost anything, at first it seems like everything is right there at your fingertips. And while there is a lot immediately accessible, there are still cases where you have to get down into the nitty-gritty. Today we are going to take a look at how to create a WPF custom control - more specifically, a custom panel.The available panels in WPF are great (perhaps we will have a tutorial on how to use them all at some point in the future). But maybe you have a very specific need, and none of the panels quite work the way you want?
 
Read More »

Posted in WPF, XAML, C#, All Tutorials | 7 Comments »

Jan
30

C# Tutorial - How To Use Custom Cursors

Custom cursors are something that you don’t need to use very often, but when you do need them, they can make a huge difference in the usability of your program. So today we are going to take a look at how to use your own custom cursors in C#/WinForms applications (don’t worry, WPF aficionados, we will take care of you at a later date).
 
Read More »

Posted in C#, All Tutorials | 8 Comments »

Dec
18

C# Tutorial - Using The ThreadPool

Ah, good old multi-threading. Always fun, and often a source of headaches. With C# and .NET, those headaches don’t go away, but there are some nice wrappers that make working with threads a little bit easier. Today we are going to take a look at how to use C#’s ThreadPool - which is probably the simplest way to make a multi-threaded C# app.
 
Read More »

Posted in C#, All Tutorials | 8 Comments »

Dec
6

C# Snippet Tutorial - Performance Timers

Every once in a while, there comes a time when some chunk of code that you have written isn’t performing quite as well as you think it should. And when that time comes, you need to have a way of figuring out what parts of your code are causing those performance problems. For the heavy duty cases, there are full blown code profiler programs out there, but for the simpler cases you probably just want to time some segments of code manually. And that is what we are going to take a look at today.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 6 Comments »

Dec
4

C# Snippet Tutorial - Static Constructors

I’m going to guess that pretty much everyone who reads this blog knows about constructors and how they work. And I bet everyone knows about static variables and functions as well. But did you know that there is such a thing as a static constructor in C#? Yup, a static constructor. And that’s what we are going to take a look at today.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 8 Comments »

Nov
26

C# Tutorial - The => (Lambda) Operator

C# in .NET 3.0 got a number of handy new language features, but the one I’m enjoying the most at the moment is the addition of the => operator (otherwise know as lambda). While this operator does not add any new functionality to the language, it is a great piece of syntactic sugar.
 
Read More »

Posted in C#, All Tutorials | 2 Comments »

Nov
22

C# Snippet Tutorial - The Params Keyword

We are going to take a quick look at another C# keyword today - the params keyword. This keyword is another one of those not well known keywords in C#, but it makes life a lot easier in certain situations - and code a lot cleaner. What does the params keyword do, you ask? Well, lets take a look.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 5 Comments »

Nov
19

C# Tutorial - Deep Cloning A Connected Graph Of Objects

Every once in a while when programming, you might end up having to make a deep copy of a bunch of objects. Depending on the complexity of the data structure, this can be really easy or a real pain. What we are going to take a look at today is a pattern for making deep copying (or deep cloning) easy to do in C#. The techniques I am going to show can be applied to any object oriented language (there is nothing real specific for C#), so hopefully this can help you out even if you never use C#.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

Nov
7

C# Tutorial - Visual Studio Code Snippets

A cool feature that Visual Studio has is the concept of a “code snippet.” You know those chunks of code that you need to type over and over again throughout your code? Perhaps your logging calls, or even more mundane than that, the generic code for try-catch blocks. Well, the folks behind Visual Studio came up with an idea to help keep that concept to a minimum - code snippets. Essentially, it is like having that repetitive code always on your clipboard, ready to “paste” into your code - except it is way more powerful than just copying and pasting code.
 
Read More »

Posted in C#, All Tutorials | 6 Comments »

Oct
30

C# Tutorial - Simple Threaded TCP Server

In this tutorial I’m going to show you how to build a threaded tcp server with C#. If you’ve ever worked with Window’s sockets, you know how difficult this can sometimes be. However, thanks to the .NET framework, making one is a lot easier than it used to be.
 
Read More »

Posted in C#, All Tutorials | 57 Comments »

Oct
19

C# Tutorial - Event Accessors

Events in C# can be weird little beasts. We have talked about how to use and create them before, but really that article only scratches the surface. At first events seem like a special construct, all by themselves in the C# world. But then, after a while (And after you learn about delegates), you realize that all they really are are multicast delegates. But then, even longer after that, you realize that while it may be just a multicast delegate underneath, there are a couple things on top that make the C# event unique.
 
Read More »

Posted in C#, All Tutorials | 3 Comments »

Oct
10

C# Tutorial - Font Scaling

We have looked at fonts in a previous C# tutorial, in Auto Ellipsing in C#. But whereas in that tutorial a string gets cut off if there is not enough room, in this tutorial we are going to take a look at how to scale the font such that the text will fit in whatever you need it in. As a side note, look for a revisit on ellipsing in C# at some point in the future - as one of our readers sent us some interesting info about the performance characteristics of the API calls we used in that tutorial.
 
Read More »

Posted in C#, All Tutorials | 6 Comments »

Sep
17

C# Snippet Tutorial - How to get an Enum from a String

Earlier, we posted a tutorial on how to get an enum from a number. In this tutorial we’re going to show you how to use a similar technique to get an enum from a string.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 4 Comments »

Sep
13

C# - Some Notes On Invoking

One nice thing about C# is that you can be holding a string in your hand, and get your fingers on the method or property that is actually represented by that string - using Reflection and Invoke. This is not unique to C# by any means, and in fact in a number of other languages it is a lot easier to do (such as PHP), but it is there and you can use it. But one thing to remember is that Invoke is a costly operation - you don’t want to be doing it when you don’t have to be.
 
Read More »

Posted in C#, All Tutorials | No Comments »

Sep
12

C# Tutorial - The Readonly Keyword

C# has a ton of keywords, and it is sometimes hard to keep track of them all. One keyword that often gets lost in the shuffle is the readonly keyword, often because many people just group it with the const keyword and leave it at that. Well, the readonly keyword deserves better than that - and so here today we are going to talk about what exactly it does, how it differs from const, and why you would ever want to use it at all.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

Sep
10

C# Tutorial - Dealing With Unhandled Exceptions

It’s a fact of life that all applications have bugs- even after strenuous bench testing, automated testing, and piles and piles of test plans. When you release software into the wild, something is going to go wrong, somewhere. So it is nice to have an underlying architecture in your program so that when something does go horribly, unavoidably wrong, there is at least some record of what happened - so you can start tracking down and reproducing it. Today we are going to talk about an extremely simple way to do just that in C#.
 
Read More »

Posted in C#, All Tutorials | 5 Comments »

Sep
5

C# Tutorial - Binding a DataGridView to a Database

In this tutorial, I’m going to show a fairly simple way to bind a .NET DataGridView to a database. The form designer has some support for data binding, but I found doing it without the form designer is a little easier to understand and implement. Also, when I’m developing a desktop database application, my database schemas are rarely 100% defined, and the form designer doesn’t easily support changes to the database.
 
Read More »

Posted in C#, All Tutorials | 41 Comments »

Sep
3

C# Tutorial - Anonymous Delegates And Scoping

Anonymous delegates in C# are extremely useful, but in order to use them correctly, it is important to understand scoping in C#. Just the other day I came across a situation in which the behavior I expected was completely different from what actually happened - because it was all happening in the wrong scope. So, I thought if it confused me, I should throw it up here as a tutorial, because I’m sure it has confused other people.
 
Read More »

Posted in C#, All Tutorials | No Comments »

Aug
31

C# Tutorial - Convert a Color Image to Grayscale

We’ve done a few tutorials about image editing with C#. We’re going to continue the series with a tutorial on how to convert a color image to black and white. This tutorial will show three different ways to convert an image to grayscale - starting with the easiest and slowest.
 
Read More »

Posted in C#, All Tutorials | 13 Comments »

Aug
27

C# - Creating Rounded Rectangles Using A Graphics Path

Do you ever do UI development in C#? Do you know what a GraphicsPath is, or how to use it? No? Well, then this tutorial is for you. We will be covering how to create a graphics path and use it, by coding a way to easily create and draw rounded rectangles. When we are done, you will have a set of functions for creating rounded rectangles, and hopefully a new way to think about certain types of custom painting in C#.
 
Read More »

Posted in C#, All Tutorials | 8 Comments »

Aug
23

C# Snippet Tutorial - How to Draw Text on an Image

In this snippet tutorial we’re going to be adding on to our series of C# image editing tutorials. This tutorial is going to provide the code required to do basic text drawing including anti-aliasing and text aligning.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 9 Comments »

Aug
21

C# Snippet Tutorial - Editing the Windows Registry

In this tutorial, I’m going to go through the code required to edit the Windows registry using C#. This will include creating new keys and values as well as modifying existing ones.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 3 Comments »

Aug
16

C# Snippet Tutorial - Custom Event Handlers

Every Control in C# is full of events like MouseButtonDown and KeyDown, but what happens when you want an object to fire an event that isn’t already built in? This snippet tutorial will go through all the code required to create your own events and custom event handlers.
 
Read More »

Posted in C#, All Tutorials | 23 Comments »

Aug
1

C# Snippet Tutorial - How to Get an Enum from a Number

To continue our snippet tutorial series, here’s a quick tutorial on how to convert an integer to an enum. This is useful when you’ve got a number, either from a file or transfered over a socket, and you want to convert that number to an enum.
 
Read More »

Posted in Snippet Tutorials, C#, All Tutorials | 4 Comments »

Jul
30

Getting Started With Silverlight

Since the release of Silverlight 1.1 alpha refresh and Visual Studio 2008 beta 2, I decided to start a series of tutorials for Microsoft’s Silverlight. If you haven’t heard of Silverlight, I strongly suggest checking it out. It’s a new web technology developed by Microsoft meant to compete with Flash and Flex.
 
Read More »

Posted in Silverlight, C#, All Tutorials | 4 Comments »

Jul
26

How To Auto Ellipse Text in C#

A while back we posted a tutorial on how to auto ellipse text in javascript, and it seems that people find it useful, because it gets a good amount of search traffic. One thing we did notice, however, is that a lot of people seem to end up there when they are actually searching for how to auto ellipse text in C#. And while you could implement the algorithm discussed on the javascript tutorial in C#, it is by no means the easiest way to ellipse strings in C#.
 
Read More »

Posted in C#, All Tutorials | 7 Comments »

Jul
25

Interprocess Communication using Named Pipes in C#

Named pipes are a great way to communicate between multiple processes. What’s makes them even better is that the processes don’t have to share the same language. If you’d like detailed information about named pipes, MSDN has a fairly in depth article. This tutorial is going to demonstrate how to implement communication over named pipes using C# and Interop Services.
 
Read More »

Posted in C#, All Tutorials | 40 Comments »

Jul
17

C# Operator Overloading - The Crazy Side - Part 2

Welcome back to the world of psychotic things you should never do if you want readable code! And in this particular case, I’ll be talking about what happens in C# when you mix operator overloading with inheritance and polymorphism. If you need a refresher on C# operator overloading, you should check out part 1 of this tutorial, at Overloading Part 1. Now, without further ado, lets jump right in.
 
Read More »

Posted in C#, All Tutorials | 1 Comment »

Jun
22 </