在C#中动态创建表单并操纵它们

时间:2011-10-08 17:55:23

标签: c# winforms

我创建了一个C#窗体,它从MS Access检索数据,根据该数据的第一列,将动态创建单独的子窗体。但是对我来说,如果任何数据是已经创建的表单的副本,那么我需要访问以前创建的表单而不是创建新表单。有人可以帮助我...

ChatWindow tempwindow = new ChatWindow();
while (aFromReader.Read()) //aFromReader retrieves the first column from a Table
{
    OleDbCommand aCommand = new OleDbCommand("select * from Messages",aConnection);
    OleDbDataReader aMessage = aCommand.ExecuteReader();

    if (this.Text != aFromReader.GetValue(0).ToString())

    {
        tempwindow = new ChatWindow();
        tempwindow.Text = aFromReader.GetValue(0).ToString();
        tempwindow.Show();
    }

1 个答案:

答案 0 :(得分:1)

一些未经测试的代码,我认为你明白了这个想法:

using System.Collections.Generic;
// ...

Dictionary<string,ChatWindow> windowDict = Dictionary<string,ChatWindow>();

while (aFromReader.Read()) 
{
    OleDbCommand aCommand = new OleDbCommand("select * from Messages",aConnection);
    OleDbDataReader aMessage = aCommand.ExecuteReader();

    string windowText = aFromReader.GetValue(0).ToString();
    if(windowDict.Contains(windowText))
    {
        // do something with windowDict[windowText]
    }
    else
    {
        tempwindow = new ChatWindow();
        tempwindow.Text = windowText;
        windowDict.Add(windowText,tempwindow);
        tempwindow.Show();
    }
}