We do not have a built in split button in .net Framework. The work around is use tool strip split button which is not that useful in aesthetic perspective.

This is very good article where the author extended the standard Button control and modified it into a split button.

Modifying a Type at runtime is always difficult.

Requirement: Object must be assigned to a PropertyGrid. To expand the nested complex types in the Object, TypeConvertor attributes should be added to the properties of the type. Type should be modified not be modified at design time.

Solution: The PropertyGrid does not expand the nested complex types of an object assigned to it, if the complex type is a user-defined object. We need to add TypeConverterAttribute(typeof(ExpandableObjectConverter) to the user-defined types. Since I cannot modify the user-defined type at design time, it was not a feasible option. That leaves me with two alternatives:

Alternative 1: Tweak the Property Grid to show the nested Complex types. Given the Complexity of Property Grid and the time constraint to solve the problem, I had to go to the second alternative.

Alternative 2: After searching  lot on the internet, I stumbled here. The way to achieve the task is to bluff the Property Grid. Before you create the object, add the attributes to the properties of the type at runtime using COM (not Reflection). What you do is tell the Property Grid that the Type you used has the Attributes required for the former display the nested complex types.

The Code goes something like this:

AttributeCollection oldAttributes = TypeDescriptor.GetAttributes(type); 
Attribute[] newAttributes = new Attribute[oldAttributes.Count + 1]; 
oldAttributes.CopyTo(newAttributes, 0); 
newAttributes[oldAttributes.Count] = new TypeConverterAttribute(typeof(ExpandableObjectConverter)); 
TypeDescriptor.AddAttributes(type, newAttributes);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Every application has a configuration file and you want to have some keys in that file which are

  • application specific
  • easily accessible in the application
  • have read and write operations

Read the keys with the following code:

System.Configuration.ConfigurationSettings.AppSettings["keyname"];

We can send WCF messages from a client application(application that consumes the WCF service) even without creating a proxy object.
For that, we can use the ChannelFactory.

If we have a contract called IMycontract and there is a message called MyMessage in the Contract with the channel IMyChannel, follow these steps to send the message without SVCUtil.exe.

ChannelFactory<IMyChannel> factory = new ChannelFactory<IMyChannel>(new BasicHttpBinding());
    IMyChannel channel = factory.CreateChannel(new EndpointAddress("http://10.10.10.10:2298/MyService/Service.svc"));
    channel.MyMessage (param1, param2);

You can include the headers as well. The IMyChannel is derieved from IMyContract and System.ServiceModel.IClientChannel.

I am working lately a lot on Reflection and WCF. So there will be quite a few posts on around those two.

This is one relates to the former category.

Problem: You want to create a Combo Box at runtime and the items to be included in the Combo Box should be read from an enum.

Solution: Read the Enumeration at runtime and fill the items of the combo box with them.

First get all the values from the Enumeration:

String[] names = System.Enum.GetNames(typeof(enumToRead));

enumToRead is the Enum which you have to read the names from.

Now, iterating through each of the names, add it to the Combo Box:

foreach (string name in lstProvider)
{
   cboxMyComboBox.Items.Add(name);
}

You can similarly get all the values in the enum by:

Array enumVal = Enum.GetValues(typeof(enumToRead)) ;

To retrieve the IP Address of the Machine on which the code is executed, you can use the following code:

 

string hostName = System.Net.Dns.GetHostName(); //get the name of the computer…

//gets the Ipaddress of the machine…

string IPAdd = System.Net.Dns.GetHostEntry(hostName ).AddressList[0].ToString();  

If you a try to host a service in Windows Vista, you will receive an AddressAccessDeniedException. It is because of overly secured environment of the Windows Vista.

You will receive an error stating

HTTP could not register URL http://+:8000/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

You get this error because the owner of the http publishing is the built in administrator and you will not be running with administrator privileges.

You have to add the HTTP namespace to your user account. For that run the Command Prompt as an Administrator and run this command:

netsh http add urlacl url=http://+:8000/ user=DOMAIN\UserName 

You can more details here

To start a thread with a method having parameters, one cannot use the traditional way of starting a thread.

The best way is to use a Anonymous Delegate to start a thread.

Here’s the sample:

MyParameteredMethod is my method which accepts 3 parameters. I wish to start this method in a new thread.

MyParameteredMethod is my paramet
ThreadStart ts = delegate() { MyParameteredMethod(param1, param2, param3); };
Thread t = new Thread(ts);
t.Start();

Next Page »