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

  • Home
  • SEARCH
  • 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 7060813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:23:52+00:00 2026-05-28T04:23:52+00:00

In a Windows Forms app, there are some things I want various controls of

  • 0

In a Windows Forms app, there are some things I want various controls of the same type on different forms to always do upon certain events. For example, I want a single click on a DataGridViewCell that is a TextBox-type and is not read only to automatically enter edit mode. The simple event handler code, in this case on a form with a DataGridView called dgvParms, is:

private void dgvParms_CellClick(object sender, DataGridViewCellEventArgs e) {
    DataGridViewCell c = dgvParms[e.ColumnIndex, e.RowIndex];
    if (!c.ReadOnly && c is DataGridViewTextBoxCell) {
        dgvParms.CurrentCell = c;
        dgvParms.BeginEdit(true);                
    }
}

I could easily move this method to a static class, say CommonEvents, and then on my individual forms’ Load handlers assign the static method definition to the respective DataGridViews’ CellClick event

this.dgvParms.CellClick += CommonEvents.dgvEditOnCellClick;

Is this acceptable or good practice? Or is it preffered to keep event handler logic with each consuming form’s code? I could of course do something in between (but redundant) by defining local event handlers which then call the common method, such as

this.dgvParms.CellClick += (a, b) => CommonEvents.dgvEditOnCellClick(a, b);
  • 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-28T04:23:53+00:00Added an answer on May 28, 2026 at 4:23 am

    This looks absolutely fine to me, although of course you will have to change you code to use the sender argument to locate the DataGridView that raised the event:

    private void dgvParms_CellClick(object sender, DataGridViewCellEventArgs e) {
        DataGridView dgvParams = sender as DataGridView;
        DataGridViewCell c = dgvParms[e.ColumnIndex, e.RowIndex];
        if (!c.ReadOnly && c is DataGridViewTextBoxCell) {
            dgvParms.CurrentCell = c;
            dgvParms.BeginEdit(true);                
        }
    }
    

    The general approach of changing or adding behaviour to a control by handling and reacting to its events is called an attached behaviour. It is most typically used in WPF, but there is nothing wrong with using it in WinForms too.

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

Sidebar

Related Questions

I inherited a Windows Forms app written in VB.Net. Certain parts of the app
I have a windows forms app with a textbox control that I want to
I have a windows forms app where I have split different functionality into several
I am working on a Windows Forms app for quite some time now, and
I have built a setup project for deploying my windows forms app. I want
In a simple app I'm working on, I've got three related assemblies: MyCompany.Geography.Windows.Forms which
I have a Windows Forms app, that has a single ElementHost containing a WPF
In a Windows Forms app I set the ContextMenuStrip property on a TabControl. How
I'm coding a Windows Forms App that has a StatusStrip that displays status informations
I have a .NET 2.0 windows forms app, which makes heavy use of 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.