understanding events

This is a discussion on understanding events within the C# Programming forums, part of the General Programming Boards category; Another Home work problem, but have been at it a bit so thought I would throw something up. I have ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    understanding events

    Another Home work problem, but have been at it a bit so thought I would throw something up.

    I have defined a custom control that has an event that is called BoolChanged that should be triggered when the bool DecimalPointAllowed is changed to false by a check box. When the event is triggered a label shoud let us know that that the "Decimal point is allowed"

    The checked check box is changing the DecimalPointAlowed to false, but the even is not being triggered and the label is not changing.

    This is the custom control with the event.

    Code:
        public class DoubleValueEdit : TextBox
        {
            internal delegate void MyBoolValueChanged(bool Was, string result);
            internal event MyBoolValueChanged BoolChanged;
    
            public bool DecimalPointAllowed;
            string a = "a";
    
            public double Value
            {
                get
                {
                    double Val;
                    if (double.TryParse(Text, out Val))
                        return Val;
                    else
                        return 0;
                }
                set
                {
                    Text = value.ToString();
    
                    if ((DecimalPointAllowed = true) && (BoolChanged != null))
                        BoolChanged(DecimalPointAllowed, a);
               }
            }
    Here is the Check box code and label that should change upon checking the checkbox. By calling the event based on the bool value changing.
    Code:
        public partial class Form1 : Form
        {
            string result2;
            public Form1()
            {
                InitializeComponent();
    
            }
                   
            private void doubleValueEdit1_TextChanged(object sender, EventArgs e)
            {
                dvLabel.Text = doubleValueEdit1.Value.ToString();
                doubleValueEdit1.BoolChanged += new DoubleValueEdit.MyBoolValueChanged(doubleValueEdit1_BoolChanged);
            }
    
            void doubleValueEdit1_BoolChanged(bool Was, string result)
            {
                result2 = Was.ToString() + result + "Decimal Point Allowed";
            }
    
            private void ChBoolValCheckBox_CheckedChanged(object sender, EventArgs e)
            {
                if (ChBoolValCheckBox.Checked)
                    doubleValueEdit1.DecimalPointAllowed = false;
              
            }
    Last edited by jefcoatv; 04-02-2011 at 04:37 PM.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Code:
    if ((DecimalPointAllowed = true) && (BoolChanged != null))
    change that to ==

    Also you have to actually set Value property to something to get your event to fire to listeners.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Understanding Some Basics
    By TheProfessor in forum C Programming
    Replies: 2
    Last Post: 10-02-2010, 11:58 AM
  2. Embed browser dont receives mouse "click" events
    By aniquilator in forum Windows Programming
    Replies: 2
    Last Post: 01-25-2010, 06:41 AM
  3. Reliable UDP communication for merging events in a distributed system
    By dwks in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-26-2009, 04:36 PM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 11:56 AM
  5. seismic events program
    By santaclaus in forum C Programming
    Replies: 16
    Last Post: 11-23-2003, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21