user2131323
user2131323

Reputation: 105

Call a method on an already open form using differnat forms

I have been trying to solve this for a while now. I have checked simar posts but non of the sollutions seem to work.

My problem is that i'm trying to call the method 'Write' from a form called 'Log'.

public void ActivityLog(string LogString)
{
    //Log what the system is doing for the user to see
    if (LogAll == true)
        {
            //check if the error logging form is open
            if (Application.OpenForms["Log"] != null)
            {
                //write to the log form

            }
            else
            {
                //the error log page is not yet open
                Log LogFrm = new Log();
                LogFrm.Show();

                //now the form is open log the error
                LogFrm.Write(LogString);
            }
        }
    }
}

Now when i run the program and I call 'ActivityLog', the 'Log' form does open and the string I enter does appear, the second time I click the button the program goes to where i commented 'write to the log form'. But I cannot find a way to call the method from the aleady open form again.

The form 'Log' will stay open throughout the use of the program and will be added to from differant forms all calling the 'ActivityLog' method. Any advice will be much appreciated.

Upvotes: 1

Views: 1065

Answers (1)

jtimperley
jtimperley

Reputation: 2544

Try something like this.

var LogFrm = Application.OpenForms["Log"] as Log;
if (LogFrm == null)
{
    LogFrm = new Log();
    LogFrm.Show();
}

LogFrm.Write(LogString);

Upvotes: 1

Related Questions