字符串变量更改时未触发事件

tf.*_*.rz 0 c# event-handling

GUI有一个显示事件日志的按钮,这是另一个带有富文本框的表单.

我正在制作一个事件日志,告诉用户发生了什么.我遇到的问题是我不知道为什么我设置的事件处理程序在字符串更改时没有被触发.基本上,此表单有一个文本框,文本框设置为一个名为"活动"的字符串.此字符串会添加有关于状态和错误的各种消息.

但是,当字符串与其他消息连接时,事件不会被触发,也不会自动更新.我正在寻找这些事件发生时的实时更新.现在,我可以关闭表单,然后重新打开它,让它再次加载活动字符串,这是有效的,但有人可以解释为什么我设置的事件没有被触发?这是代码:第一位是我发布的新表单.第二位是具有字符串活动的类.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StatusLog
{
    public partial class StatusLogForm : Form
    {
        public StatusLogForm()
        {
            InitializeComponent();
        }
        private StatusLog statuslog = StatusLog.Instance;


        private void StatusLogForm_Load(object sender, EventArgs e)
        {
            statuslog.ActivityChanged += new EventHandler(statuslog_ActivityChanged);
            richTextBox1.Text = statuslog.Activity;
        }

        void statuslog_ActivityChanged(object sender, EventArgs e)
        {
            richTextBox1.Text = statuslog.Activity;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDCanada.LCSS.VMF.ProtoVmf;

namespace StatusLog
{
    public class StatusLog
    {
        private static StatusLog instance;
        private StatusLog()
        {
        }

        public static StatusLog Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new StatusLog();
                }
                return instance;
            }
        }

        public enum LogType
        {
            Error,
            Status,
            Information,
            Warning,
        }

        public event System.EventHandler ActivityChanged;
        private string activity = "Activity:";
        public string Activity
        {
            get
            {
                return activity;
            }
            set
            {
                activity = value;
                if (this.ActivityChanged != null)
                {
                    this.ActivityChanged(this, new System.EventArgs());
                }
            }
        }
        void activitychanged(object sender, System.EventArgs e)
        {
        }

        public void Log(string message, LogType type)
        {
            activity = activity + "\n" + type.ToString() + ": " + message;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我通过'Log'函数添加字符串.非常感谢任何帮助,谢谢!如果我错过了任何信息,我很抱歉.如果需要,我很乐意提供.

-tf.rz .NET 3.5 SP1

Jon*_*eet 5

看看这个方法:

public void Log(string message, LogType type)
{
    activity = activity + "\n" + type.ToString() + ": " + message;
}
Run Code Online (Sandbox Code Playgroud)

您正在直接更改变量 - 除了更改变量的值之外什么都不做.

如果您希望执行属性代码(从而引发事件),您应该:

public void Log(string message, LogType type)
{
    // Note use of property
    Activity = Activity + "\n" + type.ToString() + ": " + message;
}
Run Code Online (Sandbox Code Playgroud)

(当然,你不必使用属性getter - 它只是重要的setter.)