Solved

event not firing for a composite control

Posted on 2012-03-27
8
425 Views
Last Modified: 2012-04-17
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
0
Comment
Question by:olmuser
  • 5
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37772247
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

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37772251
Here's the form trapping the custom event:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void userControl11_UserControlLeave(UserControl1 uc, string value)
        {
            Console.WriteLine(value);
        }

    }

Open in new window

0
 
LVL 1

Author Comment

by:olmuser
ID: 37772417
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?
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37772450
Where was the focus before you hit the tab key twice?  Where does focus move to on the first tab?
0
 
LVL 1

Author Comment

by:olmuser
ID: 37772500
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

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37772578
It's working for me with only ONE tab.  What else is on the form?
0
 
LVL 1

Author Comment

by:olmuser
ID: 37772966
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37772994
I still can't reproduce the symptom.  Even when clicking, or double clicking into the control, a single tab takes me out.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question