Link to home
Start Free TrialLog in
Avatar of ol muser
ol muserFlag for United States of America

asked on

event not firing for a composite control

Hi, I have a composite control that currently only has one masked edit box. I would like to capture the value of text entered in the text box when focus leaves the composite control. I was surprised to see the Leave event available for a composite control and I was excited as that would solve my problem, but that does not fire for me. In the containing class for the composite control, a winfows form, I need to capture the value entered in the masked edit box and without that its hard for me to capture the values properly. Not sure if this would make a difference, but for the masked edit control I ahve the following events defined:

maskededit_TypeValidationCompleted
maskededit_KeyDown
maskededit_DoubleClick
maskededit_Leave

And below is the hierarchy of my controls:

-----------
windows form
-----------
composite
-----------
maskediedit
-----------
-----------
-----------

Environment: c# using visual studio 2008
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You could raise a custom event from the MaskedTextBox:
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public delegate void UserControlLeaveDelegate(UserControl1 uc, string value);
        public event UserControlLeaveDelegate UserControlLeave;

        private void maskedTextBox1_Leave(object sender, EventArgs e)
        {
            if (UserControlLeave != null)
            {
                UserControlLeave(this, maskedTextBox1.Text);
            }
        }

    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ol muser

ASKER

great help thanks! But I can see that the custom leave evetn as well as the maskededit_KeyDown event both are fired only when I press the same key (tab) twice but not the first time...any idea there?
Where was the focus before you hit the tab key twice?  Where does focus move to on the first tab?
Just in case the whole class is below. In addition I have the custom event handler in the containing form as you have recommended, but the event fires only the 2nd time I press the tab key!!!!!! thanks in advance:

namespace TxDateTimePicker
{
    public partial class TxDateTimeControl : UserControl
    {
        private string m_DateTimeSelected;

        public delegate void UserControlLeaveDelegate(TxDateTimeControl uc, string value);
        public event UserControlLeaveDelegate UserControlLeave;


        public TxDateTimeControl()
        {
            InitializeComponent();
        }

        private void TxDateTimeControl_Load(object sender, EventArgs e)
        {
            mtbDateTime.ValidatingType = typeof(System.DateTime);
            mtbDateTime.TypeValidationCompleted += new TypeValidationEventHandler(mtbDateTime_TypeValidationCompleted);
        }

        private void mtbDateTime_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
        {
            if (!e.IsValidInput)
            {
                ttErrorMsg.ToolTipTitle = "Invalid Date";
                ttErrorMsg.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", mtbDateTime, 25, -60, 5000);
            }
        }


        private void mtbDateTime_DoubleClick(object sender, EventArgs e)
        {
            mtbDateTime.Text = System.DateTime.Today.ToString("MM/dd/yyyy");
            m_DateTimeSelected = mtbDateTime.Text;
        }

        private void mtbDateTime_Leave(object sender, EventArgs e)
        {
            //m_DateTimeSelected = mtbDateTime.Text;
            if (UserControlLeave != null)
            {
                UserControlLeave(this, mtbDateTime.Text);
            }

        }

        public string DateTimeSelected {
            get { return m_DateTimeSelected; }
            set { m_DateTimeSelected = value; mtbDateTime.Text = m_DateTimeSelected; }
        }
    
    }
}

Open in new window

It's working for me with only ONE tab.  What else is on the form?
On the form I have other typical windos controls. What I found is when I traverse using tab key to arricve at this control then it works fine, a single tab is sufficient to move it to the next control. But if I use a mouse click (single or double) then it requires two tabs.

For now as a work around I ahve removed the masked edit in the composite control and using a text box, to avoid this.

thanks
I still can't reproduce the symptom.  Even when clicking, or double clicking into the control, a single tab takes me out.