Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, September 1, 2012 11:14 AM
OK ... My buttons appear ...
namespace ButtonArray
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateTheButtons();
}
private void CreateTheButtons()
{
Button[,] BooTon = new Button[5,5];
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
BooTon[i,j]=new Button();
BooTon[i,j].Size=new Size(50,50);
BooTon[i,j].Location= new Point(i*60,j*60);
this.Controls.Add(BooTon[i, j]);
//this.Controls.Add
BooTon[i,j].Text= (i.ToString()) + " " + (j.ToString());
}
}
}
}
}
but I've not got the foggiest how to catch the click event ?
I guess
public void BooTon_Click( etc...)
{ How do we reference the array ?}
All replies (7)
Saturday, September 1, 2012 8:46 PM ✅Answered
Hello again DeFlat...
My coding is limited at best and it has been a coon's age since I played with WinForm… but if you didn't want to mess with strings and such for the array, what if you created your own button with properties holding the numbers? Something like the following and there is probably a better way to go about it...
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateTheButtons();
}
private void CreateTheButtons()
{
myButton[,] BooTon = new myButton[5, 5];
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
BooTon[i, j] = new myButton();
BooTon[i, j].Size = new Size(50, 50);
BooTon[i, j].Location = new Point(i * 60, j * 60);
BooTon[i, j].Text = (i.ToString()) + " " + (j.ToString());
BooTon[i, j].Num1 = i;
BooTon[i, j].Num2 = j;
BooTon[i, j].Click += new EventHandler(ButtonClick);
this.Controls.Add(BooTon[i, j]);
}
}
}
private void ButtonClick(object sender, EventArgs e)
{
//To Do - Click Event
myButton btn = sender as myButton;
MessageBox.Show(btn.Num1 + ", " + btn.Num2 + " - " + "Button Clicked");
}
}
public class myButton : Button
{
private int num1;
public int Num1
{
get { return num1; }
set { num1 = value; }
}
private int num2;
public int Num2
{
get { return num2; }
set { num2 = value; }
}
}
}
Hope that gives you some ideas...
~Christine
Saturday, September 1, 2012 2:48 PM
Something like the following may work for you...
private void CreateTheButtons()
{
Button[,] BooTon = new Button[5, 5];
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
BooTon[i, j] = new Button();
BooTon[i, j].Size = new Size(50, 50);
BooTon[i, j].Location = new Point(i * 60, j * 60);
BooTon[i, j].Text = (i.ToString()) + " " + (j.ToString());
BooTon[i, j].Click += new EventHandler(ButtonClick);
this.Controls.Add(BooTon[i, j]);
}
}
}
private void ButtonClick(object sender, EventArgs e)
{
//To Do - Click Event
Button btn = sender as Button;
MessageBox.Show(btn.Text + ", " + "Button Clicked");
}
Have a great weekend.
~Christine
Saturday, September 1, 2012 3:21 PM
Try a Routed Event. In the XAML of whatever container you add your buttons to reference a routed event. So if they are in a Grid it would look something like:
<Grid Name="grdButtons" ButtonBase.Click="Button_Click"
Width="250"
Height="250">
The OriginalSource property of the RoutedEventArgs will be the button that fired the event.
private void Button_Click(object sender, RoutedEventArgs e)
{
Button ClickedButton = (Button)e.OriginalSource;
ClickedButton.Content = "Click";
}
Below is an example of this using your function(tweaked a little)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Load">
<Grid>
<Grid Name="grdButtons" ButtonBase.Click="Button_Click"
Width="250"
Height="250">
<Grid.RowDefinitions>
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
</Grid.ColumnDefinitions>
</Grid>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CreateTheButtons()
{
Button[,] BooTon = new Button[5, 5];
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
BooTon[i, j] = new Button();
Grid.SetColumn(BooTon[i, j], i);
Grid.SetRow(BooTon[i, j], j);
grdButtons.Children.Add(BooTon[i, j]);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button ClickedButton = (Button)e.OriginalSource;
ClickedButton.Content = "Click";
}
private void Window_Load(object sender, RoutedEventArgs e)
{
CreateTheButtons();
}
}
}
Saturday, September 1, 2012 3:42 PM
Actually I don't believe a XAML solution would work in this case TechNoHick as he is working with a Windows Forms application.
@DeFlatCoda … There is a Windows Forms forum here... http://social.msdn.microsoft.com/Forums/en-US/category/windowsforms which appears to be active and you may find better assistance with your project there.
Wishing you the best.
~Christine
Saturday, September 1, 2012 6:55 PM
Well then I suppose D’flat shouldn’t be posting in a WPF forum..
Saturday, September 1, 2012 7:41 PM
....
this.Controls.Add(BooTon[i, j]);
//this.Controls.Add
BooTon[i,j].Text= (i.ToString()) + " " + (j.ToString());
BooTon[i, j].Click += new EventHandler(BooTon_Click);
}
}
}
private void BooTon_Click(object sender, EventArgs e)
{
textBox1.Text = sender.ToString().Substring(35,1);
//textBox1.Text = sender.ToString();
textBox2.Text = sender.ToString().Substring(37,1);
}
}
}
Christine ... in was the .click EventHandler that I needed ... Thank you.
I get the feeling that there is a much better way to get the array indicies than meddling with sender variable; but hey it works !
TechNoHick ... I am slightly baffled by your suggestion; but hey you are right I am asking this question in the wrong forum.
This is my first time asking a computing question in a forum (Ive asked lots in maths forums). Your answers are very generous & for this I am very grateful. Now if there is some etiquette for saying my question is answered & voting you guys up then let me know what I should do
All the Best DeFlatCoda :+)
<input id="atirp" type="hidden" />
Thursday, September 6, 2012 2:59 AM
Hi DeFlatCoda,
I will move this thread to Visual C# forum. Please feel free to follow up there if you need further assistance.
Thanks,
Kee Poppy [MSFT]
MSDN Community Support | Feedback to us