Copying a Form

You know, that was a pretty cool form we made. The DGV can display rows from a table, and the sidebar on the left can have buttons and links. It seems like it could be used in different places. Can we keep it and just use a copy?

In C#, you can base a form on another form, and Visual Studio has features that make this easier, but it’s still kind of complicated, and we won’t do it right now. We’re going to do it that hacky way. First, in Solution Explorer, select Form1, press Ctrl-C and then Ctrl-V. A new form called Form1 – Copy.cs will appear. You’ll also have a bunch of compile errors.

Ignore those errors for now, and let’s rename the form to ClientList. Right-click on the copy of Form1, select Rename, and type away. ClientList.

As you can see if you expand ClientList.cs, Visual Studio does a good job at renaming the files, but does not make the necessary changes to the code, which is why we got so many errors. Open the form’s code and change Form1 to ClientList.

And then you have to change the Designer code file. In most cases, you don’t have to, and if you do, Designer just rolls back your changes next time you open the form in it, but in this case, we have to. Open ClientList.Designer.cs and change the line that reads “partial class Form1” to “partial class ClientList” which should be on line 3. Then all the errors will vanish. Or rebuild the project if they don’t. Then they will.

And that’s how we copy a form the easy way. Or the hard way. I don’t know. Time will tell.

Leave a Reply

Your email address will not be published. Required fields are marked *