Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:18:24+00:00 2026-06-09T15:18:24+00:00

All of my controls inherit from a base class that creates and assigns a

  • 0

All of my controls inherit from a base class that creates and assigns a OnAccept and OnCancel to the Enter and ESC keys.

private readonly Button _accept, _cancel;

public ViewUserControl()
{
    _accept = new Button();
    _cancel = new Button();

    _accept.Click += (o, e) => OnAccept();
    _cancel.Click += (o, e) => OnCancel();
}

// the base function depends on the child functions to implement a accept/cancel function, if it doesn't then those events will fire to the
// new button and not be used for anything
public virtual IButtonControl GetAcceptButton()
{
    return _accept;
}
public virtual IButtonControl GetCancelButton()
{
    return _cancel;
}

protected virtual void OnAccept() { }
protected virtual void OnCancel()
{
    this.ClosingEvent();
}

However, when the user is in a multiline textbox, the enter key is kicking off the OnAccept of the form rather than putting a new line into the textbox (which is the expected behaviour).

Currently, to get around this, i have to find the focused control of the form and if it’s a textbox, then manually put the newline in. However when I do this, the cursor resets to the start of the textbox.

protected override void OnAccept()
{
    var focused = FindFocusedControl(this);
    if (focused is TextBox)
    {
        focused.Text += Environment.NewLine;
    }
    else
    {
        base.OnAccept();
    }
}

public static Control FindFocusedControl(Control control)
{
    var container = control as ContainerControl;
    while (container != null)
    {
        control = container.ActiveControl;
        container = control as ContainerControl;
    }
    return control;
}

My questions are:

Is there a way to bypass the OnAccept event so the enter event is recognised by the textbox?

Is there a way to call the textbox’s enter event manually?

How do I set the cursor to the end of the textbox after I manually put in a line break?

An answer to any of these questions will achieve the result that I’m after, ordered in preference of solution.

UPDATE:

I did find a way to move the caret (not cursor as I called it in the original question) to the end using RichTextBox.SelectionStart however, I’d prefer a more elegant solution.

UPDATE 2:

For anyone else with the same problem, this is what I now do:

From the child control:

txtDetails.GotFocus += (o,e) => AcceptButtonStatus(false);
txtDetails.LostFocus += (o, e) => AcceptButtonStatus(true);

From the base control:

protected void AcceptButtonStatus(bool enabled)
{
    this.ParentForm.AcceptButton = enabled?_accept:null;
}

So whenever the textbox gets focus, I remove the accept button from the form.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:18:26+00:00Added an answer on June 9, 2026 at 3:18 pm

    Here is a post on how to externally call component events.

    How can I programmatically generate keypress events in C#?

    As for the Accept event, your dialog is intercepting that before the control ever sees it. The only thing that might work would be to add a form event that watches focus changes and if the focus is a multiline text control, you set the AcceptButton control for the form to null (assuming you are using AcceptButton and CancelButton to generate the Accept/Cancel events).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1)I have the PageBase class that inherits from Page Class. The all code behind
I have an application with plugin-like structure. all application forms inherit from a base
I have a common base class from which all my ASMX webservice classes will
I want to create an abstract base class for all paramter-type classes to inherit
I am trying to create a series of UserControls that all inherit from a
I created my own user controls that inherit from the standard .net controls (for
I have heard that GTK covers all controls so that developers do not care
I am trying to make something that will enable all controls if a SubString
I am working on modifying a control on a existing site. All controls from
I need to get all controls inside a specific RowDefinition/ColumnDefinition without iterating through all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.