0 Replies Latest reply on Feb 14, 2012 9:00 AM by lucio menci

    creating classes with events and manage them

    lucio menci

      Hi,

       

      I'm new there, and I like to see the Flex usability.

      There are some days I'm searching to understand the class usage, but the copy and paste of source coude found into forums and into reference pages isn't enough for me.

      I came from Windows sdk (visual studio), and I wanted to see difference between the two sdks. I'd like to create classes using code that makes them interact with each other.

       

      I made a simple source code in VisualStudio sdk (C#, I think it's so simple to understand it) and I like that someone help me to understand Flex classes translating it as closely as possible.

       

      using System.Windows.Forms;

       

      namespace MyApplication
      {
        public class myForm : Form
        {
          // Definitions
          private myTxt[] txtBoxes;
          private Label lblResult;

       

          // Form construction
          public myForm()
          {
            // myTxt is a derived class of TextBox, with a new property, an instance of a class with events, designed by me
            // Generate some textboxes
            txtBoxes = new myTxt[6];
            for (int i = 0; i <= 5; i++)
            {
              // Designing the textboxes
              txtBoxes[i] = new myTxt();
              txtBoxes[i].Width = 150;
              txtBoxes[i].Top = 35 * i;
              txtBoxes[i].assignedProp = new myClassWithEvents();
              txtBoxes[i].assignedProp.Name = "Class Nr. " + i;
              // add them to the form
              this.Controls.Add(txtBoxes[i]);
              // managing events
              txtBoxes[i].TextChanged += this.Txt_TextChanged;
              txtBoxes[i].assignedProp.PropertyChanged += this.propChanged;
            }
            // another control: this will show the result
            lblResult = new Label();
            lblResult.Top = 200;
            lblResult.Width = this.Width;
            this.Controls.Add(lblResult);
          }

       

          // Changing the text of the control, changes something into some class (the one into the myTxt)
          private void Txt_TextChanged(object sender, System.EventArgs e)
          {
            myTxt txtSender = (myTxt)sender;
            txtSender.assignedProp.Value = txtSender.Text;
          }

       

          // an event into a class shows me that someting was appened
          private void propChanged(myClassWithEvents sender)
          {
            lblResult.Text = "PropertyName: " + sender.Name + " - new value: " + sender.Value;
          }
        }

       

        public class myTxt : TextBox
        {
          public myTxt() : base()
          {
          }
          // A new property: a mydisenged class with events
          public myClassWithEvents assignedProp;
        }

       

        // a new class with an event
        public class myClassWithEvents
        {
          // the argument type of the event (the event signature)
          public delegate void PropertyChangedEventHandler(myClassWithEvents sender);
          // the event with the previous signature
          public event PropertyChangedEventHandler PropertyChanged;

       

          // two properties: the first is a simple variable, otherwhile setting the second dispatches the event
          public string Name;
          private string _Value;

       

          public string Value
          {
            get
            {
              return _Value;
            }
            set
            {
              if (_Value != value)
              {
                _Value = value;
                PropertyChanged(this);
              }
            }
          }
        }
      }

       

      Can someone help me? Thank you