How to prevent Visual Studio from automatically reformatting C# code

In my Form.cs files, Visual Studio (2022) automatically reformats my code whenever I create a new event handler. Specifically, it deletes spaces and indentations. Original code looks like this:

private void OnTemperature(object sender,EventArgs e)

{

if(!steadystateflowsolved && !transientflowsolved) return;

double min = 0;

double max = 0;

GetFlowMinMax((int)AnalysisResults.TEMPERATURE, ref min, ref max);

string label = “Temperature”;

string unit = “°C”;

int tab = ModelForm.GetCurrentPage();

bool use3dmodel = (tab == 0);

bool usepid = (tab == 1);

if(steadystateflowsolved) {

if(use3dmodel) {

	Generate3DResults((int)AnalysisResults.TEMPERATURE,min,max);

	ModelForm.modelviewer.ImportResults();

	ModelForm.modelviewer.Render();

	IntPtr hwnd = ModelForm.GetResultsPanelHandle();

	ModelForm.DrawResultsScale(hwnd,"Temperature","°C", (float)min, (float)max);

	} 

else 

	Generate2DResults((int)AnalysisResults.TEMPERATURE, min, max);

}

}

After generating another event handler, it’s reformatted like so:

//—————————————————————————————————-

private void OnTemperature(object sender,EventArgs e) {

if(!steadystateflowsolved && !transientflowsolved) return;

double min = 0;

double max = 0;

GetFlowMinMax((int)AnalysisResults.TEMPERATURE,ref min,ref max);

string label = “Temperature”;

string unit = “°C”;

int tab = ModelForm.GetCurrentPage();

bool use3dmodel = (tab == 0);

bool usepid = (tab == 1);

if(steadystateflowsolved) {

if(use3dmodel) {

Generate3DResults((int)AnalysisResults.TEMPERATURE,min,max);

ModelForm.modelviewer.ImportResults();

ModelForm.modelviewer.Render();

IntPtr hwnd = ModelForm.GetResultsPanelHandle();

ModelForm.DrawResultsScale(hwnd,”Temperature”,”°C”,(float)min,(float)max);

} else

Generate2DResults((int)AnalysisResults.TEMPERATURE,min,max);

}

}

Under Options | Code Style | Formatting, I have automatic formatting turned off, and it still does it.

Read more here: Source link