1

I have a tab in my GUI I would like to be an auto-updating dev log. It currently only updates the initial time. How do I update this.devLogRichTextBox.Text with every change in DevLog.DevLog.log?

// 
// devLogRichTextBox
// 
this.devLogRichTextBox.Location = new System.Drawing.Point(3, 3);
this.devLogRichTextBox.Name = "devLogRichTextBox";
this.devLogRichTextBox.Size = new System.Drawing.Size(1195, 368);
this.devLogRichTextBox.TabIndex = 0;
this.devLogRichTextBox.Text = DevLog.DevLog.log; // This doesn't do what I want. This sets the text one time to string DevLog.DevLog.log

I would like to add lines to this log from anywhere in the program:

namespace DevLog
{
    public static class DevLog
    {
        public static string log = "Default";
        public static void addToLog(string addition)
        {
            log += addition + "\r\n";
        }

        public static void clearLog()
        {
            log = "";
        }

        public static void dumpLogToFile()
        {
            //Dump log to a file on HD
        }

        public void updateGUIDevLog()
        {
             //??
        }
    }
}

I have tried some things with pointers, but maybe the best option is to have it update at a fixed interval, or better yet, invoke some sort of update whenever a modification is made.

SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85

2 Answers2

1

You could have an event on your DevLog class which you fire every time the log is updated.

Add this field to your DevLog class

public static event EventHandler LogUpdated

Now, at the end of your addLog function

if (LogUpdated != null)
    LogUpdated(null, EventArgs.Empty); 

And now, in your GUI class:

DevLog.LogUpdated += (o, e) => 
{
    this.devLogRichTextBox.Text = DevLog.log;
};

Now, every time your log is updated it will fire the LogUpdated event

0

You could raise an event from the logger:

public static event EventHandler<string> LogLineAdded = delegate { };

public static void addToLog(string addition)
{
    string logLine = addition + "\r\n";
    log += logLine;
    LogLineAdded(null, logLine);
}

Then subscribe from the client form:

private void Form1_Load(object sender, System.EventArgs e)
{
    DevLog.LogLineAdded += DevLog_LogLineAdded;
}

private void DevLog_LogLineAdded(object sender, string logLine)
{
    this.devLogRichTextBox.Text += logLine;
}

private void Form1_FormClosed(object sender, System.EventArgs e)
{
    DevLog.LogLineAdded -= DevLog_LogLineAdded;
}

Unsubscribing from a static event from an instance handler is important.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272