0

I'm working on translating a VB.NET application to C#.NET (and I've never worked with either before, although I do know C++). I've got most of it working, but there's one thing stumping me. There's a function in the VB that is

Private Sub timerReadCommands_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerReadCommands.Tick

But timerReadCommands is not declared anywhere. There's no button called that, there's no variable called that, Visual Studio says this method has zero references, I even tried checking the other file to see if it was somehow declared over there. This VB application (and method) works as expected, but it's not working in the C# version. I figure it must be declared somewhere, and I just can't find the declaration. In the C# application, I tried

Timer timerReadCommands = new Timer();

to declare it, which eliminated the undeclared variable errors, but the method never runs.

I've tried Googling to figure out how the VB is working, in order to try to mimic it in C#, but I'm only getting results from people whose timers aren't working. I need to know why mine is. Alternatively, I need to know how to make my C# one work.

Thanks!

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764
tiffanie
  • 69
  • 8
  • 1
    It's probably declared in a `.designer.vb` file. It'll be declared `WithEvents`. – madreflection Sep 03 '19 at 20:43
  • 2
    VB uses that syntax _Handles timerReadCommands.Click_ to associate an event handler code to an event. In C# you need to explicitly assign the handler to the event. _timerReadCommands.Tick += timerReadCommands_Tick_ – Steve Sep 03 '19 at 20:44
  • 1
    If you're still having trouble finding it, you have to select "Show All Files" on the toolbar at the top of Solution Explorer. VB hides it; C# does not. Although Steve's comment is all you *really* need. – madreflection Sep 03 '19 at 20:48
  • Thanks, Steve, that did it! For future reference, what terminology could I have googled to lead me to that answer? madreflection, I found the file and declaration. Why is it there instead of in the same file? How could I have found out it was there on my own? Thanks for your help! – tiffanie Sep 03 '19 at 21:17
  • 1
    Timers are considered "UI-less controls". You drag them to the form like any other control but they don't have a UI so they show in the little "tray" at the bottom of the designer. *All* controls added in the designer are declared in the .designer.[cs|vb] file because that's what it's there for. When you attach an event, it adds `WithEvents` to the declaration in VB and does the `+=` syntax in the initializations in C#. – madreflection Sep 03 '19 at 21:21
  • A part from the different syntax these are Event Handlers ( or code that is executed in response to the occurence of an event ). This concept exists in WinForms, WPF and also in ASP.NET prior to the MVC introduction. A key role for desktop application is the InitializeComponent method and the partial class concept. You can read something in https://stackoverflow.com/questions/12297079/very-simple-definition-of-initializecomponent-method – Steve Sep 03 '19 at 21:29
  • That's helpful. Thanks! – tiffanie Sep 03 '19 at 21:30
  • Speaking of syntax... In VB, the `Handles` clause and the `WithEvents` declaration together do some interesting things to look out for. If you assign a new value to `timerReadCommands`, VB will detach the handler from the old instance and attach it to the new instance by converting `timerReadCommands` to a property. C# won't do this. You'll have to manage event attachment yourself. If your code is modifying that "field" and relying on that behavior, you'll have to create that behavior yourself in your C# translation. – madreflection Sep 03 '19 at 21:32
  • That is extremely useful information. Thank you. – tiffanie Sep 03 '19 at 21:38
  • @madreflection The Timers (`System.Windows.Forms.Timer`, `System.Timer`...) are not considered "UI-less controls". They are not Controls at all (don't derive from Control). Those are Components (deriving from `Component`). As the BackGroundWorker component and others, that can be selected from the ToolBox. – Jimi Sep 04 '19 at 02:41
  • @Jimi: Point taken. I was aware of all of that, although I'll concede that the wording is not 100% precise. However, that distinction was not as important, in context, as the information needed to convert the code from VB to C#. – madreflection Sep 04 '19 at 02:51
  • In C# in the Visual Studio designer, if you drag a timer component to the form and drop it, it will appear in the components tray and the From.Designer.cs file will be automatically updated. You can double click on the component in the tray and that designer file will be updated with the default event (in this case, the Tick event). The event method stub will be added to the code file. The .Interval property may have been set at design time and somewhere in the code file the .Start method is called. – Mary Sep 04 '19 at 06:29

1 Answers1

0

The C# code to create the timer is missing this excerpt from the VB:

Handles timerReadCommands.Tick

C# doesn't support this feature, so you need to add the handler with a 2nd line of C# code, usually somewhere near the call to InitializeComponent():

timerReadCommands.Click += new EventHandler(timerReadCommands_Tick);

Additionally, that timer must be declared in the VB project somewhere, or that Handles clause would not compile. If you can load the VB project in Visual Studio, you should be able to right-click the timerReadCommands text in the Handles clause and choose Go to definition from the context menu; that will show you exactly where the timer is declared... probably a formname.designer.vb file.

The designer files are there for Visual Studio to use and manage. They exist to separate the code created by the forms designer from your own code, so the forms designer does not over-write or change something you wanted to keep. You should not change anything in the designer files. Instead, you can create this timer by dragging to the form from the toolbox and giving it the same name.

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764