Solved

event not firing for a composite control

Posted on 2012-03-27
8
430 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:Mydeen Yussouf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 86

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 86

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:Mydeen Yussouf
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 86

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:Mydeen Yussouf
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 86

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:Mydeen Yussouf
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 86

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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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