4 Replies - 8020 Views - Last Post: 22 September 2009 - 11:06 PM Rate Topic: -----

#1 nir143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 09

add button click event on runtime

Post icon  Posted 22 September 2009 - 10:44 AM

hi,i build minesweeper and i stuck :blink: .
i set newgame button that make 10X10 button array apear and i make 1 more array(int) to know how many bombs apear near each button.
but beacuse i add the button array in runtime i dont know how to make an click event.
(i want make 1 method to all of the buttons,ill be glad if someone can tell me how;) )

code if help:
form1-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace minesweeper
{
   

	public partial class Form1 : Form
	{
		bool playbefore = false;

		public Form1()
		{
			InitializeComponent();
		}

	   

		private void buttonnewgame_Click(object sender, EventArgs e)
		{
			if (playbefore)
			{

			}
			else
			{
				Button[,] arr = new Button[10, 10];
				int[,] bord = new int[10, 10];
				Game g1 = new Game();
				bord = g1.setbombs(bord);

				for (int i = 0; i < 10; i++)
				{
					for (int j = 0; j < 10; j++)
					{
						Button b = new Button();
						b.Text = "";
						b.Size = new Size(25, 25);
						b.Location = new Point((160 + (25 * i)), (100 + (25 * j)));
						b.Visible = true;
						Controls.Add(b);
					}
				}
				playbefore = true;
			}		  
		 

		}

	  
	}
}




game class-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace minesweeper
{
	class Game
	{
		public int[,] setbombs(int[,] ar)
		{
			int i = 0, j = 0;
			Random r = new Random();
			for (int c = 0; c < 10; c++)
			{
				i = r.Next(9);
				j = r.Next(9);
				ar[i, j]=-1;

			}

			return ar;
		}

		public int[,] setboard(int[,] arr)
		{

		}
		

	}
}




tyvm.

Is This A Good Question/Topic? 0
  • +

Replies To: add button click event on runtime

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: add button click event on runtime

Posted 22 September 2009 - 11:23 AM

Your handler method will have to exist at compile time, but you can just register the event at run time like usual:

myButton.Click += myButtonclickHandler;



Or whatever.

Am I missing what you're asking?
Was This Post Helpful? 0
  • +
  • -

#3 nir143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 09

Re: add button click event on runtime

Posted 22 September 2009 - 11:43 AM

what your code line means?
i dont know much about the click event method,in my program i opened 100 buttons on runtime, and i want to do something when te user press on one of them,i just begginer so i what i do most of time is doubleclick on the buttn then i write whatever i want,so i dnnohw to do that on control that apear on runtime.
tyvm;)
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: add button click event on runtime

Posted 22 September 2009 - 02:28 PM

Something like this?:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MultiButtonDemo
{
	public class Form1 : Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(13, 13);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.UseVisualStyleBackColor = true;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(95, 13);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 23);
			this.button2.TabIndex = 1;
			this.button2.Text = "button2";
			this.button2.UseVisualStyleBackColor = true;
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(176, 13);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(75, 23);
			this.button3.TabIndex = 2;
			this.button3.Text = "button3";
			this.button3.UseVisualStyleBackColor = true;
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(13, 42);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(75, 23);
			this.button4.TabIndex = 3;
			this.button4.Text = "button4";
			this.button4.UseVisualStyleBackColor = true;
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(95, 42);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(75, 23);
			this.button5.TabIndex = 4;
			this.button5.Text = "button5";
			this.button5.UseVisualStyleBackColor = true;
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(176, 42);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(75, 23);
			this.button6.TabIndex = 5;
			this.button6.Text = "button6";
			this.button6.UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 264);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button6;

		public Form1()
		{
			InitializeComponent();
			RegisterButtonEvents();
			
		}

		private void RegisterButtonEvents()
		{
			foreach (Control c in Controls)
			{
				if (c as Button != null)
				{
					c.Click += myClickHandler;
				}
			}
		}

		private void myClickHandler(object sender, EventArgs e)
		{
			MessageBox.Show(String.Format("You clicked button: {0}", sender));
		}
	}
}



Now, I know that's a lot of stuff, but it's mainly designer code. Let me break it down.

First, you add the buttons in runtime, so they won't really have a name. You can get to each one by going through the controls on the form and checking if it's a button.
The "as" construct allows a way to check the control without casting. Here it is in detail:

if (c as Button != null)
{
	c.Click += myClickHandler;
}



So, if the current control is a button, we can safely register the click event to myClickHandler method. That looks like this:

private void myClickHandler(object sender, EventArgs e)
{
	MessageBox.Show(String.Format("You clicked button: {0}", sender));
}



Now, all buttons in this form will show a messagebox when clicked that shows the type and the text of the button. Obviously, you can make this method do something more contextual to your actual application.

Anyway, moving on...

The foreach, as I said earlier is the mechanism to iterate over all the controls on the form. There's a controls collection, but there's not a Button collection, so we are verifying it through that if statement as shown above.


And that's how it's done.

Hopefully this has helped you. Take care.
Was This Post Helpful? 0
  • +
  • -

#5 nir143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 09

Re: add button click event on runtime

Posted 22 September 2009 - 11:06 PM

yea ty very much that works :) i found same code line yesterday ,ty
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1