How to prevent Visual Studio from automatically reformatting C# code

Ed Hoppe 0 Reputation points
2024-12-17T21:47:00.3233333+00:00

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.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,053 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,361 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,208 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anna Xiu-MSFT 30,791 Reputation points Microsoft Vendor
    2024-12-18T04:10:23.6266667+00:00

    Hi @Ed Hoppe, 

    Welcome to Microsoft Q&A! 

    Are you using the latest version of VS2022? 

    I tested it on my side and was unable to reproduce your issue in the Visual Studio 2022 version 17.12.3. 

    You can update your VS2022 to the latest version and check it again. 

    If it persists, please report it by selecting “Help” menu > Send Feedback. 

    I also found the similar issue. You can vote on this ticket and add comments to track the process.  

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

     

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.