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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:06:05+00:00 2026-05-28T11:06:05+00:00

I am creating buttons dynamically for a user-based app. Now I have to tell

  • 0

I am creating buttons dynamically for a user-based app. Now I have to tell the new form what text to apply to the buttons via parameters to the form, the form then creates the buttons. Now I have an issue – the events for these buttons: What is the best mechanism to get the button click events through. I cannot access the form from the originating form, and I can only pass simple data types (and arrays).

My first thought is to use a code to reffer to the appropriate method in a static class – basically, pass an array of ints through with the names of the buttons and their onclick handler calls one method – handle(int code) -> where code is used in a giant switch statement to call the appropriate method.

But I doubt this is the best mechanism. I would prefer to create a listener of some sort that simply listens for button clicks, and should the click be unhandled, determine which button was clicked and manage it from there.

I have looked at the observer pattern and I am not entirely convinced this is the best one to follow. The problem is not that there is no solution, the problem is that I want the BEST solution.

This is in C# for monodroid – but the impact of this information should be minimal.

Thanks

  • 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-28T11:06:06+00:00Added an answer on May 28, 2026 at 11:06 am

    Currently I have two options:

    1. Use reflection – pass a method name to the button and that button can then invoke a method based on the string value passed. Then simply create a static class where all button methods are kept.

    2. Use a switch statement – since I can have delegates that take parameters (one of them being a SENDER object) I can easily send the sender object to a method containing a switch statement which performs an action based on that object.

    In my research I have determined that the former (reflection) is preferred, espcially since the number of buttons is rather large.

    REFS:
    http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/
    Large Switch statements: Bad OOP?
    Method Factory – case vs. reflection

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

Sidebar

Related Questions

I am coming across an issue with dynamically creating buttons. I have my text
I am dynamically creating buttons on iPhone app. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn
I have a problem with my project . I am creating link buttons dynamically
I have problem with handling dynamically created Buttons on Android. I'm creating N buttons
I'm creating a form where the user needs to add form elements dynamically. addEmptyItem()
I have a several form elements that I am dynamically creating with javascript when
I'm creating buttons dynamically ... for(int i=0; i<colSize;i++){ final Button btn = new Button(this);
Fellas, I am creating a text box dynamically, on a button click. User types
I am creating a registration form which contains two submit buttons. I need to
Creating a JApplet I have 2 Text Fields, a button and a Text Area.

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.