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".

partialclassWindow1 {
    voidbutton3Click(object sender, RoutedEventArgs e) {
        //Whatever you want here
    }
    voidbutton2Click(object sender, RoutedEventArgs e) {
        //Whatever you want here
    }
    voidbutton1Click(object sender, RoutedEventArgs e) {
        //Whatever you want here
    }

    publicWindow1() {
        this.InitializeComponent();
        populateButtons();
    }

    publicvoidpopulateButtons() {
        int xPos;
        int yPos;

        Random ranNum = new Random();
        foreach (var routedEventHandler innew 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.

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

    privatevoidChooseAction(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;
        }
    }

    privatevoiddoAction1()
    {
        Console.WriteLine("action 1");
    }

    privatevoiddoAction2()
    {
        Console.WriteLine("action 2");
    }
    privatevoiddoAction3()
    {
        Console.WriteLine("action 3");
    }
    privatevoiddoAction4()
    {
        Console.WriteLine("action 4");
    }
    privatevoiddoAction5()
    {
        Console.WriteLine("action 5");
    }

    privatevoiddoDefaultAction()
    {
        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:

publicclassCoolCommand : ICommand
{
    #region ICommand MemberspublicboolCanExecute(object parameter)
    {
        returntrue;
    }

    publicevent EventHandler CanExecuteChanged;

    publicvoidExecute(object parameter)
    {
       //do what you want :)
    }

    #endregion
}

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

privatevoidassignButton(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"