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 4081216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:01:56+00:00 2026-05-20T18:01:56+00:00

we have a problem when trying to create events for a custom control in

  • 0

we have a problem when trying to create events for a custom control in WPF. We have our code like this:


public static readonly RoutedEvent KeyPressedEvent =
            EventManager.RegisterRoutedEvent(
                "keyPressed", RoutingStrategy.Bubble,
                    typeof(KeyEventHandler), typeof(Keyboard));

    public event KeyEventHandler keyPressed
    {
        add { AddHandler(KeyPressedEvent, value); }
        remove { RemoveHandler(KeyPressedEvent, value); }
    }

void btnAlphaClick(object sender, RoutedEventArgs e)
    {
        var btn = (Button)sender;
        Key key = (Key)Enum.Parse(typeof(Key), btn.Content.ToString().ToUpper());
        PresentationSource source = null;
        foreach (PresentationSource s in PresentationSource.CurrentSources)
        {
            source = s;
        }
        RaiseEvent(new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice, source,0,key));

The control is an on screen keyboard, and we basically need to pass out to the KeyPressedEventArgs to the subscribers to the event detailing what key was pressed (we can't find much that helps us with this in WPF, only winforms).

Any help, greatly appreciated!

  • 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-05-20T18:01:56+00:00Added an answer on May 20, 2026 at 6:01 pm

    Step 1: Add Event Handler to OK and Cancel Button

    private void btnOK_Click(object sender, RoutedEventArgs e)
    {     
    }
    
    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {     
    }
    

    Add the public property in the UserControl1.xaml.cs file to share the value of the textbox with the host

    public string UserName
    {
        get { return txtName.Text; }
        set { txtName.Text = value; }
    }
    

    Declare the Events for Ok and Cancel Buttons which can be subscribed by Windows Form.

    public event EventHandler OkClick;
    public event EventHandler CancelClick;
    

    Now add the code to the event handler so that we can raise the event to host also.

    private void btnOK_Click(object sender, RoutedEventArgs e)
    {
        if (OkClick != null)
            OkClick(this, e);
    }
    
    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
        if (CancelClick != null)
            CancelClick(this, e);
    }
    

    Step 2: Handle the WPF Control Event in Windows Form

    Add Handler to OKClick and CancelClick Events just after creating the instance of the user control

    _WPFUserControl.OkClick += new EventHandler(OnOkHandler);
    _WPFUserControl.CancelClick += new EventHandler(OnCancelHandler);
    

    Write code in the handler method. Here I user the UserName property in the OK button handler so show the how to share the values also.

    protected void OnOkHandler(object sender, EventArgs e)
    {
        MessageBox.Show("Hello: " +_WPFUserControl.UserName + " you clicked Ok Button");
    }
    
    protected void OnCancelHandler(object sender, EventArgs e)
    {
        MessageBox.Show("you clicked Cancel Button");
    }
    

    Reference:
    http://a2zdotnet.com/View.aspx?Id=79

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

Sidebar

Related Questions

I have a problem with my WPF program. I'm trying to create an object
I am trying to learn ASP.NET MVC and I hit this problem: I have
I have been trying to tackle this problem , but I am having difficulty
I have a problem I'm trying to solve involving interfaceing a C++ program with
I discovered to have some problem to fully understand callbacks scoping when trying to
I have a small math problem I am trying to solve Given a number
In trying to solve the ajax back button problem I have found the Really
I'm trying to solve the 3n+1 problem and I have a for loop that
I'm trying out ASP.NET MVC routing and have of course stumbled across a problem.
I have a MySQL Left Join problem. I have three tables which I'm trying

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.