I’ve been working with WinForms for a very, very long time. Usually, because companies that hire me use this technology and I’m brave/resigned enough to help them in their strugglings 🙂 Visual Studio has quite good support for the WinForms, but it also suffers from a very serious bug(?) or shortage (at best) – when a new element is added to a form, sometimes it severely changes underlying code. On the surface, the form looks the same, but under the hood, a mess happened. It’s particularly severe when we work in a group and we want to commit our small change to the repository.
Code Review for this small change is almost impossible, as the number of changes made accidentally by the VS is enormous. Fortunately, Git is the tool that may help us to overcome this problem.
Let’s visualize what I mean. If we have a simple form and want to add another control to it (named checkbox1:)), the expected and desired output is:
The checkbox was added and only relevant lines are visible in the comparison tool. It means that VS did a good job and didn’t change anything unnecessary.
But, there might be a situation like this:
The checkbox was added and the old controls have been reorganized. Their positions and other properties have been unchanged, so it’s not very severe, although it will cause problems during code review. Another slightly more severe case:
Unfortunately, in the most complex forms, sometimes the output looks as messy as this one:
The comparison tool shows hundreds of lines changed. Why, we only added one small checkbox, so at most 10 lines of code should be added, and zero changed. But we see that all controls have slightly changed positions and sizes – a complete disaster! The form still looks like before, but internally it’s been completely changed.
This is some kind of bug in Visual Studio and I haven’t found its proper resolution. Instead, I learned what to do if it occurs. Particularly, the Git feature called „hunks” can help us overcome it.
I described Git hunks in one of my previous notes. In short – it’s a feature that allows to stage only part of the file, not the whole one. And probably you already know how to use it in our case. We just need to stage the lines truly needed when we added/changed the control on the form, not all the lines changed by Visual Studio.
So:
- Add control (checkbox in our example) to the form, change its name – for example to chkbxOnAndOff
- Compare the file – before and after addition. Find the lines with the name chkbxOnAndOff. Stage them using Git hunks.
- Discard all other changes. Recompile, and see what it looks like. If the position is incorrect, move the control to the correct position on the form, remember the coordinates, discard all changes and write correct, remembered coordinates manually.
- Repeat the procedure for all the next controls you wish to add.
That’s it. When the above procedure is finished, you have a staged file ready to be committed. This is only a bypass for the VS problem I found, but as they say – beggars can’t be choosers, right? 🙂