Jump to content

Eventhandlers


andrepadez
 Share

Recommended Posts

Boas,

Tenho uma matriz de [8x8] pictureboxes, "SquareButtons".

é criada no form principa, onload, com um for quadrático.

Quero que todas elas funcionem como um botão, cujas acções sejam iguais, mas sejam controláveis uma a uma pelos índices da matriz.

Inseri dentro dos for de criação das mesmas a linha

squareButtons[rank, file].Click += new EventHandler(SquareButton_Click);

queria de alguma forma enviar como argumentos a posição xy na matriz.

Anyone???

Obrigado

Link to comment
Share on other sites

Bem, que eu saiba, não tens maneira de mandar um argumento para o handler.

A maneira mais simples que vejo é usares a propriedade Tag.

Na altura que crias a pictureBox nos fors metes (presumindo que a variável i e j são as coordenadas)

novaPictureBox.Tag = String.Format("{0};{1}", i, j);

Depois no handler, para obter as coordenadas, só tens que ir buscar o Tag.

Control c = sender as Control;
String[] coordenadas = c.Tag.Split(';');
int i = Int32.Parse(coordenadas[0]);
int j = Int32.Parse(coordenadas[1]);

"Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !"

Sign on a computer system consultant's desk

Link to comment
Share on other sites

Podes enviar a posição XY como argumentos para o EventHandler. Para isso basta criares uma classe a extender a classe EventArgs.


// This example demonstrates the EventHandler<T> delegate.

using System;
using System.Collections.Generic;

//---------------------------------------------------------
public class MyEventArgs : EventArgs
{
    private string msg;

    public MyEventArgs( string messageData ) {
        msg = messageData;
    }
    public string Message { 
        get { return msg; } 
        set { msg = value; }
    }
}
//---------------------------------------------------------
public class HasEvent
{
// Declare an event of delegate type EventHandler of 
// MyEventArgs.

    public event EventHandler<MyEventArgs> SampleEvent;

    public void DemoEvent(string val)
    {
    // Copy to a temporary variable to be thread-safe.
        EventHandler<MyEventArgs> temp = SampleEvent;
        if (temp != null)
            temp(this, new MyEventArgs(val));
    }
}
//---------------------------------------------------------
public class Sample
{
    public static void Main()
    {
        HasEvent he = new HasEvent();
        he.SampleEvent += 
                   new EventHandler<MyEventArgs>(SampleEventHandler);
        he.DemoEvent("Hey there, Bruce!");
        he.DemoEvent("How are you today?");
        he.DemoEvent("I'm pretty good.");
        he.DemoEvent("Thanks for asking!");
    }
    private static void SampleEventHandler(object src, MyEventArgs mea)
    {
        Console.WriteLine(mea.Message);
    }
}
//---------------------------------------------------------
/*
This example produces the following results:

Hey there, Bruce!
How are you today?
I'm pretty good.
Thanks for asking!

*/



http://msdn2.microsoft.com/en-us/library/db0etb8x.aspx para mais informações.

"There are two kinds of programmers. Those who write something to get the work done and those who want to write good code."João BrandãoWebsite e blog: http://jamab.blogspot.com/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.