adapt control array to event

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

    #1

    adapt control array to event

    Hi,

    How to associate the dynamic controls array
    with its event?

    For example, creating a number of button, like 100, etc..

    Button arrButton[100] .....

    In VB6, when u copy and paste a button
    it auto create the next array and
    events such as ButtonClick(Ind ex as Integet)

    how to do that in C#?


  • Bill Priess

    #2
    Re: adapt control array to event

    In .NET, control arrays do not *necessarily* exist. In C#, if you want
    multiple controls to be handled by the same event handler, then you need to
    hookup the event handler manually...

    public void ButtonHandler(o bject sender, EventArgs e)
    {
    //do something here...
    }

    and in your load event:

    private void Page_Load(objec t sender, EventArgs e)
    {
    btn1.Click += new EventHandler(Bu ttonHandler);
    btn2.Click += new EventHandler(Bu ttonHandler);
    btn3.Click += new EventHandler(Bu ttonHandler);
    }

    or, if you have added the controls to an array, you can loop through the
    array and set the handlers too.

    HTH,

    Bill P.

    "gcl" <lnguyen59@comc ast.net> wrote in message
    news:0ebd01c34d 3e$93731610$a40 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > How to associate the dynamic controls array
    > with its event?
    >
    > For example, creating a number of button, like 100, etc..
    >
    > Button arrButton[100] .....
    >
    > In VB6, when u copy and paste a button
    > it auto create the next array and
    > events such as ButtonClick(Ind ex as Integet)
    >
    > how to do that in C#?
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: adapt control array to event

      gcl,
      To add to Bill Priess comments.

      The sender parameter of the event handler then identifies which button was
      clicked. You can cast it to Button to get the actual button object.

      Hope this helps
      Jay

      "gcl" <lnguyen59@comc ast.net> wrote in message
      news:0ebd01c34d 3e$93731610$a40 1280a@phx.gbl.. .[color=blue]
      > Hi,
      >
      > How to associate the dynamic controls array
      > with its event?
      >
      > For example, creating a number of button, like 100, etc..
      >
      > Button arrButton[100] .....
      >
      > In VB6, when u copy and paste a button
      > it auto create the next array and
      > events such as ButtonClick(Ind ex as Integet)
      >
      > how to do that in C#?
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: adapt control array to event

        gcl,
        You could cast sender to a Button variable, then check the Button.Name
        property for which button you have. Assuming you set Button.Name to uniquely
        identify the button. Actually you could use any property you want of Button.
        Or you could derive a class from Button and add customer actions (functions)
        & properties on it.

        Something like:

        private void Button_Click(ob ject sender, EventArgs e)
        {
        Button button = (Button)sender;
        if (button.Name = "Button1")
        {
        // do something with button1.
        }
        }

        I would normally create a new class that inherits from Button and having the
        click event operate polymorphically on this new class.

        Something like:

        private void ColorizingButto n_Click(object sender, EventArgs e)
        {
        ColorizingButto n button = (ColorizingButt on)sender;
        this.BackColor = button.Color;
        }

        class ColorizingButto n : Button
        {
        private Color m_color;

        ColorizingButto n(Color color)
        {
        m_color = color;
        }

        public Color Color
        {
        get
        {
        return m_color;
        }
        }
        }

        Then I would have an array of ColorizingButto ns, where each button had a
        different Color for its color property. When that button was clicked, the
        background color of the container would be changed to the color of that
        button.

        If you make the Color property above read/write, you can set the property in
        the Properties window when you design the form...

        Hope this helps
        Jay

        "gcl" <lnguyen59@comc ast.net> wrote in message
        news:033101c34d 5b$80046440$a10 1280a@phx.gbl.. .[color=blue]
        > I am not sure ..
        >
        > What I do is checking the sender object's property
        > to identify which button sent
        >
        >[color=green]
        > >-----Original Message-----
        > >gcl,
        > >To add to Bill Priess comments.
        > >
        > >The sender parameter of the event handler then[/color]
        > identifies which button was[color=green]
        > >clicked. You can cast it to Button to get the actual[/color]
        > button object.[color=green]
        > >
        > >Hope this helps
        > >Jay
        > >
        > >"gcl" <lnguyen59@comc ast.net> wrote in message
        > >news:0ebd01c34 d3e$93731610$a4 01280a@phx.gbl. ..[color=darkred]
        > >> Hi,
        > >>
        > >> How to associate the dynamic controls array
        > >> with its event?
        > >>
        > >> For example, creating a number of button, like 100,[/color][/color]
        > etc..[color=green][color=darkred]
        > >>
        > >> Button arrButton[100] .....
        > >>
        > >> In VB6, when u copy and paste a button
        > >> it auto create the next array and
        > >> events such as ButtonClick(Ind ex as Integet)
        > >>
        > >> how to do that in C#?
        > >>
        > >>[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        Working...