C#Windows窗体-链接相同窗体的实例

C#Windows窗体-链接相同窗体的实例,c#,winforms,C#,Winforms,我想基于单个模板表单创建32个Windows表单,这些实例应该相互链接。也就是说,每个表单都有一个用于调用下一个实例的按钮,以此类推。我可以创建任意多个表单,但如何将这些实例链接在一起 这就是我用来创建几个子窗体的方法: public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(obj

我想基于单个模板表单创建32个Windows表单,这些实例应该相互链接。也就是说,每个表单都有一个用于调用下一个实例的按钮,以此类推。我可以创建任意多个表单,但如何将这些实例链接在一起

这就是我用来创建几个子窗体的方法:

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

    private void button1_Click(object sender, EventArgs e)
    {
        ChildForm child = new ChildForm();
        child.Show();
    }
}
事件的顺序如下所示:

  • 用户启动应用程序,显示主窗体(只有“打开子项”按钮)
  • 用户按下“打开子窗体”按钮,子窗体的第一个实例打开
  • 第一个子表单(标题“子表单1”)有“打开子表单2”按钮
  • 如果用户按下“打开子窗体2”,子窗体1将隐藏,子窗体2将显示
  • 如果到达最后一个子表单,则环绕到子表单1
欢迎任何意见

问候


克里斯

我真的不知道你打算做什么:)。如果要计算多个表单,可以向表单中添加属性号。从那里向上搜索

主要形式

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

     private void button1_Click(object sender, EventArgs e)
     {
         ChildForm first = new ChildForm();
         first.Number = 1;
         first.Show();
     }
}
儿童型

public partial class ChildForm : Form
{
    public ChildForm()
    {
        // createButton here
    }
    private void button_Click(object sender, EventArgs e)
    {
        ChildForm _childForm = new ChildForm();
        _childForm.Owner = this;
        _childForm.Number = this.Number + 1;
        this.Hide();
        _childForm.Show();
    }

    public void FirstChildForm()
    {
        if (this.Number != 1) //maybe not that static
        {
            (this.Owner as ChildForm).FirstChildForm();
            this.Close(); // or hide or whatever
        }
    }
    public int Number
    { get; set; }
}

未测试的代码,希望这有点帮助:)。

我真的不知道您打算做什么:)。如果要计算多个表单,可以向表单中添加属性号。从那里向上搜索

主要形式

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

     private void button1_Click(object sender, EventArgs e)
     {
         ChildForm first = new ChildForm();
         first.Number = 1;
         first.Show();
     }
}
儿童型

public partial class ChildForm : Form
{
    public ChildForm()
    {
        // createButton here
    }
    private void button_Click(object sender, EventArgs e)
    {
        ChildForm _childForm = new ChildForm();
        _childForm.Owner = this;
        _childForm.Number = this.Number + 1;
        this.Hide();
        _childForm.Show();
    }

    public void FirstChildForm()
    {
        if (this.Number != 1) //maybe not that static
        {
            (this.Owner as ChildForm).FirstChildForm();
            this.Close(); // or hide or whatever
        }
    }
    public int Number
    { get; set; }
}

未经测试的代码,希望这有点帮助:)。

您可以创建表单的静态集合,在构造函数中将表单实例添加到列表中(并在dispose期间将其删除)。要计算出下一个表单,您可以找到当前表单的索引,并在此基础上获取列表中的下一个表单。创建一个带有两个按钮的表单,并按如下所示对其进行修改以测试它

 public partial class Form1 : Form
    {
        static List<Form1> formList = new List<Form1>();
        public Form1()
        {
            InitializeComponent();
            formList.Add(this);
        }



        private void button1_Click(object sender, EventArgs e)
        {
            int idx = formList.IndexOf(this);
            int nextIdx = (idx == formList.Count()-1 ?  0: idx+1 );

            Form1 nextForm = formList[nextIdx];
            nextForm.changeTextAndFocus("next form: " + nextIdx);
        }

        // moves to the next form and changes the text
        public void changeTextAndFocus(string txt)
        {
            this.Focus();
            this.Text = txt;
        }

        //Creates 5 forms
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                Form1 newForm = new Form1();
                newForm.Show();
            }
        }
    }
公共部分类表单1:表单
{
静态列表formList=新列表();
公共表格1()
{
初始化组件();
表单列表。添加(此);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
int idx=formList.IndexOf(this);
int nextix=(idx==formList.Count()-1?0:idx+1);
Form1 nextForm=formList[nextix];
changeTextAndFocus(“下一个表单:+nextix”);
}
//移动到下一个表单并更改文本
公共void changeTextAndFocus(字符串txt)
{
这是Focus();
this.Text=txt;
}
//创建5个表单
私有无效按钮2\u单击(对象发送者,事件参数e)
{
对于(int i=0;i<5;i++)
{
Form1 newForm=新Form1();
newForm.Show();
}
}
}

您可以创建表单的静态集合,在构造函数中将表单实例添加到列表中(并在dispose期间将其删除)。要计算出下一个表单,您可以找到当前表单的索引,并在此基础上获取列表中的下一个表单。创建一个带有两个按钮的表单,并按如下所示对其进行修改以测试它

 public partial class Form1 : Form
    {
        static List<Form1> formList = new List<Form1>();
        public Form1()
        {
            InitializeComponent();
            formList.Add(this);
        }



        private void button1_Click(object sender, EventArgs e)
        {
            int idx = formList.IndexOf(this);
            int nextIdx = (idx == formList.Count()-1 ?  0: idx+1 );

            Form1 nextForm = formList[nextIdx];
            nextForm.changeTextAndFocus("next form: " + nextIdx);
        }

        // moves to the next form and changes the text
        public void changeTextAndFocus(string txt)
        {
            this.Focus();
            this.Text = txt;
        }

        //Creates 5 forms
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                Form1 newForm = new Form1();
                newForm.Show();
            }
        }
    }
公共部分类表单1:表单
{
静态列表formList=新列表();
公共表格1()
{
初始化组件();
表单列表。添加(此);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
int idx=formList.IndexOf(this);
int nextix=(idx==formList.Count()-1?0:idx+1);
Form1 nextForm=formList[nextix];
changeTextAndFocus(“下一个表单:+nextix”);
}
//移动到下一个表单并更改文本
公共void changeTextAndFocus(字符串txt)
{
这是Focus();
this.Text=txt;
}
//创建5个表单
私有无效按钮2\u单击(对象发送者,事件参数e)
{
对于(int i=0;i<5;i++)
{
Form1 newForm=新Form1();
newForm.Show();
}
}
}

使用构造函数参数。您可以创建表单的静态集合,在构造函数中将表单实例添加到列表中(并在dispose期间将其删除)。要确定下一个表单,您可以找到当前表单的索引,并在此基础上获取列表中的下一个表单。使用构造函数参数。您可以创建表单的静态集合,在构造函数中将表单实例添加到列表中(并在dispose期间将其删除)。要计算出下一个表单,您可以找到当前表单的索引,并在此基础上获取列表中的下一个表单感谢您的努力。我将研究系列以及SLaks的构造方法(pinkfloyd和SLaks的道具)。感谢您的努力。我将研究集合以及SLaks的构造方法(pinkfloyd和SLaks的道具)。