[RESOLVED] Translate from C# to VB-VBForums
Results 1 to 25 of 25

Thread: [RESOLVED] Translate from C# to VB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Resolved [RESOLVED] Translate from C# to VB

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

    Here's my code:
    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...

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: Translate from C# to VB

    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:

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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

    Hehehe, GetType.... that simple.

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: Translate from C# to VB

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

    Gary

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

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

  6. #6
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

    Re: Translate from C# to VB

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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

    Right, now I'm trying to translate this:

    Code:
    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:
    Code:
    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...

  8. #8
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

    Re: Translate from C# to VB

    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.

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Translate from C# to VB

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

    Code:
    AddHandler partHighlight.DragDelta, AddressOf partHighlight_DragDelta

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

    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:

    Code:
        <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:
    1. ''Controls' is an undeclared namespace. Line x, position y.' XML is not valid.
    2. The type 'Controls.ScrollViewerThumbnail'was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

    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)
    Code:
    <Assembly: Windows.ThemeInfo(Windows.ResourceDictionaryLocation.None, Windows.ResourceDictionaryLocation.SourceAssembly)>
    Generic.xaml
    Code:
    <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
    Code:
    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)
    Code:
    <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.

  12. #12
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

    Re: Translate from C# to VB

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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

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

  14. #14
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

    Re: Translate from C# to VB

    Try changing the z-index.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

    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]
    Last edited by Hamish; Feb 2nd, 2009 at 03:22 AM.

  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Translate from C# to VB

    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:
    1. 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
    Last edited by chris128; Feb 2nd, 2009 at 09:51 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

    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.

  18. #18
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Translate from C# to VB

    Maybe thats the problem You did change it to suit your app didnt you? Can you post your current relative XAML?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: Translate from C# to VB

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

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    [RESOLVED] Translate from C# to VB

    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:
    Code:
    Public Shared ReadOnly ScrollViewerProperty As Windows.DependencyProperty = Windows.DependencyProperty.Register("ScrollViewer", GetType(ScrollViewerThumbnail), GetType(ScrollViewerThumbnail), New Windows.UIPropertyMetadata(Nothing))
    It should be:
    Code:
    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.

    Here's the project should anyone want to use it.
    Attached Files Attached Files
    Last edited by Hamish; Feb 5th, 2009 at 07:42 AM.

  21. #21
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Translate from C# to VB

    Nice one. Thanks for posting the code for others to use in future
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #22
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: [RESOLVED] Translate from C# to VB

    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

  23. #23
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Translate from C# to VB

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    252

    Re: [RESOLVED] Translate from C# to VB

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

  25. #25
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: [RESOLVED] Translate from C# to VB

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.