随机分配的按钮 [英] Randomly assigned buttons
问题描述
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();
样式buttonStyle = Window.Resources [CurvedButton]作为样式;
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 =新厚度(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;
//像someWindowsButton((i)< - 这必须基于上面的代码)= clicked.Name();
MessageBox.Show(Button's name is:+ clicked.Name);
code
$ b 每次应用程序运行时按钮是随机分配的,我想要的是我可以通过后面的代码与它们交互的方式。
所以
foo.Name =button+ i;
表示什么时候或哪个按钮被点击,一个数字被分配给它,但我怎样才能与该按钮进行交互在代码中?
private void buttonClick(object sender,EventArgs e)
{
Button clicked = (按钮)发件人;
//像someWindowsButton((i)< - 这必须基于上面的代码)= clicked.Name();
MessageBox.Show(Button's name is:+ clicked.Name);
}
}
我希望这是有道理的。
为了让您了解设计层面发生了什么:
这些灰色方块中的每一个都是按钮,每个按钮随机分配给它指定的名称和号码,每次运行应用程序时,这些按钮将被重新分配一个不同的号码。
我需要一种方式,当一个数字被分配给它时,我可以点击该按钮的事件。
因此,随机地将一个按钮分配给号码1,取这个号码1,然后将它分配给
private void buttonClick(对象发件人,EventArgs e)
如果
{
button1_Click = clicked.Name(剥离按钮并确保(variableNumber剩余匹配按钮(1)_Click);
}
else
button2
etc etc
然后触发我的按钮事件:
private void button1_Click(object sender,RoutedEventArgs e)
{
MessageBox.Show(每个随机分配的按钮现在与真正的按钮事件相关联);
}
}
}
解决方案这是您寻找的更多内容吗?并创建是它的一个按钮。这样你甚至不需要我。
部分类Window1 {
void button3Click(object sender,RoutedEventArgs e){
//无论你想在这里
void button2Click(object sender,RoutedEventArgs e){
//任何你想在这里
}
void button1Click(object sender,RoutedEventArgs e){
//无论你想要什么
}
public Window1(){
this.InitializeComponent();
populateButtons();
}
public void populateButtons(){
int xPos;
int yPos;
Random ranNum = new Random();
foreach(var routedEventHandler在新的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 =新厚度(xPos,yPos,0,0);
foo.Click + = routedEventHandler;
LayoutRoot.Children.Add(foo);
}
}
}
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 added.
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);
}
}
}
Every time the application runs the buttons are randomly assigned, what I want is a way in which I can interact with them from the code behind.
So
foo.Name = "button" + i;
means when or whichever button is clicked a number is assigned to it but how can I interact with that button in the code?
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);
}
}
I hope that makes sense.
To give you a sense of whats happening at design level:
Each of these grey squares are buttons each button is randomly assigned its designated name and number and each time the application runs these buttons will be reassigned a different number.
I need a way inwhich when a number is assigned to it I can then fire an event for that button clicked.
So randomly a button has been assigned number 1, take this number 1 and then assign it to
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
etc etc
Which then fires my button event:
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("yay each randomly assigned button now correlates with a real button event");
}
}
}
解决方案 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);
}
}
}
这篇关于随机分配的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!