Solved

C# delegates button event click

Posted on 2004-08-06
2
844 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

Technology Partners: 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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

734 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