tags:

views:

53

answers:

1

Hi all,

I'm writing a custom textblock control thats populate hyperlinks and raises event when clicked to hyperlink.

I wrote this code but I got stucked.

My code is :

Imports System.Text.RegularExpressions
Public Class CustomTextBlock
Inherits TextBlock

Public Event Klik As EventHandler(Of EventArgs)
Public ReadOnly InlineCollectionProperty As DependencyProperty = DependencyProperty.Register("InlineCollection", GetType(String), GetType(CustomTextBlock), New PropertyMetadata(New PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged)))

Private Shared Sub InlineChanged(ByVal sender As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)

    DirectCast(sender, CustomTextBlock).Inlines.Clear()

    Dim kelimeler = Split(e.NewValue, " ")
    For i = 0 To kelimeler.Length - 1
        If Regex.Match(kelimeler(i), "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?").Success Then

            Dim x = New Hyperlink(New Run(kelimeler(i)))
            x.AddHandler(Hyperlink.ClickEvent, New RoutedEventHandler(AddressOf t_Click))
            x.ToolTip = kelimeler(i)
            x.Tag = kelimeler(i)
            DirectCast(sender, CustomTextBlock).Inlines.Add(x)
            If Not i = kelimeler.Length Then DirectCast(sender, CustomTextBlock).Inlines.Add(" ")
        Else
            DirectCast(sender, CustomTextBlock).Inlines.Add(kelimeler(i))
            If Not i = kelimeler.Length Then DirectCast(sender, CustomTextBlock).Inlines.Add(" ")
        End If
        ''//Console.WriteLine(kelime(i).ToString.StartsWith("@"))
    Next
    kelimeler = Nothing
End Sub
Public Property InlineCollection As String
    Get
        Return DirectCast(GetValue(InlineCollectionProperty), String)
    End Get
    Set(ByVal value As String)
        SetValue(InlineCollectionProperty, value)
    End Set
End Property

Private Shared Sub t_Click(ByVal sender As Hyperlink, ByVal e As System.Windows.RoutedEventArgs)
    e.Handled = True
    RaiseEvent Klik(sender, EventArgs.Empty)
End Sub
End Class

This code gives error at RaiseEvent Klik(sender, EventArgs.Empty)

Error is : Cannot refer to an instance member of a class from within a shared method or shared member initializer without an expliticit instance of the class.

Thanks for your answers, Alper

A: 

The problem is clearly stated in the exception message. The t_Click method is Shared (which means common to all instances of the class), so it cannot raise an Event that is specific to an instance of the class. You should only raise the event from a method that is not shared.

Meta-Knight
ok but when I remove shared from t_Click then I cant add handler to hyperlink with this code: x.AddHandler(Hyperlink.ClickEvent, New RoutedEventHandler(AddressOf t_Click))
SeSSiZ
That's because your InlineChanged Sub is also Shared so it cannot access t_Click that is not Shared anymore. So you should also remove the Shared keyword from InlineChanged.
Meta-Knight
Ok but another problem :D When i removed shared keyword from InlineChanged then getting error at this line Public ReadOnly InlineCollectionProperty As DependencyProperty = DependencyProperty.Register("InlineCollection", GetType(String), GetType(CustomTextBlock), New PropertyMetadata(New PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged)))at this part: AddressOf CustomTextBlock.InlineChangederror is: Reference to a non-shared member requires an object reference.Thanks again :(
SeSSiZ
It's hard to answer without knowing exactly what you're trying to achieve, but bottom line is either both your event AND functions interacting with this event are shared, or both are not shared. If you don't know what Shared implies read about the subject ;-)
Meta-Knight