c# - 使用多种形式

标签 c# winforms visual-studio visual-studio-2010

我想使用用户控件在一个表单中加载多个表单,我尝试了以下代码,但在单击 button1 后似乎没有任何反应。任何人都知道怎么了?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UserControl1 control = new UserControl1();
        control.Dock = DockStyle.Fill;
        this.Controls.Add(control);
    }
 }

最佳答案

however the contents of UserControl1 seems to be overlapping and I can still see the content of Form1

窗体上控件的 Z 顺序很重要。使用 Controls.Add(),控件最终位于顺序的底部,现有控件与它重叠。你这样修复它:

    this.Controls.Add(control);
    control.BringToFront();

或使用 Controls.SetChildIndex() 将其插入控件之间。

关于c# - 使用多种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140926/

相关文章:

vb.net - 如何阻止我的工具条菜单项出现在任务栏中?

c# - 如何配置packages.config下载最高版本

c++ - 如何将 Visual Studio 2010 WinForm 对象添加到 C++ 控制台应用程序项目

c# - Windows 通用 (UWP) 地理定位 API 权限

c# - 从 HTML 输入中去除脚本标签

c# - 从 UIElement 对象动态生成 xaml 字符串

c# - DateTimePicker 需要多次单击复选框才能触发 OnValueChanged

c# - 在调试器中查看 C# 泛型的类型

c# - 左侧的 WPF 选项卡控件标题

c# - MVVM轻信使,ViewModel之间的通信