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

2
  • Why can't you access the form from the originating form? Commented Jan 6, 2012 at 8:53
  • Because of the way that creating Android forms works - it is not some object you simply create - it is an activity instance that must be started. There is no 'activity object' to access. Commented Jan 6, 2012 at 12:47

3 Answers 3

1

Not sure to fully understand what's actually your problem, but here's how you should deal with dynamic controls and event handlers:

Button myNewButton = new Button { Text = "MyCaption" };
myNewButton.Click += (sender, e) =>
    {
        ((Button)sender).Text = "New text here!";
        // Another logic could be put here
    };
Sign up to request clarification or add additional context in comments.

1 Comment

I can do this, but what I am doing is dynamic form generation. I am using an activity to inflate several versions of the same button (due to styling resctrictions) now the issue is that I have 3 or 4 activities which are loaded from the same code (re-usability). So the code is one class but made into many activities - each one with different buttons (all inflated dynamically). So now how do I create a management system for these buttons?
1

If it was WPF i'd use Commanding but i don't know if it's available for monofroid. You may look at http://www.codeproject.com/KB/dotnet/EventBroker.aspx

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.