I had been using RDC available in Windows for a long time and didn't actually found a problem with it until the day when I had to connect to half a dozen virtual machines - all of them having long, hard to remember auto generated passwords.

Enter Royal TS. It's just the first day of using it and I am already thanking my stars. (Got to know about this tool just by chance on of the manager's computer).

Tuesday, 12 October 2010

Let us take a simple example, but a useful one. We start with a form, having 3 text boxes and a button. The button validates whether all the text boxes are filled up or not (a common business case).

We want to color background of the textboxes Red if they are empty:

var textBoxes = from tBox inthis.Controls.OfType<TextBox>()

where tBox.Text.Trim() == ""

select tBox;

foreach (TextBox tbox in textBoxes)

{

tbox.BackColor = Color.Red;

}

We get the same result as before. Building upon this, right now we get all the text boxes but what if we want to check only the visible text boxes? We should be able to achieve it by adding ‘tBox.Visible == true’ in the LINQ query. Which brings us to the question, how do we add AND condition in LINQ? (along the way let’s add Enabled condition as well)

var textBoxes =

from tBox inthis.Controls.OfType<TextBox>()

where tBox.Text.Trim() == "" &&

tBox.Visible == true &&

tBox.Enabled == true

select tBox;

For testing the OR condition, we select not just text boxes but Combo Boxes as well.

Sunday, 1 August 2010

Q. What happens if we define both the Command for a button and also provide handler for its click event?

A. The click event's handler fires first, and then the command.

Q. Can we assign add a command twice to the CommandBindings collection with different execute event handler?

A. Through testing it looks only one execute handler is fired. The first binding which is added to the collection gets invoked.

Q. Can we do something additional when the built-in commands are fired? For example we want to do something more when a copy command is invoked. Adding a new binding for Application.Copy does not work. (refer previous question) Or if we can't extend can override the Application.Cut behavior?

Friday, 30 July 2010

Today while testing out data binding in WPF to list controls, I learnt something new.

On the window, I created two list boxes, and a textblock control all have data context of an xml static resource.

For the ItemsSource in listbox I was using: {Binding XPath=@name} [the xml resource contains a Color element with attribute 'name]. I used IsSynchronizedWithCurrentItem on both the listboxes and bound the TextBlock Text to {Binding XPath=@Name}.

On testing the app I found, the controls are not synchronized. After referring to MSDN and some testing with different bindings - I found that to enable synchronization between all the controls to the CurrentItem of the collection, it is important to bind the list boxes to the whole collection. and not to a member or attribute (in this example).

So for listboxes it should be ItemsSource={Binding}, for display purposes we can use DisplayMemberPath or DataTemplate.

Wednesday, 7 July 2010

For example the attribute - AttributeUsage - is implemented in the class AttributeUsageAttribute. And we can use either AttributeUsage or AttributeUsageAttribute in our code.

In fact if you create a custom attribute in class named "TestAttribute":

public class TestAttribute : Attribute

{

}

and define another custom attribute named "Test":

public class Test : Attribute

{

}

and then try using the attribute in a class:

[Test()]

public class MyClass

{ }

This will give compilation error:

'Test' is ambiguous between 'Test' and 'TestAttribute'; use either '@Test' or 'TestAttribute'

The code intellisense as well displays only the "Test" pointing to TestAttribute in its list, it doesn't show the other "Test".

I believe C# creates an alias of TestAttribute somewhere for the attribute. It actually makes sense to use aliased-name of attribute. It would be unwieldy to write attributes as WebMethodAttribute, ConditionalAttribute or AttributeUsageAttribute - just to name a few defined in the framework. Microsoft also uses the alias on MSDN.

(The above links says: ANSI does not necessarily have to map to CP1252. It does, however, always refer to the legacy codepage set for the system. This may be CP1252 on western European or US systems but don't count on that)

While performing File I/O, it is important to keep in mind whether the files being processed can contain special characters or not. If it is so, then check the encoding you are using.Encoding used in:

StreamReader: it uses UTF-8 encoding by default unless otherwise specified.StreamWriter: Uses the default encoding of the system unless otherwise specified.If we do a check on the default encoding - we get "Windows-1252" [which is ANSI]. So while development if we don't specify encoding (and use defaults) then the files created by applications will use ANSI encoding, and applications reading them will use UTF-8!!!

This will give error, and we will see square characters/questions marks in place of the special characters.

Solution:- We can use encoding "Windows-1252" when reading the file or- use encoding UTF-8 while writing /reading the files (better approach)

Ironically:

You will get error (if no encoding is specified) only if you are reading an ANSI file using UTF-8 encoding. But you can successfully read a UTF-8 file using the ANSI (Windows-1252) encoding!!!