Abin Thomas
Abin Thomas

Reputation: 11

Dynamically created form in C# and manipulating them

i have created a C# windows form which retrieves data from MS Access and according to the first column of that data separate child forms will be dynamically created. However for me if any data is a copy of already created form then instead of creating a new form i need to access the previously created form. Can someone help me in this...

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

Upvotes: 1

Views: 307

Answers (1)

Doc Brown
Doc Brown

Reputation: 20044

Some untested code, I think you get the idea:

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

Upvotes: 1

Related Questions