[ DarkMan @ 25.06.2008. 14:37 ] @
Klasa forme sadrzi promenljivu state i CheckBox koji vizuelno predstavlja vrednost ove promenljive. Kada korisnik klikne na CheckBox CheckedChanged dogadjaj se aktivira u kojem se menja vrednost promenljive state da bude jednako stanju CheckBox kontrole.
Na formi je takodje postavljeno dugme na ciji se klik prvo menja vrednost promenljive state a zatim se menja stanje CheckBox kontrole da bi vrednosti bile konzistentne. Kada se u samom kodu promeni stanje CheckBox-a automatski se aktivira CheckedChanged dogadjaj sto nije potrebno/pozeljno.

Moje pitanje je, za one koji su imali takvu situaciju, na koje sve nacine se moze izbeci ovo?
Ja sam uvek koristio jednu bool promenljivu, na primer wait kojom bi privremeno onemogucio izvrsavanje koda u dogadjajima.

Code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }

    public class Form1: Form
    {
        bool state = false;

        bool wait = false;

        CheckBox check = new CheckBox();
        Button button = new Button();
        TextBox text = new TextBox();
        public Form1()
        {
            check.Location = new Point(20, 20);
            button.Location = new Point(150, 20);
            text.Location = new Point(20, 100);
            text.Multiline = true;
            text.Size = new Size(260, 160);
            this.Controls.Add(check);
            this.Controls.Add(button);
            this.Controls.Add(text);
            this.Size = new Size(300, 300);
            check.Checked = state;
            check.CheckedChanged += new EventHandler(check_CheckedChanged);
            button.Click += new EventHandler(button_Click);
        }

        void check_CheckedChanged(object sender, EventArgs e)
        {
            if(wait) return;
            text.AppendText("CheckedChanged" + Environment.NewLine);
            state = check.Checked;
        }
        void button_Click(object sender, EventArgs e)
        {
            wait = true;
            text.AppendText("State changed" + Environment.NewLine);
            state = !state;
            check.Checked = state;
            wait = false;
        }
    }
}
[ deerbeer @ 25.06.2008. 15:03 ] @
Mozes i da ukljucujes i iskljucujes event-e po potrebi ...
Code:

void button_Click(object sender, EventArgs e)
{
   check.CheckedChanged -= new EventHandler(check_CheckedChanged);
   text.AppendText("State changed" + Environment.NewLine);
   state = !state;
   check.Checked = state;
   check.CheckedChanged += new EventHandler(check_CheckedChanged);

}

Onda se event check_CheckedChanged nece ni pozivati ..