简体   繁体   English

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

[英]Event not being fired when string variable changes

A GUI has a button that displays the event log, which is just another form with a rich text box. GUI具有显示事件日志的按钮,它是带有富文本框的另一种形式。

I'm making an event log that tells the user what's going on. 我正在制作一个事件日志,以告诉用户发生了什么事。 The problem I have is that I don't know why the event handler i set up is not being fired when a string changes. 我的问题是我不知道为什么更改字符串时未触发我设置的事件处理程序。 Basically, this form has a textbox and the textbox is set to a string called 'activity'. 基本上,此表单具有一个文本框,并且该文本框设置为名为“活动”的字符串。 This string gets added on to with various messages about status and errors. 该字符串被添加到有关状态和错误的各种消息中。

However, when the string get concatenated with other messages, the event does not get triggered and it won't automatically update itself. 但是,当字符串与其他消息串联在一起时,该事件不会被触发,并且不会自动更新。 I'm looking for a real-time update as these events happen. 这些事件发生时,我正在寻找实时更新。 Right now, I can close the form and then reopen it to get it to load the activity string again, and that works, but can someone shed some light on why the event i set up is not being triggered? 现在,我可以关闭该表单,然后重新打开它以使其再次加载活动字符串,该方法可以正常工作,但是有人可以阐明为什么我未触发设置的事件吗? Here is the code: The first bit is the New form that I launch. 这是代码:第一部分是我启动的New表单。 The second bit is the class with the string activity. 第二位是具有字符串活动的类。

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

I add the string via the 'Log' function. 我通过“日志”功能添加字符串。 Any help is greatly appreciated, thanks! 非常感谢任何帮助,谢谢! My apologies if I missed any information. 如果我错过任何信息,我深表歉意。 I will be happy to provide any if needed. 如果需要,我将很乐意提供。

-tf.rz .NET 3.5 SP1 -tf.rz .NET 3.5 SP1

Look at this method: 看一下这个方法:

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

You're changing the variable directly - which does nothing but change the variable's value. 您正在直接更改变量 -除了更改变量的值外,它什么也不做。

If you want the property code to execute (and thus raise the event) you should do: 如果希望属性代码执行(从而引发事件),则应该执行以下操作:

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

(You don't have to use the property getter of course - it's only the setter that's important.) (当然,您不必使用属性获取器,它只是重要的设置器。)

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

You need to call the Property of the class, not the member variable. 您需要调用类的Property,而不是成员变量。

 public void Log(string message, LogType type)
 {
        Activity = Activity + "\n" + type.ToString() + ": " + message;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM