create an event handler for control array in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frankcvc

    create an event handler for control array in C#

    I created a control array at runtime using the following code. I also wanted
    to code the controlarray click event. My code can respond to click event
    but cannot identify the specific control clicked on, which is important to my
    application.

    Appreciate your help.

    Frank
    ---------------------------
    private void initBoard()
    {
    this.Width=250;
    this.Height=250 ;
    buttonArray = new Button[4,4];
    for (int i = 1; i<=3; i++)
    for (int j=1; j<=3;j++)
    {
    Button aButton = new Button();
    aButton.Click += new System.EventHan dler(ClickHandl er);
    aButton.Width=5 0;
    aButton.Height= 50;
    aButton.Top = i * 50;
    aButton.Left = j*50;
    buttonArray[i,j]=aButton;
    // Add the button to the controls collection of the form
    this.Controls.A dd(aButton);
    }
    }

    private void ClickHandler(ob ject sender, System.EventArg s e)
    {
    MessageBox.Show ("Button" +((Button) sender).Tag.ToS tring() + " was
    clicked");
    }

    --
    Frank
  • Joakim Karlsson

    #2
    Re: create an event handler for control array in C#

    The control that is clicked will be passed as the sender argument of
    your ClickHandler method.

    /Joakim

    frankcvc wrote:[color=blue]
    > I created a control array at runtime using the following code. I also wanted
    > to code the controlarray click event. My code can respond to click event
    > but cannot identify the specific control clicked on, which is important to my
    > application.
    >
    > Appreciate your help.
    >
    > Frank
    > ---------------------------
    > private void initBoard()
    > {
    > this.Width=250;
    > this.Height=250 ;
    > buttonArray = new Button[4,4];
    > for (int i = 1; i<=3; i++)
    > for (int j=1; j<=3;j++)
    > {
    > Button aButton = new Button();
    > aButton.Click += new System.EventHan dler(ClickHandl er);
    > aButton.Width=5 0;
    > aButton.Height= 50;
    > aButton.Top = i * 50;
    > aButton.Left = j*50;
    > buttonArray[i,j]=aButton;
    > // Add the button to the controls collection of the form
    > this.Controls.A dd(aButton);
    > }
    > }
    >
    > private void ClickHandler(ob ject sender, System.EventArg s e)
    > {
    > MessageBox.Show ("Button" +((Button) sender).Tag.ToS tring() + " was
    > clicked");
    > }
    >[/color]

    Comment

    • Joakim Karlsson

      #3
      Re: create an event handler for control array in C#

      Oops! I jumped the gun there a little :)

      You've already realized that I see.

      You never set the Tag property of the buttons in the creation code, so
      there is nothing for you to read there when you reach the event handler.

      /Joakim

      Joakim Karlsson wrote:[color=blue]
      > The control that is clicked will be passed as the sender argument of
      > your ClickHandler method.
      >
      > /Joakim
      >
      > frankcvc wrote:
      >[color=green]
      >> I created a control array at runtime using the following code. I also
      >> wanted to code the controlarray click event. My code can respond to
      >> click event but cannot identify the specific control clicked on, which
      >> is important to my application.
      >> Appreciate your help.
      >>
      >> Frank
      >> ---------------------------
      >> private void initBoard()
      >> {
      >> this.Width=250;
      >> this.Height=250 ;
      >> buttonArray = new Button[4,4];
      >> for (int i = 1; i<=3; i++)
      >> for (int j=1; j<=3;j++)
      >> {
      >> Button aButton = new Button();
      >> aButton.Click += new
      >> System.EventHan dler(ClickHandl er);
      >> aButton.Width=5 0;
      >> aButton.Height= 50;
      >> aButton.Top = i * 50;
      >> aButton.Left = j*50;
      >> buttonArray[i,j]=aButton;
      >> // Add the button to the controls collection of
      >> the form this.Controls.A dd(aButton);
      >> }
      >> }
      >>
      >> private void ClickHandler(ob ject sender, System.EventArg s e)
      >> {
      >> MessageBox.Show ("Button" +((Button) sender).Tag.ToS tring()
      >> + " was clicked");
      >> }
      >>[/color][/color]

      Comment

      • Benoit Vreuninckx

        #4
        Re: create an event handler for control array in C#

        frankcvc wrote:[color=blue]
        > I created a control array at runtime using the following code. I also wanted
        > to code the controlarray click event. My code can respond to click event
        > but cannot identify the specific control clicked on, which is important to my
        > application.
        >
        > Appreciate your help.
        >
        > Frank
        > ---------------------------
        > private void initBoard()
        > {
        > this.Width=250;
        > this.Height=250 ;
        > buttonArray = new Button[4,4];
        > for (int i = 1; i<=3; i++)
        > for (int j=1; j<=3;j++)
        > {
        > Button aButton = new Button();
        > aButton.Click += new System.EventHan dler(ClickHandl er);
        > aButton.Width=5 0;
        > aButton.Height= 50;
        > aButton.Top = i * 50;
        > aButton.Left = j*50;
        > buttonArray[i,j]=aButton;
        > // Add the button to the controls collection of the form
        > this.Controls.A dd(aButton);
        > }
        > }
        >
        > private void ClickHandler(ob ject sender, System.EventArg s e)
        > {
        > MessageBox.Show ("Button" +((Button) sender).Tag.ToS tring() + " was
        > clicked");
        > }
        >[/color]

        Apart from the button identification problem, are you correctly
        iterating over the array of buttons ? The first row and column aren't
        populated with buttons, but that might be your intention.

        Regards,
        Benoit

        Comment

        • Michael Groeger

          #5
          Re: create an event handler for control array in C#

          i would appreciate something like this:

          class MyButton : Button
          {
          int _i,_j;

          public int _I
          {
          get { return _i; }
          }

          public int _J
          {
          get { return _j; }
          }

          public MyButton(int i, int j)
          {
          _i = i;
          _j = j;
          }
          }

          In your code replace:
          Button aButton = new Button();

          with:
          MyButton aButton = new MyButton(i,j);

          And write a appropriate handler:
          private void ClickHandler(ob ject sender, System.EventArg s e)
          {
          MyButton button = (MyButton)sende r;
          MessageBox.Show (string.Format( "Button ({0}.{1}) was clicked", button._I,
          button._J));
          }

          Michael

          "frankcvc" <frankcvc@discu ssions.microsof t.com> schrieb im Newsbeitrag
          news:7011A430-A05E-40B0-945E-02DCBC444273@mi crosoft.com...[color=blue]
          > I created a control array at runtime using the following code. I also[/color]
          wanted[color=blue]
          > to code the controlarray click event. My code can respond to click event
          > but cannot identify the specific control clicked on, which is important to[/color]
          my[color=blue]
          > application.
          >
          > Appreciate your help.
          >
          > Frank
          > ---------------------------
          > private void initBoard()
          > {
          > this.Width=250;
          > this.Height=250 ;
          > buttonArray = new Button[4,4];
          > for (int i = 1; i<=3; i++)
          > for (int j=1; j<=3;j++)
          > {
          > Button aButton = new Button();
          > aButton.Click += new System.EventHan dler(ClickHandl er);
          > aButton.Width=5 0;
          > aButton.Height= 50;
          > aButton.Top = i * 50;
          > aButton.Left = j*50;
          > buttonArray[i,j]=aButton;
          > // Add the button to the controls collection of the form
          > this.Controls.A dd(aButton);
          > }
          > }
          >
          > private void ClickHandler(ob ject sender, System.EventArg s e)
          > {
          > MessageBox.Show ("Button" +((Button) sender).Tag.ToS tring() + " was
          > clicked");
          > }
          >
          > --
          > Frank[/color]


          Comment

          • Cor Ligthert

            #6
            Re: create an event handler for control array in C#

            Frank,

            In my opinion do you do nothing with your button array.
            You have it already added to the collection "this.controls" , so there it is
            and as long as your form object is there it will be there until you remove
            it.

            When you give dynamicly your button a name from 0 to 15 or as others said
            use the tage for that, than
            you can get that in the event using the code you almost used however than.

            messagebox.show ((Button)sender ).Name.ToString () + ect)

            Just my idea

            Cor


            Comment

            • frankcvc

              #7
              Re: create an event handler for control array in C#

              Many thanks for all of the responses to my question. Yes, all of these
              suggestions are very relevant . The problem is solved by adding a tag to the
              button.

              However, in my click event, I can only identify the individual button but
              not as a group member or its subscript. Of course, with proper taging I can
              extract the subscript out from the tag. I am wondering if there is a more
              straightforward way to do it like in the older VB.

              Thanks again.

              Frank

              "Cor Ligthert" wrote:
              [color=blue]
              > Frank,
              >
              > In my opinion do you do nothing with your button array.
              > You have it already added to the collection "this.controls" , so there it is
              > and as long as your form object is there it will be there until you remove
              > it.
              >
              > When you give dynamicly your button a name from 0 to 15 or as others said
              > use the tage for that, than
              > you can get that in the event using the code you almost used however than.
              >
              > messagebox.show ((Button)sender ).Name.ToString () + ect)
              >
              > Just my idea
              >
              > Cor
              >
              >
              >[/color]

              Comment

              • Cor Ligthert

                #8
                Re: create an event handler for control array in C#

                Frank,
                [color=blue]
                > Many thanks for all of the responses to my question. Yes, all of these
                > suggestions are very relevant . The problem is solved by adding a tag to
                > the
                > button.
                >
                > However, in my click event, I can only identify the individual button but
                > not as a group member or its subscript. Of course, with proper taging I
                > can
                > extract the subscript out from the tag. I am wondering if there is a more
                > straightforward way to do it like in the older VB.
                >[/color]
                Real because it makes me curious, why you want to do that? This is very
                straigthforward in my idea.
                (I keep saying you do not need the array).

                When you want it using the designer, than you can create an array by using
                the buttons which are created.

                \\\
                private void FormLoad(object sender, System.EventArg s e)
                {Button[] but = {button1,button 2};
                foreach (Button bt in but)
                {bt.Click += new System.EventHan dler(this.butto n_Click);}
                }
                ///

                The rest is almost the same.

                I don't know it this is the idea

                Cor

                Cor


                Comment

                • Frank Kwong

                  #9
                  Re: create an event handler for control array in C#

                  In my case, I have a data collection control that when data comes it, it
                  fires up The DataIn event handler. I have 100 of these controls fired up
                  and the incoming data for each is saved into a buffer array. The I use a
                  timer to go in and scan the buffer and process the message accordingly.
                  If I can have an event array then I can process the data in the indexed
                  handler instead of saving it and process later. I cannot process the
                  data in the single DataIn handler as it would miss data coming in.

                  Thanks,

                  FK



                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  Working...