Translating a Windows Forms custom control from VB.NET to C#

It's not as easy as tossing the code into some translator, copying and pasting. There are some gotchas:

  1. After creating the C# project, instead of creating the .cs and .Designer.cs files manually, it's more practical to right-click the project in Visual Studio's Solution Explorer and selecting Add->User Control. This way, the .cs and .Designer.cs are automatically associated.
  2. Before editing these two files, open the newly created control in Design view. Open the Toolbox window, and drag and drop any control (a Label, for instance). This will make Visual Studio create a .resx file which will be associated to the control. Once that is done, you can safely delete the control you added in the design view.
  3. If the original project has resources, that is, there is also a .resx file at the project level, we need to generate one for our C# project as well. This can be done by going to the project's properties, Resources section. You'll see some text saying "This project does not contain a default resources file. Click here to create one.". Do so.
  4. If the original project has resources, it will also be necessary to implement in Resources.Designer.cs the properties used to access them. About this, said properties are typically accessed using My.Resources in VB.NET; the C# equivalent is Properties.Resources.
  5. Now all that remains is copying translated code. About .resx files, it's not even necessary to translate them, since the format is the same.

Comments

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x