Solved

C# delegates button event click

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Java import and method not resolved 4 73
learn programming 8 66
WordPress: Debugging from my Windows 10 Desktop 6 61
Survey branching tutorial 11 38
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
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 …

740 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