Hi @Brad Howard , Welcome to Microsoft Q&A.
As Viorel said, the code for what you are doing is generated by winform itself , which is why it changes each time you modify it.
Not in designer.csz you have some alternative options:
One is to not use native controls, use custom controls.
The second is to use code to customize and add controls, but you may need to have a concept of location and so on.
You need to refer to the self-generated code to write or paste your own code into cs.
Here is a simple code to add a button:
using System;
using System.Windows.Forms;
namespace_7_17_2
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
CreateCustomControls(); // Manually create controls after InitializeComponent
}
private static Button myButton = new Button();
private void CreateCustomControls()
{
// Create the controls you need
myButton.Text = "Custom Button";
// Add the control to the placeholder control (such as a Panel control)
this.Controls.Add(myButton);
// Set properties such as control position and size
// myButton. Location = ...
// myButton. Size = ...
//...
// Add control event handling logic
myButton.Click += MyButton_Click;
}
private void MyButton_Click(object sender, EventArgs e)
{
// Logic for handling button click events
}
}
}
Best Regards,
Jiale
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.