2

I have converted a large sized ASp.Net Web forms project from VB.Net to C# using SharpDevelop and have been able to compile the project after fixing all errors(2100 in my case to be precise).

When I execute the code, I notice that the .aspx page works fine however, none of the events that converted to .cs file from .vb file is getting fired. For ex - Page_Load / btn_click etc. I have changed the event handler definitions from Private void Function() to protected void Function() but still same problems. Can someone advise on how to resolve the same?

Thanks in advance.

Nitesh
  • 2,286
  • 2
  • 43
  • 65
  • Did you convert the handlers from vb.net style to C# style? C# doesn't have a handles clause, so you would need to convert that to use delegates and then register them as the handler for the events. See https://stackoverflow.com/questions/794332/migrating-handles-from-vb-net-to-c-sharp for more information. – Wenadin Dec 16 '19 at 19:23

1 Answers1

1

In VB.Net the events are attached inlined on event handler method signature. e.g.

YoueventHandler(object, sender) Handles button1.Click

When you convert vb.net code to C# using any converters, the handler is converted but event handler registration will be gone. You have to re-register these eventhandler with the events in C#.

Here's Linqpad script that will help you scan a VB file for events and generate the C# handler for it.

void Main()
{
    var file = @"youcodeFile.vb";
    Regex r = new Regex(@"Sub (?<evthandler>[\S]+)\(.*\)\s?Handles\s?(?<eventName>[\S]+)");
    var events = r.Matches(File.ReadAllText(file)).Cast<Match>().Select(x => new { Event = x.Groups["eventName"].Value, Handler = x.Groups["evthandler"].Value});
    events.Dump();

    events.Select(x => $"{x.Event} += {x.Handler};").Dump();
}

Scanned events:

enter image description here

C# Output :

Me.Load += frmOutlookSyncStatus_Load; 
btnGo.Click += BtnGo_Click; 
btnLookupCase.Click += BtnLookupCase_Click; 
RepositoryItemButtonEdit1.Click += RepositoryItemButtonEdit1_Click; 
GridView1.CustomRowCellEdit += GridView1_CustomRowCellEdit; 
GridView1.CustomUnboundColumnData += gridViewEvents_CustomUnboundColumnData; 
vendettamit
  • 14,315
  • 2
  • 32
  • 54