Solved

event not firing for a composite control

Posted on 2012-03-27
8
422 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Achieve json result 2 65
Class object 2 28
Export import database 4 45
I need help making a collection from a class in VB.Net 3 27
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now