Skip to main content

Posts

Showing posts from October, 2013

C# Tutorial - How To Use Custom Cursors [Intermediate]

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). Changing the cursor on a WinForms control is extremely easy, as long as you are only trying to change it to one of the other standard cursors. To do that, all you need to do is set the Cursor property on your control to one of the cursors on the Cursors object. However, using a cursor of your own can be a little more difficult. There are a couple ways to use your own cursors, and they all eventually create a new Cursor object. The simplest way is to just load a cursor file (you know, the ones with the ".cur" extension) that you created. The constructor for the Cursor can take a file path to do just that: Cursor myCurso...

C# Tutorial - Using The ThreadPool [Intermediate]

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. A thread pool takes away all the need to manage your threads - all you have to do is essentially say "hey! someone should go do this work!", and a thread in the process' thread pool will pick up the task and go execute it. And that is all there is to it. Granted, you still have to keep threads from stepping on each other's toes, and you probably care about when these 'work items' are completed - but it is at least a really easy way to queue up a work item. In fact, working with the ThreadPool is so easy, I'm going to throw all the code at you at once. Below is a pretty simple test app that gives 5...

C# Tutorial - Deep Cloning A Connected Graph Of Objects [Intermediate]

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#. First off, you might be wondering what I mean by "deep copy" or "deep clone." Well, the common form of copy in a language is shallow copy - in fact, C# has a standard interface for it, called ICloneable . The main difference between shallow and deep copy is that shallow copy does not make a duplicate of anything underneath the top level object. For instance, say you have an object 'A' which has a reference to an object 'B'. If you made a shallow copy ...

C# Tutorial - Anonymous Delegates And Scoping [Intermediate]

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. Take a look at the block of code below, where we are creating (and then returning) a set of delegates that will print out a number to the console. What do you think will get printed out when each delegate in the returned array gets called? public delegate void NoArgDelegate (); static NoArgDelegate [] InitializeOne () { NoArgDelegate [] array = new NoArgDelegate [ 5 ]; for ( int i = 0 ; i < array . Length ; i ++) array [ i ] = delegate () { Console . WriteLine ( i ); }; return array ; }   At first glanc...

C# Silverlight - Using Sliders [Beginner]

Being the month of Silverlight, I decided to get off my PHP and JS butt, and learn something new. I am incredibly new to the whole silvery-light thing, but luckily with Visual Studio on your side, it is pretty easy to get things going. I am also using VS2010, which makes the whole experience a grand opening of new stuff for me. So to begin my foray into Silverlight, I did something easy, but slightly useful at the same time. What I have made is a set of three sliders that represent a RGB value, which in turn changes the color of some text. It is some pretty short code, but it does showcase how to change the color of TextBlock text, which took me a few minutes to find. [silverlight width="400" height="300" src="SilverlightApplication.xap" border="true"] To begin with, lets get the XAML out of the way: <UserControl x:Class = "SilverLightColorSliders.Page" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml...