# .NET and More > WPF, WCF, WF >  [RESOLVED] Translate from C# to VB

## Hamish

I'm trying to build this: http://www.thejoyofcode.com/Controll...Thumbnail.aspx in VB, but run in to trouble.

Here's my code:


```
Partial Public Class ScrollViewerThumbnail
	Inherits Windows.Controls.Control

	Public Sub New()
		DefaultStyleKeyProperty.OverrideMetadata(TypeOf(ScrollViewerThumbnail), New Windows.FrameworkPropertyMetadata(TypeOf(ScrollViewerThumbnail)))
	End Sub

	Public ReadOnly ScrollViewerProperty = Windows.DependencyProperty.Register("ScrollViewer", ScrollViewer.GetType, TypeOf(ScrollViewerThumbnail), New Windows.UIPropertyMetadata(Nothing))

	Public Property ScrollViewer() As Windows.Controls.ScrollViewer
		Get
			Return
		End Get
		Set(ByVal value As Windows.Controls.ScrollViewer)

		End Set
	End Property

End Class
```

I guess it comes down to translating the C# function typeof.... it's not TypeOf() however obvious it seems...

----------


## gep13

Hey,

I haven't really looked at the code at all, but this is what I get if you take the code you linked to and run it through the code converter here:



```
Public Class ScrollViewerThumbnail
    Inherits Control
    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(ScrollViewerThumbnail), New FrameworkPropertyMetadata(GetType(ScrollViewerThumbnail)))
    End Sub
    
    Public Property ScrollViewer() As ScrollViewer
        Get
            Return DirectCast(GetValue(ScrollViewerProperty), ScrollViewer)
        End Get
        Set(ByVal value As ScrollViewer)
            SetValue(ScrollViewerProperty, value)
        End Set
    End Property
    
    ' Using a DependencyProperty as the backing store for ScrollViewer. This enables animation, styling, binding, etc...
    Public Shared ReadOnly ScrollViewerProperty As DependencyProperty = DependencyProperty.Register("ScrollViewer", GetType(ScrollViewer), GetType(ScrollViewerThumbnail), New UIPropertyMetadata(Nothing))
End Class
```

Hope that helps!!

Gary

----------


## Hamish

Hehehe, GetType.... that simple.

----------


## gep13

Cool, is everything working?  If so, remember to mark your thread as resolved.

Gary

----------


## Hamish

I'm still testing... will do if resolved.

----------


## DeanMc

I would also try to make an effort to learn to read C# because while converters are good sometimes they get things wrong!

----------


## Hamish

Right, now I'm trying to translate this:



```
private const string PART_Highlight = "PART_Highlight";

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    var partHighlight = (Thumb)this.Template.FindName(PART_Highlight, this);
    partHighlight.DragDelta += partHighlight_DragDelta;
}

void partHighlight_DragDelta(object sender, DragDeltaEventArgs e)
{
    ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset + e.VerticalChange);
    ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset + e.HorizontalChange);
}
```

I've got:


```
Private Const PART_Highlight As String = "PART_Highlight"

Public Overrides Sub OnApplyTemplate()
    MyBase.OnApplyTemplate()
    Dim partHighlight As Windows.Controls.Primitives.Thumb
    partHighlight = DirectCast(Me.Template.FindName(PART_Highlight, Me), Windows.Controls.Primitives.Thumb)
    partHighlight.DragDelta += partHighlight_DragDelta
End Sub

Private Sub partHighlight_DragDelta(ByVal sender As Object, ByVal e as Windows.Controls.Primitives.DragDeltaEventArgs)
    ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset + e.VerticalChange)
    ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset + e.HorizontalChange)
End Sub
```

The bold line is the one giving me trouble, I can see it's not right, but I don't really understand what it is supposed to do, so have no idea what it should be...

----------


## DeanMc

Thats creating and event handler for the partHighlight.DragDelta event. You cannot do this in VB you need to go look up event on the MSDN websites.

----------


## Negative0

You can add handlers in VB code, the syntax is just a bit different:



```
AddHandler partHighlight.DragDelta, AddressOf partHighlight_DragDelta
```

----------


## Hamish

Ah, is that what it does! Helpful, thanks!

Now I have just 1 more issue before I can close this topic. I have translated the control to VB, but I can't use it. The XAML from the site:



```
    <Controls:ScrollViewerThumbnail ScrollViewer="{Binding ElementName=scrollViewer}" 
        Width="150" Height="150" 
        HorizontalAlignment="Right" VerticalAlignment="Bottom" 
        Margin="10" />
```

is not working for me. I get 2 errors:
''Controls' is an undeclared namespace. Line x, position y.' XML is not valid.The type 'Controls.ScrollViewerThumbnail'was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

----------


## Hamish

OK, solved that error. Namespace troubles....

Now my new control is completly without errors.... only problem is, it doesn't show up on my window. I'll post the complete code.

AssemblyInfo.vb (added line only)


```
<Assembly: Windows.ThemeInfo(Windows.ResourceDictionaryLocation.None, Windows.ResourceDictionaryLocation.SourceAssembly)>
```

Generic.xaml


```
<ResourceDictionary
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Controls="clr-namespace:WPFControls">

	<Style TargetType="{x:Type Controls:ScrollViewerThumbnail}">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="{x:Type Controls:ScrollViewerThumbnail}">
					<Viewbox DataContext="{TemplateBinding ScrollViewer}" Stretch="Uniform">
						<Grid>
							<Rectangle Width="{Binding Content.ActualWidth}" Height="{Binding Content.ActualHeight}">
								<Rectangle.Fill>
									<VisualBrush Visual="{Binding Content}" />
								</Rectangle.Fill>
							</Rectangle>
							<Thumb Name="ThumbHighlight" Background="{TemplateBinding HighlightFill}" Width="{Binding ViewportWidth}"
								Height="{Binding ViewportHeight}" HorizontalAlignment="Left" VerticalAlignment="Top">
								<Thumb.RenderTransform>
									<TranslateTransform X="{Binding HorizontalOffset}" Y="{Binding VerticalOffset}" />
								</Thumb.RenderTransform>
								<Thumb.Template>
									<ControlTemplate TargetType="Thumb">
										<Border Background="{TemplateBinding Background}" />
									</ControlTemplate>
								</Thumb.Template>
							</Thumb>
						</Grid>
					</Viewbox>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</ResourceDictionary>
```

ScrollViewerThumbnail.xaml.vb


```
Imports System.Windows.Media

Partial Public Class ScrollViewerThumbnail
	Inherits Windows.Controls.Control

	Public Sub New()
		DefaultStyleKeyProperty.OverrideMetadata(GetType(ScrollViewerThumbnail), New Windows.FrameworkPropertyMetadata(GetType(ScrollViewerThumbnail)))
	End Sub

	Public Property ScrollViewer() As Windows.Controls.ScrollViewer
		Get
			Return DirectCast(GetValue(ScrollViewerProperty), Windows.Controls.ScrollViewer)
		End Get
		Set(ByVal value As Windows.Controls.ScrollViewer)
			SetValue(ScrollViewerProperty, value)
		End Set
	End Property

	Public Property HighlightFill() As Brush
		Get
			Return DirectCast(GetValue(HighlightFillProperty), Brush)
		End Get
		Set(ByVal value As Brush)
			SetValue(HighlightFillProperty, value)
		End Set
	End Property

	Public Shared ReadOnly ScrollViewerProperty As Windows.DependencyProperty = Windows.DependencyProperty.Register("ScrollViewer", GetType(ScrollViewerThumbnail), GetType(ScrollViewerThumbnail), New Windows.UIPropertyMetadata(Nothing))
	Public Shared ReadOnly HighlightFillProperty As Windows.DependencyProperty = Windows.DependencyProperty.Register("HighlightFill", GetType(Brush), GetType(ScrollViewerThumbnail), New Windows.UIPropertyMetadata(New SolidColorBrush(Color.FromArgb(128, 255, 255, 0))))

	Private Const ThumbName As String = "ThumbHighlight"

	Public Overrides Sub OnApplyTemplate()
		MyBase.OnApplyTemplate()
		Dim PartHighlight As Windows.Controls.Primitives.Thumb
		PartHighlight = DirectCast(Me.Template.FindName(ThumbName, Me), Windows.Controls.Primitives.Thumb)
		AddHandler PartHighlight.DragDelta, AddressOf PartHighlight_DragDelta
	End Sub

	Private Sub PartHighlight_DragDelta(ByVal sender As Object, ByVal e As Windows.Controls.Primitives.DragDeltaEventArgs)
		ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset + e.VerticalChange)
		ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset + e.HorizontalChange)
	End Sub

End Class
```

VUI_Main.xaml (using the control)


```
<Window x:Class="VUI_Main"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Controls="clr-namespace:WPFControls;assembly=WPFControls"
	Title="Victory! User Interface" Width="1280" Height="1024">
    <Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="360" />
			<ColumnDefinition />
		</Grid.ColumnDefinitions>
		<Grid.RowDefinitions>
			<RowDefinition />
			<RowDefinition Height="206" />
		</Grid.RowDefinitions>
		<TabControl Name="MenuPanel" Grid.Row="0" Grid.Column="0">
			<TabItem Name="EconomyPanel" Header="Economy" />
			<TabItem Name="MilitaryPanel" Header="Military" />
			<TabItem Name="PoliticalPanel" Header="Political" />
		</TabControl>
		<ScrollViewer x:Name="MapScroller" HorizontalScrollBarVisibility="Visible" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top">
			<Canvas Name="MapCanvas" Width="8980" Height="5160">
				<Canvas.Background>
					<ImageBrush ImageSource="/Resources/MainMap.jpg" ScrollViewer.CanContentScroll="True"/>
				</Canvas.Background>
			</Canvas>
		</ScrollViewer>
		<Controls:ScrollViewerThumbnail ScrollViewer="{Binding ElementName=MapScroller}" Grid.Row="1" Grid.Column="0" />
	</Grid>
</Window>
```

Just a reminder: I'm trying to build this (and the 2 articles it builds on) in VB.

----------


## DeanMc

Which part doesnt show up the scroll viewer or the thumbnail?

----------


## Hamish

Ah... good question. The thumbnail does not show up.

----------


## DeanMc

Try changing the z-index.

----------


## Hamish

How do I do that? Can't find a property z-index.....

[edit]
I don't think this will help though. My window is divided into a 2x2 grid:
* A TabControl in (0,0)
* The ScrollViewer in (0,1) with RowSpan=2
* The ScrollViewerThumbnail in (1,0)

And that's all.... So there's nothing to get in the way of the ScrollViewerThumbnail.
When I put the cursor in the <Controls:ScrollViewerThumbnail .... /> tag, the correct area in my window gets highlighted, so I think it is showing up, it's just not displaying anything. :S
[/edit]

----------


## chris128

I agree that its unlikely it is the Z index in light of what you have just said, but to make sure, you can set the z index of a control by code like this:


vb Code:
Grid.SetZIndex(Grid_Name_Here, 10) 'sets the grid to being 10th in the z order

Note that not all controls have that method available though (which is why I have used a Grid as an example as I know that works), so its possible the Thumb control does not.

EDIT:
Also, have you tried downloading the ClickOnce demo that is linked to in that article and then viewing the source code using http://www.thejoyofcode.com/Reflecto...kOnce_app.aspx

----------


## Hamish

I did some more debugging, and I when I pause the application and add the ScrollViewerThumbnail to my watch window, I see that the ScrollViewer property (i.e. the reference to the big map) is Nothing. I have no idea how this can be, the reference is right there in the XAML, and I practically copy/pasted that from the site.  :Confused:

----------


## chris128

Maybe thats the problem  :Wink:  You did change it to suit your app didnt you? Can you post your current relative XAML?

----------


## Hamish

The XAML is still the same as in post #11 (VUI_Main.xaml)

----------


## Hamish

I finally got it to work!!!
First I noticed I was using a Custom Control instead of a straighforward Class, so I changed that, but no luck.
Then I tried setting the ScrollViewer property from code and found an error in the line:


```
Public Shared ReadOnly ScrollViewerProperty As Windows.DependencyProperty = Windows.DependencyProperty.Register("ScrollViewer", GetType(ScrollViewerThumbnail), GetType(ScrollViewerThumbnail), New Windows.UIPropertyMetadata(Nothing))
```

It should be:


```
Public Shared ReadOnly ScrollViewerProperty As Windows.DependencyProperty = Windows.DependencyProperty.Register("ScrollViewer", GetType(ScrollViewer), GetType(ScrollViewerThumbnail), New Windows.UIPropertyMetadata(Nothing))
```

But still, not working!

Then I copy/pasted the XAML into Generic.xaml again, and voila. No idea what was wrong with it, and frankly I'm fed up with staring at this particular piece of code, so I'll just assume a typo somewhere and leave it at that.  :Smilie: 

Here's the project should anyone want to use it.

----------


## chris128

Nice one. Thanks for posting the code for others to use in future  :Smilie:

----------


## gep13

On that note, it is worth pursuing a CodeBank for WCF, WPF etc related code samples?  That way submissions like this could be grouped in one place.  Just a thought?

Gary

----------


## chris128

I think this has been mentioned and at the moment the idea is to just put them in the utilitybank/codebank and link to them in this forum.

----------


## Hamish

Do I have to do something to get it in the codebank, or will someone pick it up and post it there?

----------


## gep13

Sounds good.

@Hamish, you can create your own thread in the code bank, follow the sticky's instructions for rules etc, and just drop your code in there, with example usage etc.

Gary

----------

