I am trying to add another button into windows form using dnSpy. Is it possible to add another?
I want the end result be like this one.
Any help will be highly appreciated.Thanks
Reverse Engineering Stack Exchange is a question and answer site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation. It only takes a minute to sign up.
Sign up to join this communityI am trying to add another button into windows form using dnSpy. Is it possible to add another?
I want the end result be like this one.
Any help will be highly appreciated.Thanks
Yes, it is possible!
You'll have to do some trial and error for positioning though.
Adding in a new control is fairly easy. We essentially need to do what the designer usually automatically does for us and create the new control and set up its properties ourselves. Luckily, we can use the button that's already there as a template.
First order of business is declaring our new button. Right click inside the class and hit "Edit Class" to open the code editor. Scroll down to the bottom where the variables are declared and declare our new button like so:
public Button button2;
Once that's done, you should be able to find the designer auto-generated InitializeComponent()
method for the form you want to modify. This is the function that does the initial setup for our form. it'll look something like this:
private void InitializeComponent()
{
this.button1 = new Button();
base.SuspendLayout();
this.button1.Location = new Point(189, 45);
this.button1.Name = "button1";
this.button1.Size = new Size(170, 53);
this.button1.TabIndex = 0;
this.button1.Text = "ADD ANOTHER LIKE ME";
this.button1.UseVisualStyleBackColor = true;
base.AutoScaleDimensions = new SizeF(6f, 13f);
base.AutoScaleMode = AutoScaleMode.Font;
base.ClientSize = new Size(563, 329);
base.Controls.Add(this.button1);
base.Name = "Form1";
this.Text = "Form1";
base.ResumeLayout(false);
}
We can copy and paste the creation code for the button that already exists in this case and replace "button1" with "button2". You'll probably want to change the tab index too.
private void InitializeComponent()
{
this.button1 = new Button();
this.button2 = new Button();
base.SuspendLayout();
this.button1.Location = new Point(189, 45);
this.button1.Name = "button1";
this.button1.Size = new Size(170, 53);
this.button1.TabIndex = 0;
this.button1.Text = "ADD ANOTHER LIKE ME";
this.button1.UseVisualStyleBackColor = true;
this.button2.Location = new Point(189, 145);
this.button2.Name = "button2";
this.button2.Size = new Size(170, 53);
this.button2.TabIndex = 1;
this.button2.Text = "I am Added";
this.button2.UseVisualStyleBackColor = true;
base.AutoScaleDimensions = new SizeF(6f, 13f);
base.AutoScaleMode = AutoScaleMode.Font;
base.ClientSize = new Size(563, 329);
base.Controls.Add(this.button1);
base.Name = "Form1";
this.Text = "Form1";
base.ResumeLayout(false);
}
Then, to get your button to actually appear, you'll need to add the line base.Controls.Add(this.button2);
under base.Controls.Add(this.button1);
.
We're not done yet, as our new button will have the exact same properties as the original if we leave it like this, and will appear in the same place with the same text. Replacing the text is easy, simply change the value of this.button2.Text
to your desired value. The position will take a little trial and error.
You'll need to edit this line:
this.button2.Location = new Point(189, 45);
Here, the first argument of Point
is the button's X position and the second is the button's Y position. To move the button downwards as seen in your question, you'll need to increase the Y position. How much you need to increase it depends on where you want the new button to be.
To save and test your changes, go to File -> Save Module
. Your final code should look something like this:
// Token: 0x02000002 RID: 2
public class Form1 : Form
{
// Token: 0x06000001 RID: 1
public Form1()
{
this.InitializeComponent();
}
// Token: 0x06000002 RID: 2
protected override void Dispose(bool disposing)
{
if (disposing && this.components != null)
{
this.components.Dispose();
}
base.Dispose(disposing);
}
// Token: 0x06000003 RID: 3
private void InitializeComponent()
{
this.button1 = new Button();
this.button2 = new Button();
base.SuspendLayout();
this.button1.Location = new Point(189, 45);
this.button1.Name = "button1";
this.button1.Size = new Size(170, 53);
this.button1.TabIndex = 0;
this.button1.Text = "ADD ANOTHER LIKE ME";
this.button1.UseVisualStyleBackColor = true;
this.button2.Location = new Point(189, 145);
this.button2.Name = "button2";
this.button2.Size = new Size(170, 53);
this.button2.TabIndex = 1;
this.button2.Text = "I am Added";
this.button2.UseVisualStyleBackColor = true;
base.AutoScaleDimensions = new SizeF(6f, 13f);
base.AutoScaleMode = AutoScaleMode.Font;
base.ClientSize = new Size(563, 329);
base.Controls.Add(this.button1);
base.Controls.Add(this.button2);
base.Name = "Form1";
this.Text = "Form1";
base.ResumeLayout(false);
}
// Token: 0x04000001 RID: 1
private IContainer components;
// Token: 0x04000002 RID: 2
private Button button1;
public Button button2;
}
and my final form looked like this:
I hope this helped!