C# 随机分配的按钮
因此,这里的代码动态地将按钮添加到我的wpf windows应用程序中。我想不出有什么方法可以调用在服务器上实际运行的按钮,因为它们是随机添加的C# 随机分配的按钮,c#,html,wpf,xaml,C#,Html,Wpf,Xaml,因此,这里的代码动态地将按钮添加到我的wpf windows应用程序中。我想不出有什么方法可以调用在服务器上实际运行的按钮,因为它们是随机添加的 namespace DynamicButtons { public partial class Window1 { public Window1() { this.InitializeComponent(); populateButtons();
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);
}
}
}
意味着在单击按钮时,或无论单击哪个按钮,都会为其分配一个编号,但如何在代码中与该按钮交互
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);
}
}
等等
然后触发我的按钮事件:
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("yay each randomly assigned button now correlates with a real button event");
}
}
}
这是你想要的吗?这将获取每个ButtonClick处理程序并为其创建一个按钮。这样你甚至不需要“我”
正如lobsterism所说,或者你可以有一个命令列表,你可以随机绑定到按钮上,而不是这些 下面是一个详细的示例:
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);
}
public类命令:ICommand
{
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
//做你想做的:)
}
#端区
}
var allCommands=new List();
添加(新命令);
添加(新的OtherCoolCommand);
添加(新的第三个命令);
专用无效分配按钮(按钮)
{
var idx=new Random().nextInt(allCommands.Count-1);
var命令=allComands[idx];
按钮。命令=命令;
allCommands.remove(命令);
}
如果您只想根据按钮名称选择操作,下面的代码可能会有所帮助。显然,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");
}
对不起,你能在我的代码中显示这一点而不是“dowhatyouwant”吗?也许你可以按一下按钮,而不是“dowhatyouwant”?@Kirsty不知道你的意思。您可以使用反射API执行类似(伪代码)
Reflect.GetMethod(this.Type,“button”+i2+“_click”).Execute()的操作
,但我认为这不是最好的选择。foo.Name=“button”+I代码>它看起来几乎是对的,但是这行有问题。你真的用了这个名字吗?如果您没有将此行用于其他任何用途,则可以将其删除。啊,不,在所有按钮都变为静态并丢失所有格式后,此行将不起作用。是否单击了“名称”
“不给您想要的名称?”?看起来应该可以。。。事件处理程序的sender参数的全部要点是它包含触发事件的对象,即单击的按钮……是的,但是如何将button1绑定到代码背后的某个对象?例如,如果我想让“button1”打开内容,当按下button1时,我如何在代码中调用它?我不想让所有按钮都做相同的事情,这只是改变我按下按钮时显示的按钮名称,如果我想用一个按钮显示消息框,而用另一个按钮打开另一个窗口,那么我如何区分这两个按钮呢?最简单的方法就是在名称上使用switch语句,在每种情况下调用不同的方法。我可能会用另一种方法来表达,而不是点击事件要求整洁,但它应该做你想做的事情,除非我遗漏了什么。根据要求,我已经给出了一个答案,告诉你我的意思。希望这比我的简短解释更有意义。:)
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);
}
}
}
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);
}
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");
}