Tuesday 28 December 2010

Fixed - One or more ActiveX controls could not be displayed because either:...– When opening windows services screen

 

Sometimes when opening windows services screen (or by running ‘services.msc’ from the command prompt) you might receive the following error screen:

screen_shot_1

And gives you an empty service screen.

screen_shot_2

This can be caused due to many reasons. But most of the time it can be fixed by simply editing a registry entry.

Open the registry editor (or run ‘regedit’ from the command prompt) and browse to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones

And if you see a pseudo-graphic number before zone 0 like shown below, it could be the issue.

screen_shot_3

Delete this. How ever if that doesn’t fix the issue, or if the entry does not exist, select zone ‘0’. And change the value of ‘1200’ to ‘0’ or to ‘1’ (Usually the value will be ‘3’). The values 0,1 and 3 are associated with the following behavior:

  • 0 = Enabled, or the action is permitted
  • 1 = Prompt
  • 3 = Disabled, or the action is not permitted

screen_shot_4

And if this doesn’t work try to reset the Internet Explorer. To do that follow these steps:

  1. Click on Start & Run.
  2. Type ‘inetcpl.cpl’ & click ok
  3. Click on Advance Tab & click on Reset (Make sure to put the check mark on the delete the personal setting for IE8) then again reset.
  4. Click close.

Thursday 9 December 2010

Constructor Chaining in C#

 

What’s ‘Constructor Chaining’ ?

Constructor Chaining is an approach where a constructor calls another constructor in the same or base class.

This is very handy when we have a class that defines multiple constructors. Assume we are developing a class call ‘Student’. And this class consist of 3 constructors. On each constructer we have to validate the students id and categorize him/her. So if we do not use the constructor chaining approach, it would be something similar to the one shown below:

screen_01

Even the above approach solve our problem, it duplicate our code. (We are assigning a value to ‘_id’ in all constructors). This where constructor chaining is very useful. It will eliminate this problem. This time we only assign values only in the constructor which consist most number of parameters. And we call that constructor, when other two constructers are called.

class Student {
string _studentType = "";
string _id = "";
string _fName = "";
string _lName = "";

public Student(string id)
: this(id, "", "") {

}

public Student(string id, string fName)
: this(id, fName, "") {

}

public Student(string id, string fName, string lName) {
//Validate logic.....
_studentType = "<student_type>";

_id = id;
_fName = fName;
_lName = lName;
}
}


**Please note: If you do not specify anything [In this example we used ‘this’), it will be consider that we are calling the constructor on the base class. And it’s similar to using ‘: base(…)’]

Friday 3 December 2010

Show properties of a class on Property Grid

When developing user controls, usually we use Booleans, Integers, Strings, etc.. as data types of the attributes of the control. But sometimes, we have to use structures or other classes as attributes. So when we use those, we should be able to change or browse the attributes of those structures or classes.

Open visual studio IDE and create a new Windows Forms Application type project.

Add a new class and name it as ‘MyCustomClass’. And define two properties. One integer and a string type.

public class MyCustomClass {

public int MyIntProperty { get; set; }
public string MyStringProperty { get; set; }

public override string ToString() {
return "...";
}
}







**Please note that I have overridden the ‘ToString’ method. Because this what will be shown on the property grid when the properties are collapsed.



Now add a new user control to the project and name it as ‘MyCutomUsercontrol’. And create three properties. One integer, string and MyCustomClass. And use ‘[TypeConverter(typeof(ExpandableObjectConverter))]’ attribute on the third property. The syntax should be:






public partial class MyCutomUsercontrol : UserControl {
public MyCutomUsercontrol() {
InitializeComponent();
}

private MyCustomClass _MyCustomClass = new MyCustomClass();

public int Property1 { get; set; }
public string Property2 { get; set; }

[TypeConverter(typeof(ExpandableObjectConverter))]
[EditorBrowsable(EditorBrowsableState.Always)]
public MyCustomClass Property3 {
get {
return _MyCustomClass;
}
set {
_MyCustomClass = value;
}
}

}





 



Now build the solution. And add it a windows form. And on the property grid you can see your custom controls properties.



screen_1

Wednesday 1 December 2010

Calling Methods using Named Parameters using C# 4.0

Another new feature added to .Net 4.0 is to call methods using named parameters. This is very handy when calling a method which has optional arguments (You can use this to call other methods which consist of non optional parameters too). We’ll consider the following method.

static void SampleMethod(string ar1, 
string ar2 = "",
string ar3 = "",
string ar4 = "",
string ar5 = "",
string ar6 = "") {



//Some logic..
Console.WriteLine(ar6);
}



And assume that we only need to pass a value only to the last parameter. If the named parameter method was not there, the way to call the method would be something like this:


SampleMethod("message", "", "", "", "", "Hello World!");



But instead we can call the method using the following syntax.


SampleMethod(
ar1: "message",
ar6: "Hello World!");



And you will get the following output:


screen_1

Defining Optional Parameters for Methods using C# 4.0

The ability to create methods or functions with optional parameters/arguments was there in VB 6.0. How ever it was taken away when .Net came into action. And it was not there in earlier versions. But now it's available again in framework version 4.0.

Assume you have a method called "Greeting" which defines a single optional parameter:

static void Greeting(string message, string user = "Guest") {
Console.WriteLine(message + ", {0}",user);
}


 


So when the method is called by passing a value only to the first parameter, the default value of "Guest" will be assigned to the second argument. And you will get the following output.


Greeting("Hello");

screen_1

 


And when the method is called passing parameters to both arguments, you will get the following output.


Greeting("Hello","John");

screen_2