0

What is the equivalent vb.net code of the corresponding MouseDown event shown below (C#)? How should I implement this event in vb.net?

Thank you in advance, Goicox

var model = new PlotModel("MouseDown HitTestResult", "Reports the index of the nearest point.");

var s1 = new LineSeries();
s1.Points.Add(new DataPoint(0, 10));
s1.Points.Add(new DataPoint(10, 40));
s1.Points.Add(new DataPoint(40, 20));
s1.Points.Add(new DataPoint(60, 30));
model.Series.Add(s1);
s1.MouseDown += (s, e) =>
            {
                model.Subtitle = "Index of nearest point in LineSeries: " + Math.Round(e.HitTestResult.Index);
                model.InvalidatePlot(false);
            };
goicox
  • 31
  • 1
  • 9
  • 7
    The title is funny. – Tim Schmelter Jun 21 '13 at 15:59
  • 1
    Use redgate reflector for converting code from CLR languages to VB/C#/C++ and vice versa. http://www.red-gate.com/products/dotnet-development/reflector/ – apollosoftware.org Jun 21 '13 at 15:59
  • The event is defined in .NET code, which both C# and VB are built on top of. The event will exist for the same class in VB with the same name. – Servy Jun 21 '13 at 16:01
  • @Servy: He's actually asking how to do lambdas. – SLaks Jun 21 '13 at 16:02
  • @SLaks Perhaps that's what he meant; it's not what he asked. – Servy Jun 21 '13 at 16:03
  • Depending on your version, it may be necessary to have a separate method (instead of a lambda/anon method). It can be helpful to declare "model" as WithEvents so you can get the correct signature stub handler for the event. Then remove WithEvents and the "Handles" clause at the end, and use AddHandler to wire up the event. – Idle_Mind Jun 21 '13 at 16:04

2 Answers2

1

Simple convertion should do it :

Dim model = New PlotModel("MouseDown HitTestResult", "Reports the index of the nearest point.")

Dim s1 = New LineSeries()
s1.Points.Add(New DataPoint(0, 10))
s1.Points.Add(New DataPoint(10, 40))
s1.Points.Add(New DataPoint(40, 20))
s1.Points.Add(New DataPoint(60, 30))
model.Series.Add(s1)
s1.MouseDown += Function(s, e) 
model.Subtitle = "Index of nearest point in LineSeries: " &         Math.Round(e.HitTestResult.Index)
model.InvalidatePlot(False)

End Function

Sources : http://www.developerfusion.com/tools/convert/csharp-to-vb/

Pakk
  • 1,299
  • 2
  • 18
  • 36
  • Using this, I got the following error: Error 1 'Public Event MouseDown(sender As Object, e As OxyPlot.OxyMouseEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. – goicox Jun 21 '13 at 16:20
  • Actually I got the following which is working: AddHandler s1.MouseDown, AddressOf Me.LSMouseDown – goicox Jun 21 '13 at 16:23
  • Thats because you cannot convert that statement. You need to use the Addhandler statement and wire that event to a sub routine you make. Like Idle_Mind stated. – OneFineDay Jun 21 '13 at 16:24
  • Well congratz , remember to mark helpfull posts aswell + rep is nice for everyone – Pakk Jun 21 '13 at 16:24
  • Now a second question, how could I pass arguments to the delegate function in vb.net? In C# the MouseDown method is able to modify "model" variables (e.g. model.Subtitle). Goicox – goicox Jun 21 '13 at 16:30
  • also im thinking this is not exactly what you want, could you refresh you code up top and i can help you out further – Pakk Jun 21 '13 at 16:46
0

You'll need to use a VB 'Sub' lambda (available in VS 2010 and beyond):

AddHandler s1.MouseDown, Sub(s, e)
    model.Subtitle = "Index of nearest point in LineSeries: " & Math.Round(e.HitTestResult.Index)
    model.InvalidatePlot(False)
    End Sub
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28