Solved

C# delegates button event click

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
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…

839 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