随机分配的按钮

时间:2012-03-23 17:23:55

标签: c# html wpf xaml

所以这里的代码动态地将按钮添加到我的wpf windows应用程序中。我无法想象我可以调用实际运行服务器的按钮的方式,因为它们是随机添加的。

namespace DynamicButtons
{
    public partial class Window1
    {
        public Window1()
        {
            this.InitializeComponent();

            populateButtons();
        }

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

            Random ranNum = new Random();

            for (int i = 0; i < 4; i++)
            {
                Button foo = new Button();
                Style buttonStyle = Window.Resources["CurvedButton"] as Style;

                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.Style = buttonStyle;
                foo.Name = "button" + i;

                foo.Click += new RoutedEventHandler(buttonClick);

                LayoutRoot.Children.Add(foo);
           }
        }

        private void buttonClick(object sender, EventArgs e)
        {
            Button clicked = (Button) sender;
            // something like certainWindowsButton((i)<-this has to be based on above code) = clicked.Name(); 
            MessageBox.Show("Button's name is: " + clicked.Name);
        }
    }
}

每次应用程序运行时,按钮都是随机分配的,我想要的是一种可以从后面的代码中与它们进行交互的方式。

所以

foo.Name = "button" + i; 

表示点击按钮的时间或点击时,会为其分配一个数字但是如何与代码中的该按钮进行交互?

    private void buttonClick(object sender, EventArgs e)
    {
        Button clicked = (Button) sender;
        // something like certainWindowsButton((i)<-this has to be based on above code) = clicked.Name(); 
        MessageBox.Show("Button's name is: " + clicked.Name);
    }
}

我希望这是有道理的。

让您了解在设计层面发生的事情:

enter image description here

每个灰色方块都是按钮,每个按钮随机分配其指定的名称和编号,每次应用程序运行时,这些按钮将被重新分配不同的数字。

我需要一种方法,当为其分配一个数字时,我可以点击该按钮的事件。

所以随机为一个按钮分配了编号1,取这个编号1,然后将其分配给

private void buttonClick(object sender, EventArgs e) 

if
{
    button1_Click = clicked.Name(strip away "button" and make sure the (variableNumber leftover matches button(1)_Click);
}
else 
    button2

等等

然后触发我的按钮事件:

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("yay each randomly assigned button now correlates with a real button event");
        }
    }
}

3 个答案:

答案 0 :(得分:1)

这更像是你在寻找什么?这将获取每个buttonNClick处理程序并为其创建一个按钮。这样你甚至不需要“我”。

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);
        }
    }
}

答案 1 :(得分:1)

如果您只想根据按钮名称选择操作,则以下代码可能有所帮助。显然doAction方法调用会像你的情况一样改变,但希望代码能给你一个足够好的总体思路。

    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");
    }

答案 2 :(得分:0)

正如龙虾主义所说,或者你可以有一个命令列表,你可以随机绑定到按钮,而不是任何一个。 http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

这是一个详细的样本:

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);
}