Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2006, 04:10 AM
Authorized User
 
Join Date: Nov 2006
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Event On PictureBox In C#

Hi Expert
I have created a Panel in Visual C#. At Runtime on page_Load event i added pictureBox[n] Control to Panel. Now Picture added successfully to that Panel. But I have problem how to Generate event (Such as PictureBox_Click or PictureBox_MouseDown) on that Dynamic PictureBox. Please Give me Code and Instruction, how to write event code for PictureBox in my Visual C# Desktop Application.

Thanks with Regards
Kuamar Ashish
krashishroa@yahoo.co.in
__________________
Thanks
Kumar Ashish
krashishroa@yahoo.co.in
 
Old November 30th, 2006, 10:45 PM
Authorized User
 
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello akumarp2p,

Here's some code that illustrates how to accomplish your task. The controls I've used to illustrate this are as follows:

Code:
  Panel:       panel1
  PictureBox:  m_pictureBox
Hope this helps...

Code:
  public partial class Form1 : Form
  {
    //------------------------------
    // Member Variables
    //------------------------------

    private PictureBox m_pictureBox = null;

    //------------------------------
    // Constructors
    //------------------------------

    public Form1()
    {
      InitializeComponent();
    }

    //------------------------------
    // Overrides
    //------------------------------

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      // Instantiate the picture box.
      m_pictureBox = new PictureBox();

      // Initialize some of the picture box's properties.
      m_pictureBox.Dock = DockStyle.Fill;
      m_pictureBox.BackColor = Color.Bisque;

      // Add the picture box to the panel's Controls collection.  This way, the panel will correctly
      // dispose of the picture box later when the panel itself is destroyed.
      panel1.Controls.Add(m_pictureBox);

      // Add event handlers for the picture box's Click and MouseDown events.
      m_pictureBox.Click += new EventHandler(m_pictureBox_Click);
      m_pictureBox.MouseDown += new MouseEventHandler(m_pictureBox_MouseDown);
    }

    //------------------------------
    // Event Handlers
    //------------------------------

    void m_pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
      System.Diagnostics.Debug.WriteLine("PictureBox MouseDown event called.");
    }

    void m_pictureBox_Click(object sender, EventArgs e)
    {
      MessageBox.Show(this, "PictureBox Click event called.");
    }
  }


Cheers.

- Roger Nedel
 
Old February 2nd, 2007, 09:53 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi RNedel, and rest of the forum.

I incorporated some of your code into mine, but I don't understand how to get the name of the PictureBox onClick. I am creating a big tileset of PictureBoxes and it's necessary for me to know which PB the user clicked on. If you have any clue it's much appreciated! :)

Code:
Code:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            base.OnLoad(e);


            int[,] gridArray = new int[20, 20];

            for (int i = 0; i < numRows.Value; i++)
            {
                xpos = 300;
                ypos = ypos + 32;
                for (int j = 0; j < numCols.Value; j++)
                {
                    PictureBox curPb = new PictureBox();
                    curPb.Name = "picBox" + iPictureboxID.ToString();
                    curPb.Location = new Point(xpos, ypos);
                    curPb.Size = new Size(32, 32);
                    curPb.BackColor = Color.ForestGreen;
                    groupboxGame.Controls.Add(curPb);
                    curPb.Click += new EventHandler(curPb_Click);
                    xpos = xpos + 32;
                    iPictureboxID++;
                }
            }
        }

        void curPb_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "PictureBox Click event called.");
            // "this" returns nothing visible in the MessageBox. I'd like to get the name of the PictureBox here. How? :)
        }
    }
Thanks!
/Hoffe

 
Old February 2nd, 2007, 10:02 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

In the event handler of your Mousedown do something like this:

if(sender is PictureBox)
{
    PictureBox pb = (PictureBox)sender;
    MessageBox.Show(pb.Name + " was clicked!");

}

Of course if pictureboxes will be the only one's raising the event then you could forget the if statement and simply do:

    PictureBox pb = (PictureBox)sender;
    MessageBox.Show(pb.Name + " was clicked!");
================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old February 2nd, 2007, 10:07 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You, sir, made my day. Thanks a million!

 
Old February 2nd, 2007, 10:08 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Glad it worked out for you. ^^

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old February 2nd, 2007, 10:57 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<s>I'm adding to my question here. As you can see I'm using a two dimensional array to draw the grid. Is there any way my click can return the array indexes of the clicked grid rather than the name? I.E, is there any way to store values in a PictureBox and retrieve them later?

Since I want to check the surrounding tiles having the array "coordinates" would make it a lot simpler to figure out which tiles are nearby.

Am I making any sense? :)</s>

I realized this was a poor solution, please ignore. =)
 
Old January 14th, 2009, 07:55 AM
Registered User
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thankxxx

Thankxxxx, I got the specific answer from this thread!!!!
Thaks a lot....





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic picturebox array thomaz C# 3 September 17th, 2008 01:15 PM
Dynamic control onclick event mii2029 ASP.NET 3.5 Basics 7 August 27th, 2008 11:19 AM
How to use PictureBox OnPaint Event daming_ocean C# 2005 4 March 24th, 2008 07:13 PM
handling event for a dynamic object deepak_8282 General .NET 2 May 14th, 2007 12:42 PM
Dynamic Event rajanikrishna Classic ASP Professional 1 August 11th, 2006 05:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.