2-way communication with Winforms

Today one of my colleagues asked me if it was possible for 2 Windows Forms to communicate with each other in .Net 3.5

Of course it is, and it is easy too.
So I made him an example that showed an easy way to do it.

I’ll try and explain it.
Continue reading


My ASP.Net intellisense was gone!

I use Visual Studio 2008 at work on a remote terminal server.
This server doesn’t have enough disk space on its C drive, and VS2008 likes to place a lot of ReflectedSchemas there.
It got to the point where I has some 4GB of ReflectedSchemas and this was too much, so I deleted them all (nice and definitive) and things were great again.

A couple of weeks later the exact same thing happened again, so I deleted them again, this time though I noticed that my Intellisense for the standard ASP.Net controls (like ) was gone! Still had it for other (custom) controls and HTML tags and everything, just not ASP.Net controls.

Today finally I decided to look into it (2 or 3 months later).
I got a tip to try devenv /ResetSettings, which didn’t help.
One person suggested you change the tag prefix, which also didn’t help.
I also tried copying someone else’s ReflectedSchemas, which still didn’t help, though this could be explained perhaps by the fact that I didn’t overwrite the entries.xml, I don’t know.
After this I tried deleting the ReflectedSchemas again, this time making 100% sure that I had closed devenv.exe. This fixed it…

So now I am enjoying my ASP intellisense once again.

An IComparer

In .Net you can very easily create a special class to handle comparing of (custom) objects. All you have to do is implement the IComparer or IComparer interface and implement it’s one simple function: Compare.

So write a class that implements it:

