Skip to content Skip to sidebar Skip to footer

Randomly Assigned Buttons

so this code here dynamically adds buttons to my wpf windows application. I cant think of the way in which I can call buttons which actually runat server because they are randomly

Solution 1:

Is this more what you're looking for? This takes each buttonNClick handler and creates a button for it. This way you don't even need the "i".

partial class Window1 {
    void button3Click(object sender, RoutedEventArgs e) {
        //Whatever you want here
    }
    void button2Click(object sender, RoutedEventArgs e) {
        //Whatever you want here
    }
    void button1Click(object sender, RoutedEventArgs e) {
        //Whatever you want here
    }

    public Window1() {
        this.InitializeComponent();
        populateButtons();
    }

    public void populateButtons() {
        int xPos;
        int yPos;

        Random ranNum = new Random();
        foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3Click }) {
            Button foo = new Button();

            int sizeValue = ranNum.Next(100);

            foo.Width = sizeValue;
            foo.Height = sizeValue;

            xPos = ranNum.Next(200);
            yPos = ranNum.Next(300);

            foo.HorizontalAlignment = HorizontalAlignment.Left;
            foo.VerticalAlignment = VerticalAlignment.Top;
            foo.Margin = new Thickness(xPos, yPos, 0, 0);

            foo.Click += routedEventHandler;

            LayoutRoot.Children.Add(foo);
        }
    }
}

Solution 2:

If you just want to choose an action based on the button name the below code may help. Obviously the doAction method calls will change as will your cases but hopefully the code gives you a good enough general idea.

    private void buttonClick(object sender, EventArgs e)
    {
        Button clicked = (Button) sender;
        ChooseAction(clicked.Name);
    }

    private void ChooseAction(string buttonName)
    {
        switch(buttonName)
        {
            case "button1": doAction1(); break;
            case "button2": doAction2(); break;
            case "button3": doAction3(); break;
            case "button4": doAction4(); break;
            case "button5": doAction5(); break;
            default: doDefaultAction(); break;
        }
    }

    private void doAction1()
    {
        Console.WriteLine("action 1");
    }

    private void doAction2()
    {
        Console.WriteLine("action 2");
    }
    private void doAction3()
    {
        Console.WriteLine("action 3");
    }
    private void doAction4()
    {
        Console.WriteLine("action 4");
    }
    private void doAction5()
    {
        Console.WriteLine("action 5");
    }

    private void doDefaultAction()
    {
        Console.WriteLine("button name not recognised, performing default action");
    }

Solution 3:

As lobsterism said, or you could have a list of commands, that you bind randomly to the buttons, instead of any of this. http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

here is a verbose sample:

public class CoolCommand : ICommand
{
    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
       //do what you want :)
    }

    #endregion
}

var allCommands = new List<ICommand>();
allCommands.Add(new CoolCommand);
allCommands.Add(new OtherCoolCommand);
allCommands.Add(new ThirdCoolCommand);

private void assignButton(button)
{
    var idx = new Random().nextInt(allCommands.Count-1);
    var command = allComands[idx];
    button.Command = command;
    allCommands.remove(command);
}

Post a Comment for "Randomly Assigned Buttons"