C# 访问新的表单实例#

C# 访问新的表单实例#,c#,windows,winforms,multiple-forms,C#,Windows,Winforms,Multiple Forms,我正在用C#编写一种即时消息应用程序,它可以在套接字上工作。我管理了所有的套接字问题,现在它像IRC一样工作。然而,我想实现私有消息系统。我们有两种形式-一种是主要的,第二种是一个类似私人msg的原型。窗户应该是这样的。这是我的问题: string priv_windows, who, msg; 这对于创建表单的新实例非常有效,但是在创建这些子表单之后,我找不到与它们通信的方法。我的意思是,举例来说,我如何在第一个表单实例中更改文本? 有没有一种简单的方法,可以为特定的表单实例设置索引或其

我正在用C#编写一种即时消息应用程序,它可以在套接字上工作。我管理了所有的套接字问题,现在它像IRC一样工作。然而,我想实现私有消息系统。我们有两种形式-一种是主要的,第二种是一个类似私人msg的原型。窗户应该是这样的。这是我的问题:

string priv_windows, who, msg;

这对于创建表单的新实例非常有效,但是在创建这些子表单之后,我找不到与它们通信的方法。我的意思是,举例来说,我如何在第一个表单实例中更改文本?
有没有一种简单的方法,可以为特定的表单实例设置索引或其他内容?请稍后简要说明如何使用它。

您可以创建一个列表并将表单保存在其中,例如

private List<Form2> forms = new List<Form2>();

public void createform(string who, string msg)
{    
    Form2 frm = new Form2();
    forms.Add(frm);
    // etc

使用任何集合来存储和引用表单实例

您需要保留对该集合的引用,以便用于与它们“通信”。与私有变量(如字符串)类似,例如:

public class Form2 : System.Windows.Forms.Form
{
    public System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();

    public void Main(string args[])
    {
        Form2 first = createform("1st instance", "some text");
        Form2 second = createform("2nd instance", "other text");
    }

    public Form2 createform(string who, string msg)
    {
        Form2 frm = new Form2();
        frm.Text = who;
        frm.label1.Text = msg;
        priv_windows += who += " ";
        frm.Show();
        return frm;
    }

    public void SetMessage(string message)
    {
        this.label1.Text = message;
    }
}
string priv_windows, who, msg;
List<Form> forms = new List<Form>();

public void createform(string who, string msg)
{    
    Form2 frm = new Form2();
    frm.Text = who;
    frm.label1.Text = msg;
    priv_windows += who += " ";
    forms.Add(frm);
    frm.Show();
}

public void ChangeChild()
{
    Form f = forms[0];
    f.Text = "New Form Title Text";
}
字符串priv_windows,who,msg;
列表形式=新列表();
public void createform(字符串who,字符串msg)
{    
Form2 frm=新Form2();
frm.Text=who;
frm.label1.Text=msg;
priv_windows+=谁+=”;
表格。添加(frm);
frm.Show();
}
public-void-ChangeChild()
{
表格f=表格[0];
f、 Text=“新表格标题文本”;
}

这只是一个操作集合中第一个表单的简单示例,如果有很多表单,则需要更复杂的方法来确定要更改的表单。

如果我解释void
CreateForm(String,String)的参数who,则,您希望与某个人的每次活动对话都有一个私人窗口

如果这是正确的,我将使用类
字典
(有关详细信息,请参阅)而不是
列表
来保存对对话窗口的引用


另一件好事是实现一个表单控制器类,它实际上有静态成员来管理所有使用过的表单。这将允许您在代码中的任何位置访问windows窗体。如果您不希望代码的每个部分都可以访问它,您可以使用名称空间来控制可访问性。

我建议使用与域中的对象通信

多谢各位。这是最好的办法。
foreach (Form2 form in forms)
{
    form.Text = "Hello world";
}
public class Form2 : System.Windows.Forms.Form
{
    public System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();

    public void Main(string args[])
    {
        Form2 first = createform("1st instance", "some text");
        Form2 second = createform("2nd instance", "other text");
    }

    public Form2 createform(string who, string msg)
    {
        Form2 frm = new Form2();
        frm.Text = who;
        frm.label1.Text = msg;
        priv_windows += who += " ";
        frm.Show();
        return frm;
    }

    public void SetMessage(string message)
    {
        this.label1.Text = message;
    }
}
string priv_windows, who, msg;
List<Form> forms = new List<Form>();

public void createform(string who, string msg)
{    
    Form2 frm = new Form2();
    frm.Text = who;
    frm.label1.Text = msg;
    priv_windows += who += " ";
    forms.Add(frm);
    frm.Show();
}

public void ChangeChild()
{
    Form f = forms[0];
    f.Text = "New Form Title Text";
}