Solved

Please tell me how to convert this C# to VB.NET

Posted on 2014-11-13
7
195 Views
Last Modified: 2014-11-17
        private void button1_Click(object sender, EventArgs e) {
            const string SERVER_URL = "My.Settings.ReportServerURL";
            const string REPORT_PATH = "My.Settings.ReportPath";

            var p = new ReportParameter("name", "val");
            var rv = new ReportViewer() {
                AutoSize = true,
                ProcessingMode = ProcessingMode.Remote
            };
            rv.ServerReport.ReportServerUrl = new Uri(SERVER_URL);
            rv.ServerReport.ReportPath = REPORT_PATH;
            rv.ServerReport.SetParameters(p);
            rv.RefreshReport();
            rv.RenderingComplete += (sender2, eventArgs) => rv.PrintDialog(new PrinterSettings() {
                PrintRange = PrintRange.SomePages,
                FromPage = 1,
                ToPage = 2,
                PrinterName = "MYPRINTER"
            });
        }

Open in new window


all is fine except i cannot figure out the syntax in VB.Net of this line

rv.RenderingComplete += (sender2, eventArgs) => rv.PrintDialog(new PrinterSettings() {
                PrintRange = PrintRange.SomePages,
                FromPage = 1,
                ToPage = 2,
                PrinterName = "MYPRINTER"
            }

Open in new window

0
Comment
Question by:FamousMortimer
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 50 total points
ID: 40440434
The => is a lambda expression:
http://msdn.microsoft.com/en-us/library/bb311046.aspx

The VB.Net equivalent is here:
http://msdn.microsoft.com/en-us/library/bb531253.aspx
where you have to write that one line as a function
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40440571
You need to do it in this way:
	Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
		Const SERVER_URL As String = "My.Settings.ReportServerURL"
		Const REPORT_PATH As String = "My.Settings.ReportPath"

		Dim p = New ReportParameter("name", "val")
		Dim rv = New ReportViewer() With {.AutoSize = True, .ProcessingMode = ProcessingMode.Remote}
		rv.ServerReport.ReportServerUrl = New Uri(SERVER_URL)
		rv.ServerReport.ReportPath = REPORT_PATH
		rv.ServerReport.SetParameters(p)
		rv.RefreshReport()
		AddHandler rv.RenderingComplete, AddressOf OnRenderingComplete
	End Sub

	Private Sub OnRenderingComplete(ByVal sender As Object, ByVal e As RenderingCompleteEventArgs)
		Dim rv As ReportViewer = TryCast(sender, ReportViewer)
		If Not rv Is Nothing Then
			rv.PrintDialog(New PrinterSettings() With { .PrintRange = PrintRange.SomePages, .FromPage = 1, .ToPage = 2, .PrinterName = "MYPRINTER" }
		End If
	End Sub

Open in new window


VB.NET, currently does not allow Lambda based sub event handlers.

http://10rem.net/blog/2010/04/16/tip-anonymous-event-handlers-in-vbnet

-saige-
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 40440676
@it_saige

Your article reference is 4 years old. Lambdas work just fine in VS 2013:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Me.Button1.Click, Function(snd, evt) MessageBox.Show("Click!")
    End Sub
End Class

Open in new window


Screenshot
P.S.

Sub works as well  : )
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 32

Accepted Solution

by:
it_saige earned 250 total points
ID: 40440723
Aha.  Thanks Kaufmed.  I just tried and confirmed in VS2010.  So I can say that atleast since .NET 3.5, this has been resolved.  Modifed code example above:
	Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
		Const SERVER_URL As String = "My.Settings.ReportServerURL"
		Const REPORT_PATH As String = "My.Settings.ReportPath"

		Dim p = New ReportParameter("name", "val")
		Dim rv = New ReportViewer() With {.AutoSize = True, .ProcessingMode = ProcessingMode.Remote}
		rv.ServerReport.ReportServerUrl = New Uri(SERVER_URL)
		rv.ServerReport.ReportPath = REPORT_PATH
		rv.ServerReport.SetParameters(p)
		rv.RefreshReport()
		AddHandler rv.RenderingComplete, Sub(sender2 As Object, e2 As RenderingCompleteEventArgs)
									   Dim rv2 As ReportViewer = TryCast(sender, ReportViewer)
									   If Not rv2 Is Nothing Then
										   rv2.PrintDialog(New PrinterSettings() With {.PrintRange = PrintRange.SomePages, .FromPage = 1, .ToPage = 2, .PrinterName = "MYPRINTER"})
									   End If
								   End Sub
	End Sub

Open in new window


-saige-
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40440924
ps.  My link to lambda support in VB states it is supported from VS2008 onwards
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40440942
As I recall, multi-line lambdas weren't introduced in VB until VS 2010/2012.  I'm getting older, though, so I could be off my rocker  ; )
0
 
LVL 10

Author Closing Comment

by:FamousMortimer
ID: 40447944
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now