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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:05:08+00:00 2026-06-10T21:05:08+00:00

Using a windows forms ListBox, how can I bind both double-click and return key

  • 0

Using a windows forms ListBox, how can I bind both double-click and return key to a single action. The way I have it I just copied the same action into both listBox1_MouseDoubleClick and listBox1_KeyUp.

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
    }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.textBox1.Text = this.listBox1.SelectedItem.ToString(); // Repeated
    }

    private void listBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            this.textBox1.Text = this.listBox1.SelectedItem.ToString(); // Repeated
        }
    }
}

Not really a big deal for just two events but is there a way to bind both of these listeners to the single action?

  • 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-10T21:05:09+00:00Added an answer on June 10, 2026 at 9:05 pm

    Since signatures of the two events’ delegates are different you can’t really combine event handlers, especially if you need the keyboard handler to have some additional logic, like checking which key was pressed.

    But, what you can do is put the assignment into its own method and then call it from both event handlers. That way you will not be violating DRY principle and if you would ever need to extend the action you’d only need to do it in one place as well as if you would want to use this same action for some other event you’ll be able to do it just by calling the method.

    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
        }
    
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SyncListToTextBox();
        }
    
        private void listBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                SyncListToTextBox();
            }
        }
    
        private void SyncListToTextBox() 
        {
            this.textBox1.Text = this.listBox1.SelectedItem.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam using windows forms. How can I query all child controls of a Form
I have a C# project using windows forms. I need to let the user
I have an f# script using Windows Forms and I want it to run
Could someone provide an example for drawing graphics without using Windows Forms? I have
I can already do: using System.Windows.Forms; Button b; or: System.Windows.Forms.Button b; but I would
Is there a way to manipulate the mouse position without using System.Windows.Forms.Cursor? Something like
I am using System.Windows; and System.Windows.Controls; so I can't use System.Windows.Forms; because there is
Is there a way to enable smooth scroll for ListBox class in Windows Forms
I am using a listbox in my C#2.0 windows forms application. The method to
I have a Windows Forms ListBox data-bound to a BindingList of business objects. The

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.