Solved

event not firing for a composite control

Posted on 2012-03-27
8
421 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

12 Experts available now in Live!

Get 1:1 Help Now