I don't know what Visual Studio did to the code on this project

357mag

Well-known member
Joined
Mar 31, 2023
Messages
58
Programming Experience
3-5
So I made a program using labels and textboxes to prompt the user to enter in three integers. When I finished designing the GUI then I double-clicked the Calculate button and I looked at the code Visual Studio created and
I saw some things I have not seen before.

It looks like Visual Studio has made an event handler for everything, not just the Calculate button. It made an event handler for Form Load, for labelFirstInteger, labelSecondInteger, labelThirdInteger. I have not had the IDE do this before.

C#:
public partial class FormMaximumAndMinimum : Form
    {
        public FormMaximumAndMinimum()
        {
            InitializeComponent();
        }

        private void FormMaximumAndMinimum_Load(object sender, EventArgs e)
        {

        }

        private void labelThirdInteger_Click(object sender, EventArgs e)
        {

        }

        private void labelFirstInteger_Click(object sender, EventArgs e)
        {

        }

        private void labelSecondInteger_Click(object sender, EventArgs e)
        {

        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {

        }
    }
}

What should I do?
 

Attachments

  • Program Interface.jpg
    Program Interface.jpg
    74.3 KB · Views: 8
So I made a program using labels and textboxes to prompt the user to enter in three integers. When I finished designing the GUI then I double-clicked the Calculate button and I looked at the code Visual Studio created and
I saw some things I have not seen before.

It looks like Visual Studio has made an event handler for everything, not just the Calculate button. It made an event handler for Form Load, for labelFirstInteger, labelSecondInteger, labelThirdInteger. I have not had the IDE do this before.

C#:
public partial class FormMaximumAndMinimum : Form
    {
        public FormMaximumAndMinimum()
        {
            InitializeComponent();
        }

        private void FormMaximumAndMinimum_Load(object sender, EventArgs e)
        {

        }

        private void labelThirdInteger_Click(object sender, EventArgs e)
        {

        }

        private void labelFirstInteger_Click(object sender, EventArgs e)
        {

        }

        private void labelSecondInteger_Click(object sender, EventArgs e)
        {

        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {

        }
    }
}

What should I do?

It didn't even make a public static void main method.
 
I don't use WinForms Designer anymore, but when I used to, I was under the impression that if your double click on a control it will create a the stub for the default event for that control. If you are in the habit of double clicking on stuff (or you set your mouse settings to have a shorter delay between clicks), then chances are that this is how you ended up with those event handlers.
 
I thought that double-clicking is what you're supposed to do to open the code window. I only double-clicked on the Calculate button so I could start writing code for the Calculate event. I'm going to have to do the whole thing over I guess.

My mistake when I said it did not even make a void static method. I think that is present in another part of the code.
 
Last edited:
When you do a release build, those empty methods will have negligible overhead on performance as they get optimized by the compiler. But if you have OCD, feel free to redo. But if you are also OCD, you may also want to be OCD about using source control. With source control, you should be able to roll back to a version without the un needed methods, or just just cherry pick the versions where you added relevant methods.
 
Can't quite remember, but I think if you have multiple controls selected and double click one of them it makes event handlers for all of them
 
I'm going to have to do the whole thing over I guess.

Just find the control in the designer, click it, click the lightning bolt ⚡️ in the properties grid, find the useless event and right click/reset it. If the event handler is empty, VS will clear it away too
 
Back
Top Bottom