CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Mar 2003
    Posts
    145

    Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    I have a UserControl that contains lots of TextBoxes, ComboBoxes, Buttons, etc.
    The UserControl has one specific button that I want to be clicked whenever the user presses 'Enter' on any of the controls contained in the UserControl.

    If I were dealing with a Form, I would use its ‘AcceptButton’ property.
    What do I do when dealing with a UserControl?

    One way is to implement the ‘KeyDown’ event of each control, check if the ‘Enter’ key was pressed, and if so call the ‘Click’ event of that button.
    But, is there another solution?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    Set the IsDefault = true property on the button you are interested in.

  3. #3
    Join Date
    Mar 2003
    Posts
    145

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    Quote Originally Posted by Arjay
    Set the IsDefault = true property on the button you are interested in.
    How do I use the IsDefault property? It's protected.

    Will this property set this button to be the default button on its UserContol, (that's my intention), or on the whole application?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    The title of your post talks about 'in a form', but the text talks about 'if I were using a form'.

    Can you clarify? Are you in ASP.Net web application, a Windows form, or WPF?

  5. #5
    Join Date
    Mar 2003
    Posts
    145

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    I am working on a Windows Form application written in C-Sharp.
    Should have mentioned that before. Sorry.

    (By the way, wasn’t my first post clear? I said there that if I were dealing with a Form, I would use its ‘AcceptButton’ property. But, as it is, I am dealing with a UserControl that doesn’t have this property. Therefore, I was wondering whether a UserControl has something similar. Just like I say in the titile: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    Since the IsDefault is protected, try inheriting from the button.

    Code:
    public partial class MyButton : Button
    {
     public new bool IsDefault { get { return base.IsDefault; } set { base.IsDefault = value; } }
    }

  7. #7
    Join Date
    Mar 2003
    Posts
    145

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    I tried doing as you suggested, but it doesn't seem to work.

    This is what I did:
    - I've created a MyButton class derived from the Button class, that implements the IsDefault property. (Just as you wrote).
    - I've added an instance of MyButton to my UserControl.
    - I've set the IsDefault property to true.
    - I've implemented the Click event.

    When the application is activated, it looks as if this button has focus, but clicking on the 'Enter' key does not fire the Click event.
    Did I do something wrong?

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    There is actually several methods, luckily not so complicated

    The easiest method to use is to use the UserControl's TopLevelControl property and determine if the usercontrol is placed on a Windows Form. Then, if it is placed on a Form, that means that the UserControl's TopLevelControl needs to be cast into a Form, then, we can utilise the AcceptButton property.

    I'm attaching a sample here for you.
    On the sample, I've placed 2 buttons on the usercontrol, one will be used as the AcceptButton.

    In the UserControl's Load event, I did this :
    Code:
    		private void UserControl1_Load(object sender, System.EventArgs e)
    		{
    			if (this.TopLevelControl is Form) 
    			{ 
    				((Form)this.TopLevelControl).AcceptButton = MyAcceptButton; 
    				
    			} 
    		}
    Then, the button I want to use as the AcceptButton, I did this in it's Click event :
    Code:
    		private void MyAcceptButton_Click(object sender, System.EventArgs e)
    		{
    			MessageBox.Show("Works!");
    		}
    I then added the usercontrol to the form, and ran it. When I pressed Enter, the usercontrol's button fired.

    Does this help ¿
    Last edited by HanneSThEGreaT; June 14th, 2010 at 05:40 AM.

  9. #9
    Join Date
    Mar 2003
    Posts
    145

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    I tried what you suggested, and it works. Thanks!

    The only thing is that I have a few UserControls on my main form, and I would like the specific button to act as an ‘AcceptButton’ only when the UserControl that owns this button, is active. (Meaning, that if another UserContol is active, and the user clicks ‘Enter’, the ‘Click’ event of the button won’t be fired).

    Is the way to do so is to implement the ‘Enter’ & ‘Leave’ events of the UserControl?
    (In the ‘Enter’ event set the ‘AcceptButton’ property to this button, and in the ‘Leave’ event set the ‘AcceptButton’ to null)

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    Just add a Property to determine Whether or not the AcceptButton fired. If it hasn't fired yet, fire it, if it did fire already, don't execute again.

    Add this variable to your UserControl:
    Code:
    		private bool EnterPressed;
    Then add this Property :
    Code:
    		private bool EnterKeyPressed
    		{
    			get
    			{
    				return EnterPressed;
    			}
    			set
    			{
    				EnterPressed = value;
    			}
    		}
    And lastly, just modify your MyAcceptButton_Click event to look like :
    Code:
    		private void MyAcceptButton_Click(object sender, System.EventArgs e)
    		{
    			if (!EnterKeyPressed)
    			{
    				MessageBox.Show("Works!");
    				EnterKeyPressed = true;
    			}
    		}
    This will fire only once. Now, the benefit of having this as a property is so that you can set this property from anywhere you need on the form.

    Does this help ¿

  11. #11
    Join Date
    Mar 2003
    Posts
    145

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    I don’t understand how what you wrote helps me.
    Can you explain, please?

    But, first let me try to explain my problem again. (Hopefully, more clearly …)
    Let’s say I have 3 UserControls on my form: MyUserCtrl, UserCtrl2, & UserCtrl3. All of them contain buttons, text-boxes, etc.
    When MyUserCtrl is in focus, I would like pressing ‘Enter’ to fire the ‘Click’ event of a specific button on this UserControl.
    If MyUserCtrl is not in focus, pressing ‘Enter’ should not do so.

    How can I achieve that?

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    I had a suspicion this would happen

    Have a look at the revised attachment, and test it
    Last edited by HanneSThEGreaT; June 14th, 2010 at 05:30 AM.

  13. #13
    Join Date
    Mar 2003
    Posts
    145

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    At the beginning, when I click ‘Enter’ the 'MyAcceptButton_Click' event is fired, and I see the message box. (So far so good).
    But, then the message box is never shown again because of the ‘if sentence’.
    Code:
    private void MyAcceptButton_Click(object sender, System.EventArgs e)
    {
         if (!EnterKeyPressed)
        {
             MessageBox.Show("Works!");
             EnterKeyPressed = true;
         }
    }
    But, what does the if sentence suppose to do?

    I took your sample, and added another UserControl called DummyUserControl to it.
    This is what I want to happen:
    - When ‘AcceptButUC’ is active, clicking ‘Enter’ fires ‘MyAcceptButton_Click’ and displays the message box.
    - When ‘DummyUserControl’ is active, clicking ‘Enter’ does not fire ‘MyAcceptButton_Click’

    I’ve attached your revised solution.
    Attached Files Attached Files

  14. #14
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    Easy!
    You just need to use the UserControl's Enter and Leave events, to determine if it is active or not.

    I have modified your project. Have a look at it.
    It should be what you need
    Last edited by HanneSThEGreaT; June 14th, 2010 at 05:30 AM.

  15. #15
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?

    Actually, all we need is the Enter and Leave events. We don't really need the EnterPressed property now anymore.

    So we can just use:
    Code:
    		private void UserControl1_Load(object sender, System.EventArgs e)
    		{
    			if (this.TopLevelControl is Form) 
    			{ 
    				((Form)this.TopLevelControl).AcceptButton = MyAcceptButton; 
    				
    			} 
    		}
    
    		private void MyAcceptButton_Click(object sender, System.EventArgs e)
    		{
    			if (Focussed)
    			{
    				MessageBox.Show("First Control Works!");
    			}
    		}
    
            //private bool EnterKeyPressed
            //{
            //    get
            //    {
            //        return EnterPressed;
            //    }
            //    set
            //    {
            //        EnterPressed = value;
            //    }
            //}
    
            private void UserControl1_Enter(object sender, EventArgs e)
            {
                Focussed = true;
            }
    
            private void UserControl_Leave(object sender, EventArgs e)
            {
                Focussed = false;
            }
    	}
    And it should be fine

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured