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 :)

aatish2327 commented: good question +0

Recommended Answers

All 7 Replies

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 :)

commented: good question +0

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"

commented: good question +0

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 ?

commented: nice try +0

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 ???

commented: nice job done +0

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 :)

commented: good try +0

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
commented: good code format +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.