2011-10-08 62 views
1

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

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(); 
    } 
+0

,如果您告诉我们您的表单创建的代码示例,这将是更更容易给你一个合理的答案。 –

+0

你只能使用c#2.0吗?如果不是,请将其重新标记为c# –

回答

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(); 
    } 
} 
+0

谢谢布朗博士。那将会有所帮助。我现在会尝试。 –

相关问题