Solved

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

Posted on 2014-11-13
7
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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 75

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 34

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 75

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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