简体   繁体   中英

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.

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. 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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