Tuesday, March 24, 2009

Word 2003 Style Tracking Issue

In word 2003 or ealier versions, whenever we format a text on which a style is already applied, a new style is added in the Styles list and it keeps on increasing if we dont delete it. However, we can stop this automatic tracking and the automatic addition of styles will be disabled. For disabling the automatic addition of styles, go to Tools-->Options --> Edit tab and uncheck "Keep Track of Formatting" option. It is shown in the figure below




Thanks,
Vikas

Thursday, March 19, 2009

Filter using Predicate (WPF)

In WPF, XAML has changed the traditional way of creating and validation the form fields. For example, what if we want to display few of the items in a listview? And what if we want to give user an option to view the items with in the listview?

Without WPF, we will create the loop through all the items and will hide/show the items accordingly. But with WPF, we can do it very easily. I will explain in the following example.

Create a class SampleClass with Two Properties, Name(of string type) and Visible(of boolean type)

Create an ObservableCollection of type SampleClass in your Xaml.cs.

Code : ObservableCollection< SampleClass > collection = new ObservableCollection ()

Add few Items :

collection.Add(new SampleClass("Vikas", true));
collection.Add(new SampleClass("Sonia", true));
collection.Add(new SampleClass("Viman", False));
collection.Add(new SampleClass("Krishna", False));

Add the collection to our listview

myListView.DataContext = collection.

Now, for having a smooth access on filtering, sorting etc features, we will need to get the ICollectionView(Click to know more about ICollectionView) object out of out observable collection.

ICollectionView view = CollectionViewSource.GetDefaultView(collection);

now, to apply the filter we can use the following line anywhere in the code where in we want to put a filter on:

view.Filter = delegate(object item)
{
SampleClass sItem = item as SampleClass;
bool retValue = false;

if(sItem!=null)
{
retValue = sItem.Visible;
}

return retValue;
}

We can do the above step by using a new method with the predicate as well.

private bool GetVisibleState(object item)
{
SampleClass sItem = item as SampleClass;
bool retValue = false;

if(sItem!=null)
{
retValue = sItem.Visible;
}

return retValue;
}

And add the filter like following :

view.Filter = new Predicate< object >(GetVisibleState);

The above code will show the items in which the Visible property is set to true.

The code in VB.net is as follows :


Dim view As ICollectionView
Dim collection As ObservableCollection(Of SampleClass) = New ObservableCollection(Of SampleClass)

collection.Add(New SampleClass("Vikas", True))
collection.Add(New SampleClass("Sonia", True))
collection.Add(New SampleClass("Viman", False))
collection.Add(New SampleClass("Krishna", False))

myListView.DataContext = collection


view = CollectionViewSource.GetDefaultView(collection)
view.Filter = New Predicate(Of Object)(AddressOf FilterItems)



Private Function FilterItems(ByVal param As Object) As Boolean

Dim proxy As SampleClass
Dim Name As String
Dim retValue As Boolean

Name = combovalue ' (some arbit value, which you want to use as a filter. Example, the if you want to filter the value if the name is vikas, then say name = "Vikas")

proxy = TryCast(param, SampleClass)
retValue = proxy.Show

Return retValue

End Function


Public Class SampleClass
Private _name As String
Private _show As Boolean

Public Sub New(ByVal name As String, ByVal show As Boolean)
_name = name
_show = show
End Sub

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public Property Show() As Boolean
Get
Return _show
End Get
Set(ByVal value As Boolean)
_show = value
End Set
End Property
End Class

Wednesday, March 18, 2009

How to Use Linqs in VB.Net

With the new versions of Visual Studios, we have got the Power of LINQ(Language INtegrated Query). With the help of LINQ, we can make query on any collection like we do in SQL. What if we want to search if a string is present in an array or not. Let me explain this in an example :

I have a string Array :

Dim validXLExtns() As String = {"XLS", "XLSX", "XLA", "XLAM", "XLSM", "XLTX", "XLT", ""}


Now, I have to check programmatically if "XLS" exists in the array or not. Ideally, you will like to loop through the items in the array. But, now, with the help of LINQs you can do it with the following command :

Dim product = From s In validXLExtns _
Where s = "XLS"


And if product.count is greater than 0, the value actually exists in the array.


Some very good examples of LINQs:

Developer.com
MSDN LINQ Examples


Monday, October 13, 2008

Working with Margins in WPF

Hi all,

As we know, that WPF has come up with a new Concept of XAML, the thing which irritated me most while I as learning it, is working with Margins. I, being an average guy, certainly took pretty long to understand how Margin Works.

     When I start learning WPF, I thought that previous version(Win Forms) is pretty easy and effective as compared to new WPF. But more familiar I got with WPF, I found it more and more effective.



Q. What are Margins?
A. Margins are mere numbers, which decide how far your control will be placed from the boundaries of the parent control/container.




Example:

  

  
< Grid >
      < Image Height="40" Margin="0,1,0,0" Name="imgTop" Opacity="1" Stretch="Fill" Style="{DynamicResource formHeaderImageStyle}" VerticalAlignment="Top" />

   < / Grid >

Now here, in the above line, it says vertical alignment = Top, that means, the margin will be considered from the top only. Margin 0,1,0,0 means 1 point away from top. Suppose if you want to increase and decrease the height of the control propotionate to the parent container then you can use following code.

< Grid >
      < Image Height="40" Margin="0,10,0,10" Name="imgTop" Opacity="1" Stretch="Fill" Style="{DynamicResource formHeaderImageStyle}" VerticalAlignment="Strech" />

   < / Grid >

The above code says Vertical alignment = strech. It means that the image control will be streched according to the margin set and the actual height of the container. The image control will be ten-ten points away from the top and the bottom of the container(Grid in the current case). If the Grid's height is increased, the image's height will be increased to ensure that the bottom is only 10 points aways from the Grid's lower bottom.

Thanks,
Vikas