Hi all,

I am a naive programmer to VB and VB.Net, I have mostly used C# and now I am stuck at a simple point as follows :-

CollectionView.CollectionChanged += Function(sender, e) RebuildSearchIndex()

I don't have any idea how to replace "+=" as its showing some error.
Requesting you all if anyone could please suggest me the correct syntax of the above code.

Earlier (before converting my C# code to VB.Net), it was in C# as follows :-

CollectionView.CollectionChanged += (sender, e) => RebuildSearchIndex();

Thanks a lot in advance :)

Edited 2 Years Ago by KM499

Comments
good question

Hi all,

I did the above thing on my own using the following :-

AddHandler CollectionView.CollectionChanged, AddressOf RebuildSearchIndex

However, I am still struggling with the following code in c# and want the same functionality in VB/VB.Net :-

NextCommand = new DelegateCommand(
                p => FindItem(SearchType.ForwardSkipCurrent), 
                x => !String.IsNullOrEmpty(SearchTerm) && !NoResults);

Could someone please help me with the same.
Thanks a ton in advance :)

Comments
good question

How about this?

NextCommand = New DelegateCommand(Function(p) FindItem(SearchType.ForwardSkipCurrent), Function(x) Not String.IsNullOrEmpty(SearchTerm) AndAlso Not NoResults)

I already tried the same and in

FindItem(SearchType.ForwardSkipCurrent)

its showing "Expression does not produce a value"

Comments
good question

Hey, I replaced Function(p) with Sub(p) and that solved my problem.
Ok now another one...

Dim autoScroller = New EventHandler(Function(sender, e1)
                                                If grid.SelectedItem Is Nothing Then
                                                    Return Nothing
                                                End If
                                                grid.UpdateLayout()
                                                grid.ScrollIntoView(grid.SelectedItem)

                                            End Function)

        If CBool(e.NewValue) Then
            AddHandler grid.Items.CurrentChanged, AddressOf autoScroller
        Else
            RemoveHandler grid.Items.CurrentChanged, AddressOf autoScroller
        End If

In "AddressOf autoscroller", I am getting an error saying "Error 1 'AddressOf' operand must be the name of a method (without parentheses)."

Could anyone please help me with this ?

Comments
nice try

I solved this one also by applying the following code :-

 AddHandler grid.Items.CurrentChanged, Function() autoScroller

But I am not sure if it would work or not.
Anyways, I have another problem now...

Public Shared ReadOnly IsAutoScrollProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsAutoScroll", GetType(Boolean), GetType(DataGridExtenders), New UIPropertyMetadata(False, OnIsAutoScrollChanged()))



 Public Shared Sub OnIsAutoScrollChanged(ByVal o As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim grid = TryCast(o, DataGrid)
        If grid Is Nothing Then
            Return
        End If

        Dim autoScroller = New EventHandler(Function(sender, e1)
                                                If grid.SelectedItem Is Nothing Then
                                                    Return Nothing
                                                End If
                                                grid.UpdateLayout()
                                                grid.ScrollIntoView(grid.SelectedItem)

                                            End Function)



        If CBool(e.NewValue) Then
            AddHandler grid.Items.CurrentChanged, Function() autoScroller
        Else
            RemoveHandler grid.Items.CurrentChanged, Function() autoScroller
        End If
    End Sub

But in "New UIPropertyMetadata(False, OnIsAutoScrollChanged())", its showing some error as "Argument not specified for parameter 'o' of 'Public Shared Sub OnIsAutoScrollChanged(o As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)'".

Could some one please help me with this ???

Edited 2 Years Ago by KM499

Comments
nice job done

Solved this one also by having :-

Public Shared ReadOnly IsAutoScrollProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsAutoScroll", GetType(Boolean), GetType(DataGridExtenders), New UIPropertyMetadata(False, Sub(a As DataGrid, b As DependencyPropertyChangedEventArgs) OnIsAutoScrollChanged(a, b)))

Thanks to all anyways for at least going through this one :)

Comments
good try

So, my whole code goes like following (but not sure still whether it is the correct approach or not and if not please do correct this one) :-

Public Class DataGridExtenders
    Public Shared ReadOnly IsAutoScrollProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsAutoScroll", GetType(Boolean), GetType(DataGridExtenders), New UIPropertyMetadata(False, Sub(a As DataGrid, b As DependencyPropertyChangedEventArgs) OnIsAutoScrollChanged(a, b)))
    Public Shared Function GetIsAutoScroll(ByVal obj As DependencyObject) As Boolean
        Return CBool(obj.GetValue(IsAutoScrollProperty))
    End Function

    Public Shared Sub SetIsAutoScroll(ByVal obj As DependencyObject, ByVal value As Boolean)
        obj.SetValue(IsAutoScrollProperty, value)
    End Sub

    Public Shared Sub OnIsAutoScrollChanged(ByVal o As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim grid = TryCast(o, DataGrid)
        If grid Is Nothing Then
            Return
        End If

        Dim autoScroller = New EventHandler(Sub(sender, e1)
                                                If grid.SelectedItem Is Nothing Then
                                                    Return
                                                End If
                                                grid.UpdateLayout()
                                                grid.ScrollIntoView(grid.SelectedItem)

                                            End Sub)



        If CBool(e.NewValue) Then
            AddHandler grid.Items.CurrentChanged, Function() autoScroller
        Else
            RemoveHandler grid.Items.CurrentChanged, Function() autoScroller
        End If
    End Sub

End Class
Comments
good code format
This question has already been answered. Start a new discussion instead.