Solved

C# delegates button event click

Posted on 2004-08-06
2
795 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
2 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 85 total points
Comment Utility
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
Comment Utility
Thank u so much that worked out perfectly!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 164
topping3 challenge 14 48
word0 challenge 3 56
listing all functions in JavaScript 19 98
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now