Solved

C# delegates button event click

Posted on 2004-08-06
2
852 Views
Last Modified: 2008-03-17
K,
This one is something I've done before but i can't remember the syntax or find it out there.

I have an arraylist of Windows.System.Forms
I pass it into a new form and I am building dynamic buttons based on the forms in the arraylist.  So thus trying to launch new forms on the click event of these button depending on the item in the array.  I can't remember for the life of me how to override the system.eventargs to allow more arguments than sender and eventargs.

so here's the syntax

namespace BackOfficeUtility
{
      public delegate void SelectedKeyEventHandler(object sender, SelectedKeyEventArgs e);
      public class frmMain : System.Windows.Forms.Form
      {
            public event SelectedKeyEventHandler KeySelected;
public frmMain(ArrayList arForms)
{
     int counter = 0;
     foreach(Form frm in arForms)
     {
           CreateButtons(counter, frm);
           counter ++;
     }
}

public void CreateButtons(int counter, Form frm)
{
   Button myButton = new Button();
   myButton.Click += new SelectedKeyEventArgs(frm);
}


//Here's the class for the delegates

      public class SelectedKeyEventArgs : EventArgs
      {
            private int _keyvalue;
            //public Form frm;
            public SelectedKeyEventArgs(Form frm)
            {
                  _frm = new Form(frm);
            }


            public Form frm
            {
                  get
                  {
                        return _frm;
                  }
                  set
                  {
                        _frm = Value;
                  }
            }
      }

The Delegate and related class is screwed up.  

So the problem lies in the last line which is the click event I need to send the form in so that I can launch the new form.

Please help out this one is more frustrating than anything

BG






                  
0
Comment
Question by:painlessprod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 85 total points
ID: 11741177
the problem lies in ...

public void CreateButtons(int counter, Form frm)
{
   Button myButton = new Button();
   myButton.Click += new SelectedKeyEventArgs(frm);
}

you are using your own custom eventargs ... the button does not ...

instead what you probably want to do is ...

namespace BackOfficeUtility
{
     private HashTable ButtonMap = new HashTable();

     public class frmMain : System.Windows.Forms.Form
     {
          public event SelectedKeyEventHandler KeySelected;

public frmMain(ArrayList arForms)
{
     int counter = 0;
     foreach(Form frm in arForms)
     {
           CreateButtons(counter, frm);
           counter ++;
     }
}

private void foo(object sender, EventArgs e) {
     Form f = ButtonMap[sender] as Form;
     if(f!=null) {
          f = the form you want ...
     }
}

public void CreateButtons(int counter, Form frm)
{
   Button myButton = new Button();
   this.ButtonMap.Add(myButton, frm);
   myButton.Click += new EventHandler(this.foo);
}


0
 

Author Comment

by:painlessprod
ID: 11744284
Thank u so much that worked out perfectly!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question