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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:42:41+00:00 2026-05-19T03:42:41+00:00

I have a class that creates an instance of a form with several buttons.

  • 0

I have a class that creates an instance of a form with several buttons. I have a function in this class which is meant to wait for the user to click one of the buttons and return different values depending on which button was pressed. I’ve read some of the stuff on using anonymous delegates for this, but I’m not sure how I would determine which specific button was pressed. My original approach was to create a custom event that takes the button number as parameter, and tacking an event handler onto that from my class, but again I am not sure how I would have that function return anything once I get into a delegate.

Is there any straightforward way of doing this?

PM

  • 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-19T03:42:42+00:00Added an answer on May 19, 2026 at 3:42 am

    Assuming WinForms, there are a few approaches you could take. You could expose each button as a property on your form class and have the class the creates the form subscribe to the Click event for each button. For example,

    In the Form class:

    public class MyForm : Form 
    {
        // form initialization, etc, etc.
    
        public Button Button1 
        {
            get { return button1; }
        }
    }
    

    In the class that creates the form:

    public class MyClass
    {
        public Form CreateForm()
        {
            var form = new MyForm();
            form.Button1.Click += HandleButton1Clicked;
            return form;
        }
    
        private void HandleButton1Clicked(object sender, EventArgs e)
        {
            // do whatever you need to do when Button1 is clicked
        }
    }
    

    Alternatively, you could add a ButtonClicked event to the Form and determine which button was pressed that way. The form would subscribe to each of its button’s Click events and fire ButtonClicked with button as the sender.

    I’d probably go with the former since it would avoid having to write an if statement to determine which button was pressed.


    Edited to adapt to the workflow in comments:

    In that case, what you can do is have the Form take care of some of the details for you. For example, have the form record which button was pressed. If you show the form as a modal dialog, that will by design block the function that’s created the form until it is dismissed.

    public class MyForm : Form 
    {
        // form initialization, etc, etc.
        private Button button1;
        private Button button2;
    
        public MyForm()
        {
            InitializeComponent();
            button1.Click += HandleButtonClicked;
            button1.DialogResult = DialogResult.OK;
            button2.Click += HandleButtonClicked;
            button2.DialogResult = DialogResult.OK;
        }
    
        private void HandleButtonClicked(object sender, EventArgs e)
        {
            ButtonClicked = sender as Button;
        }
    
        public Button ButtonClicked
        {
            get; private set;
        }
    }
    

    Calling code:

    public class MyClass
    {
        public int GetValue()
        {
            var form = new MyForm();
            if(form.ShowDialog() == DialogResult.OK) // this will block until form is closed
            {
                // return some value based on form.ButtonClicked
                // adjust method's return type as necessary
            }
            else 
            {
                // do something if the user closed the form without 
                // clicking on one of the buttons
            }
        }
    }
    

    Note that the HandleButtonClicked event handler is the same for both buttons, since the form is just storing which button was clicked.

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

Sidebar

Related Questions

I have a class Page that creates an instance of DB , which is
I have a class that creates several IDisposable objects, all of these objects are
I have a PHP class that creates a PNG image on the fly and
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have some class initialized in Appdelegate, but when I get this class instance
I have a complex django object, which has properties of other class types. This
Basically I have a class A which creates an array upon construction. class A
I have a control that is created like so: public partial class MYControl :
I have a collection of classes that inherit from an abstract class I created.
I have class method that returns a list of employees that I can iterate

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.