没有直接的方法,但有可用的解决方法(Visual Studio 2010 - 2019):
假设您有一个名为Form1.cs 的表单,并且上面已经有控件,例如linkLabel、checkBoxes、radioButtons 和progressBar。
诀窍是编辑*.Designer.cs 文件,而不是移动控件。执行以下操作:
- 将新面板 (
panel1) 放置在 Form1 上,就像您通常做的那样(使用工具箱),并给它一个大小以覆盖其他控件。
- 关闭表单(和所有相关文件),然后在解决方案资源管理器中激活“显示所有文件”。现在
Form1.Designer.cs 变得可见。打开它。
-
找到以下代码,它包含注册到表单的控件:
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.btnOk);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
然后,查找创建面板的代码:
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 198);
this.panel1.TabIndex = 7;
您只需将控件从表单的Controls 集合(this.Controls) 移动到面板的Controls 集合(this.panel1.Controls)。在源代码中将其从一处移动到另一处,然后使用 Alt+Shift (Visual Studio 编辑器中的 block edit mode - 按住键之前你开始选择,并释放它们之后你已经选择了整个块)以将this.Controls替换为this.panel1.Controls:
添加到表单中的唯一剩余控件是panel1 和确定按钮btnOk:
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.panel1);
this.Controls.Add(this.btnOk);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
最后,关闭Form1.Designer.cs 并通过双击Form1.cs 重新打开表单。现在您应该看到面板内的控件。位置和以前一样。
注意:此描述是针对 Visual Studio 进行的,如果您使用的是 Visual Studio Code,您可以使用 multi cursor selection 实现相同的效果:键盘快捷键分别是:Strg+Alt+向上箭头或Strg+Alt+向下箭头。或者,您可以选择,然后按 Ctrl+Shift+L 将多个光标添加到当前选择的所有位置。使用多个光标,您键入的任何内容都将在所有光标位置插入/覆盖。