M
mark e. barron
using System.Reflection
namespace WindowsFormsApplication2
{
public partial class GameForm : Form
{
...
private void GameForm_Load(object sender, EventArgs e)
{
...
TextBox tx = new TextBox();
tx.Name = "txt" + y.ToString() + x.ToString();
...
string sMethodName= tx.Name + "_MouseClick";
Type t = Type.GetType("WindowsFormsApplication2.GameForm");
MethodInfo mi = t.GetMethod(sMethodName);
//tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())
.......
public void txt00_MouseClick(object sender, MouseEventArgs e)
{
--------------------------------
I want to dynamically register an event handler with an event for
a newly created TextBox using C# and reflection. I have used VS2008
to create a TableLayoutPanel and now want to populate it with
TextBoxes.
mi.MethodHandle.GetFunctionPointer() returns System.IntPtr
Needed here is System.EventHandler, I believe.
I cannot cast
(System.EventHandler) System.IntPtr.
The Code compiles and runs as intended as long as I comment out
tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())
which is the whole purpose of this effort.
Please advise. I am stuck.
namespace WindowsFormsApplication2
{
public partial class GameForm : Form
{
...
private void GameForm_Load(object sender, EventArgs e)
{
...
TextBox tx = new TextBox();
tx.Name = "txt" + y.ToString() + x.ToString();
...
string sMethodName= tx.Name + "_MouseClick";
Type t = Type.GetType("WindowsFormsApplication2.GameForm");
MethodInfo mi = t.GetMethod(sMethodName);
//tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())
.......
public void txt00_MouseClick(object sender, MouseEventArgs e)
{
--------------------------------
I want to dynamically register an event handler with an event for
a newly created TextBox using C# and reflection. I have used VS2008
to create a TableLayoutPanel and now want to populate it with
TextBoxes.
mi.MethodHandle.GetFunctionPointer() returns System.IntPtr
Needed here is System.EventHandler, I believe.
I cannot cast
(System.EventHandler) System.IntPtr.
The Code compiles and runs as intended as long as I comment out
tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())
which is the whole purpose of this effort.
Please advise. I am stuck.