public class MyComparer : IComparer<MyObj>
public int Compare(MyObj x, MyObj y) {
// If, for example you're working with an int:
return x.IntProperty.CompareTo(y.IntProperty);

// If, perhaps, it is a string:
return string.CompareTo(x.StringProperty, y.StringProperty)

The value you return must be negative if x is less then y, 0 if x is equal to y or greater then 0 if x is greater then y.

This can of course be used with things like MyCollection.Sort(new MyComparer()), really handy if you have strange objects.

My Reflection

The other day I had some trouble with ASP.Net. I was saving a class I’d written into the ViewState (I did this a long time ago, I know now that that might not be the best idea) and it was giving me errors saying that the ViewState was corrupted somwhere around saving it into or loading it from the ViewState.

I had to fix this, but I still needed all the information inside the class. Even though I knew that it was not the right thing to do really and in the end proved to be seriously useless since another measure I took apparently removed the need of saving it in the ViewState altogether.

I did find a solution that worked before the other thing started to take over. I wrote 2 functions, one to save the info in the object into the ViewState and one to load it again.

using System;
using System.Reflection;

namespace My.NameSpace
class MyClass
private void savetoViewState(MyObj saveobject)
Type t = saveobject.GetType();
foreach (PropertyInfo info in t.GetProperties())
object value = info.GetValue(saveobject, null);
if (value != null && test.GetType().Namespace == "System")
ViewSate["myobj_" + info.Name] = value;

private MyObj loadfromViewState()
MyObj myObj = new MyObj();
Type t = myObj.GetType();

foreach (PropertyInfo info in t.GetProperties())
object obj = ViewState["myobj_" + info.Name];
if (obj != null)
info.SetValue(myObj, obj, null);

return myObj;

class MyObj
public string Name

public int Number

In the save function we get the type of the object we’re working with, because the type class has some very nice reflection functions, among which are GetProperties(), GetMethods(), GetContructors and many more.
We use the GetProperties method to get all the properties and we loop through them in a foreach loop. Now we can examine each property seperately.

We get the value of the property by calling the PropertyInfo.GetValue() method which requires the object from which to get the property (our argument in this case) and an object array of index values, so that you can, I’m guessing, get a or multiple items in an array. The index must be null of you’re getting non-indexed properties.

Then we check if our value is null, because if it is we don’t need to save it, and we check to see if the namespace of the property’s type is System, which in my case made sure that I was only saving properties that were of type System.String or System.Int32 etc and not Some.NameSpace.In.My.Project.

Finally if our property’s value gets through these checks, we save it in the ViewState with a prefix to identify it as part of our object.

In the load function we do something very similar. First we instantiate a new instance of our class and we again get the type of it.
Then we loop through all it’s properties, much like we did in the save function.
Now, though, we get a value from the ViewState, which should correspond to what we saved in there before. Now if there is a property that we didn’t save then getting it from the ViewState will of course result in null, so we check if the value if null. If it’s not then we call the PropertyInfo.SetValue() method which needs the instance of the object it must place the value in, then the value that it must place and again like with GetValue() an object array of indexes.

Once we’ve looped through all the properties, it should again be filled with all the relevant information and we’re good to get to work on it again.

This is my first little bit of Reflection that I’ve written to date. I know it’s not much, but since I didn’t know if MyObj was going to stay the way it was and I wanted it to work regardless. I also didn’t feel like going ViewState["myobj_name"] = myObj.Name; over and over, since the object I was working with had many more properties than this example.

In the line with if (value != null && test.GetType().Namespace == "System") I had just gotten the value of the property, if it had one at all, and then I’m checking to see if the type of that property’s namespace equals “System”, this is because in the object I was working with also had a few properties that were connected to lazy-loaded database entities, which in turn also had these, and if I wanted to save these then it would have been a serious disaster (Damn how did I ever think that writing an object like that in the ViewState would just work…).

Anyway, it’s fun to see how easy a simple reflection function can be. I’d read about it when I studied for my certificate exams and I saw some of it at the Microsoft Dev Days this year, but it always seemed complicated and strange to me. All the more reason to try it out somewhere.

Telerik Controls in Web Custom Controls

I work at a .Net Application Development company and the other day I didn’t have anything to do, so I decided to change one of my User Controls into a Web Custom Control (ASP.Net Server Control) because I was told that we might want to use it in some of the other projects we have or are going to start in the near future.

I had never made a server control before and from what I had seen here and there I thought that this might be somewhat of a challenge. Thankfully it was easier then I thought it would be and I was successful fairly quickly.

Then one of my co-workers came up with the brilliant idea to make it’s ‘parent’ into a server control, so I started work on that.

This ‘parent’ was a collection of a Fieldset with a legend, 2 radiobuttons, 2 instances of my new server control and 2 rad controls.

When I started building this control I did it all (as I was taught) in the RenderContents(HtmlTextWriter output) function.

The problem was that when it got to the point where the Telerik controls were being Rendered, they liked to throw some ReferenceNullExceptions my way.

After some looking around though I found an article in the Telerik Knowledge Base that said that I should add the Telerik controls in the CreateChildControls() function, so I did, and it works.

Check out the article here

Non-presenter ServiceDependency

Well, ASP .Net developing can be a pain in the a$$ if you only have experience with windows applications in any language, but when the boss wants to use WCSF and the view-presenter model AND foundational modules, things can get even rougher. And don’t even think about using Opf3 with that, it’s fun, but you’ll spend many hours researching stuff.


Opf3 uses an ObjectContext class to connect with the database and work with your business entities and such, so generally you should be able to access the static method you need to get this object from every single part of your application imaginable, right? So if you’re working with WCSF you’ll probably need to use a Foundational Module.
This all shouldn’t really be a problem if you have presenters and views everywhere that derive from the Microsoft.Practices.CompositeWeb.Web.UI.Page class. Even if you have some pages that don’t have a presenter you can make them derive from the CompositeWeb’s Page class, so then your problem is fixed too. But what to do when you have a class that derives from MembershipProvider, or custom server controls?


The ObjectBuilder goes through classes that derive from certain CompositeWeb classes and searches for attributes like ServiceDependency, InjectionConstructor, CreateNew and such. You can make a public property with a ServiceDependency attribute so that any dependencies are handled and your compiler won’t complain about there not being an argumentless constructor.


Classes that don’t derive from CompositeWeb classes don’t get the benefit of this ObjectBuilder.


After many hours of trying, searching, researching and asking around I finally found out how to fix this. Being inexperienced as I am, though, doesn’t make me at all certain if there’s something wrong with this approach, but if you create an argumentless constructor and call the Microsoft.Practices.CompositeWeb.WebClientApplication.BuildItemWithCurrentContext(this) function, the ObjectBuilder will run through your class and use these attributes.


using System;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.ObjectBuilder;
using Chili.Opf3;

namespace Your.Namespace.Here
class MyClass : SomeClassItDerivesFrom
private ObjectContext _objContext; // The ObjectContext Opf3 uses to work with the database.
private IContextService _objContextFactory; // The Service you made with the static function.

[ServiceDependency] // This won't work without the function we call in the constructor
public IContextService ObjContextFactory
_objContextFactory = value;
_objContext = _objContextFactory.GetObjectContext(); // The static function you should have made.

public MyClass()

private SomeFunction()
ObjectSet objectSet = _objContext.GetObjectContext();

// Well, you should know what to do next.


Well, having never done anything like this before in my life I hope this is all and it might help someone. I’ve searched the internet for hours without any luck really, so maybe it will be helpful

My First Serialization…

When my study of Visual C# just started I set out to create an application that my girlfriend’s mother could use to organize her game shortcuts and stuff.
Today, maybe 6 months later, I finally figure out a way to save the shortcuts into a file and get them out (I learned this a while ago already), but today I figured out how to each give them their own eventhandler.

I created a class that contains the information that I need to get the program to work and also 1 function with the Button.Click event handler signature. I made another class that contains a generic list of the first class.
I save this list through a serializer and call it when the application loads.
I run through every instance of the first class and create a button using the information from my first class and call the event handler from it. This evenhandler uses the info from this class to start the application.

This is probably no great feat, since I’m only beginning and still need to adjust to the idea of the written program and the compiled program. When I’m talking to someone about 5 apples I can’t easily talk about each apple individually, but a well-written compiled program can because he doesn’t just see 5 apples, but 5 instances of an apple each with unique names that it gave to them (right?).