ASP.NET equilivant of VB6 control array

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

    ASP.NET equilivant of VB6 control array

    I have a project in ASP.NET using C#. I have a large number of Button Controls and a large number of ImageButton Controls. It would be convienent if I could refer to a specific control by it's index number, in the same way in which I used a Command Array in VB6. What is the method of assigning and then obtaining a specific unique identified for a control ? Sample code or reference articles would be appreciated.
    Thanks,
    Jim
  • Scott M.

    #2
    Re: ASP.NET equilivant of VB6 control array

    Use the "sender" argument of your event handler to identify which control
    fired the event.


    "James McGivney" <acura66@acura6 6.com> wrote in message
    news:C015B154-40E1-436C-AF52-AF93389527BB@mi crosoft.com...[color=blue]
    > I have a project in ASP.NET using C#. I have a large number of Button[/color]
    Controls and a large number of ImageButton Controls. It would be convienent
    if I could refer to a specific control by it's index number, in the same way
    in which I used a Command Array in VB6. What is the method of assigning and
    then obtaining a specific unique identified for a control ? Sample code or
    reference articles would be appreciated.[color=blue]
    > Thanks,
    > Jim[/color]


    Comment

    • Klaus H. Probst

      #3
      Re: ASP.NET equilivant of VB6 control array

      Cast the sender argument to the control type and then access its properties.

      There is a control array collection equivalent to the VB6 model, but AFAIK
      it only works on windows forms.

      --
      _______________ _____
      Klaus H. Probst, MVP


      "James McGivney" <acura66@acura6 6.com> wrote in message
      news:C015B154-40E1-436C-AF52-AF93389527BB@mi crosoft.com...[color=blue]
      > I have a project in ASP.NET using C#. I have a large number of Button[/color]
      Controls and a large number of ImageButton Controls. It would be convienent
      if I could refer to a specific control by it's index number, in the same way
      in which I used a Command Array in VB6. What is the method of assigning and
      then obtaining a specific unique identified for a control ? Sample code or
      reference articles would be appreciated.[color=blue]
      > Thanks,
      > Jim[/color]


      Comment

      • Scott M.

        #4
        Re: ASP.NET equilivant of VB6 control array

        Can you elaborate on this control array collection available in a windows
        forms project?


        "Klaus H. Probst" <usenet001@vbbo x.com> wrote in message
        news:eqt7R2G8DH A.1716@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Cast the sender argument to the control type and then access its[/color]
        properties.[color=blue]
        >
        > There is a control array collection equivalent to the VB6 model, but AFAIK
        > it only works on windows forms.
        >
        > --
        > _______________ _____
        > Klaus H. Probst, MVP
        > http://www.vbbox.com/
        >
        > "James McGivney" <acura66@acura6 6.com> wrote in message
        > news:C015B154-40E1-436C-AF52-AF93389527BB@mi crosoft.com...[color=green]
        > > I have a project in ASP.NET using C#. I have a large number of Button[/color]
        > Controls and a large number of ImageButton Controls. It would be[/color]
        convienent[color=blue]
        > if I could refer to a specific control by it's index number, in the same[/color]
        way[color=blue]
        > in which I used a Command Array in VB6. What is the method of assigning[/color]
        and[color=blue]
        > then obtaining a specific unique identified for a control ? Sample code[/color]
        or[color=blue]
        > reference articles would be appreciated.[color=green]
        > > Thanks,
        > > Jim[/color]
        >
        >[/color]


        Comment

        • James McGivney

          #5
          Re: ASP.NET equilivant of VB6 control array

          Thanks for your help. I'm almost there
          I can retreive the name of the pressed button by
          string WhichButton = ((System.Web.UI .WebControls.Bu tton)sender).ID .ToString()
          Now, how do I use this information, say to change the BackColor
          WhichButton.Bac kColor = Color.Green; generates an error
          What is the syntax to change the BackColor having the name of the control
          Thanks
          Jim

          Comment

          • Scott M.

            #6
            Re: ASP.NET equilivant of VB6 control array

            Don't declare WhichButton as a string, declare it as a button. Then set an
            object reference to Sender cast as the button type.

            Don't have the C# for you, but here it is in VB.NET

            Dim WhichButton as System.Web.UI.W ebControls.Butt on
            WhichButton = CType(sender, System.Web.UI.W ebControls.Butt on)
            WhichButton.Bac kColor = Color.Green

            Or even more simply (no variable reference, just working directly with
            sender):

            CType(sender, System.Web.UI.W ebControls.Butt on).BackColor = Color.Green



            "James McGivney" <acura66@acura6 6.com> wrote in message
            news:1700AC59-2A1F-4C7D-893D-181C16104AD7@mi crosoft.com...[color=blue]
            > Thanks for your help. I'm almost there.
            > I can retreive the name of the pressed button by:
            > string WhichButton =[/color]
            ((System.Web.UI .WebControls.Bu tton)sender).ID .ToString();[color=blue]
            > Now, how do I use this information, say to change the BackColor ?
            > WhichButton.Bac kColor = Color.Green; generates an error.
            > What is the syntax to change the BackColor having the name of the control.
            > Thanks,
            > Jim[/color]


            Comment

            • Klaus H. Probst

              #7
              Re: ASP.NET equilivant of VB6 control array

              I've never used them, but for VB.NET GUI projects in (I believe)
              compatibility mode, there are a bunch of framework classes that wrap the
              control collection functionality. I don't know how close they are to the VB6
              ones.


              --
              _______________ _____
              Klaus H. Probst, MVP
              Great prices on a large selection of domains. Find the pefect domain for your new startup.


              "Scott M." <s-mar@BADSPAMsnet .net> wrote in message
              news:egXxXNL8DH A.2924@tk2msftn gp13.phx.gbl...[color=blue]
              > Can you elaborate on this control array collection available in a windows
              > forms project?
              >
              >
              > "Klaus H. Probst" <usenet001@vbbo x.com> wrote in message
              > news:eqt7R2G8DH A.1716@TK2MSFTN GP10.phx.gbl...[color=green]
              > > Cast the sender argument to the control type and then access its[/color]
              > properties.[color=green]
              > >
              > > There is a control array collection equivalent to the VB6 model, but[/color][/color]
              AFAIK[color=blue][color=green]
              > > it only works on windows forms.
              > >
              > > --
              > > _______________ _____
              > > Klaus H. Probst, MVP
              > > http://www.vbbox.com/
              > >
              > > "James McGivney" <acura66@acura6 6.com> wrote in message
              > > news:C015B154-40E1-436C-AF52-AF93389527BB@mi crosoft.com...[color=darkred]
              > > > I have a project in ASP.NET using C#. I have a large number of Button[/color]
              > > Controls and a large number of ImageButton Controls. It would be[/color]
              > convienent[color=green]
              > > if I could refer to a specific control by it's index number, in the same[/color]
              > way[color=green]
              > > in which I used a Command Array in VB6. What is the method of assigning[/color]
              > and[color=green]
              > > then obtaining a specific unique identified for a control ? Sample code[/color]
              > or[color=green]
              > > reference articles would be appreciated.[color=darkred]
              > > > Thanks,
              > > > Jim[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Scott M.

                #8
                Re: ASP.NET equilivant of VB6 control array

                I ask because in .NET you can't create a control array in the VB 6.0 sense
                (several controls with the same name identified by an index). In .NET, you
                can create a collection of controls, but this is not the same thing.

                You can get the same benefits of a control array in .NET by assigning one
                event handler to handle all the various controls with the event handlers
                "Handles" clause and then you can use a Select statement that looks at the
                Sender argument to see which one was responsible for firing the event.


                "Klaus H. Probst" <usenet001@vbbo x.com> wrote in message
                news:uhvJkWS8DH A.3380@tk2msftn gp13.phx.gbl...[color=blue]
                > I've never used them, but for VB.NET GUI projects in (I believe)
                > compatibility mode, there are a bunch of framework classes that wrap the
                > control collection functionality. I don't know how close they are to the[/color]
                VB6[color=blue]
                > ones.
                >
                >
                > --
                > _______________ _____
                > Klaus H. Probst, MVP
                > http://www.vbbox.com/
                >
                > "Scott M." <s-mar@BADSPAMsnet .net> wrote in message
                > news:egXxXNL8DH A.2924@tk2msftn gp13.phx.gbl...[color=green]
                > > Can you elaborate on this control array collection available in a[/color][/color]
                windows[color=blue][color=green]
                > > forms project?
                > >
                > >
                > > "Klaus H. Probst" <usenet001@vbbo x.com> wrote in message
                > > news:eqt7R2G8DH A.1716@TK2MSFTN GP10.phx.gbl...[color=darkred]
                > > > Cast the sender argument to the control type and then access its[/color]
                > > properties.[color=darkred]
                > > >
                > > > There is a control array collection equivalent to the VB6 model, but[/color][/color]
                > AFAIK[color=green][color=darkred]
                > > > it only works on windows forms.
                > > >
                > > > --
                > > > _______________ _____
                > > > Klaus H. Probst, MVP
                > > > http://www.vbbox.com/
                > > >
                > > > "James McGivney" <acura66@acura6 6.com> wrote in message
                > > > news:C015B154-40E1-436C-AF52-AF93389527BB@mi crosoft.com...
                > > > > I have a project in ASP.NET using C#. I have a large number of[/color][/color][/color]
                Button[color=blue][color=green][color=darkred]
                > > > Controls and a large number of ImageButton Controls. It would be[/color]
                > > convienent[color=darkred]
                > > > if I could refer to a specific control by it's index number, in the[/color][/color][/color]
                same[color=blue][color=green]
                > > way[color=darkred]
                > > > in which I used a Command Array in VB6. What is the method of[/color][/color][/color]
                assigning[color=blue][color=green]
                > > and[color=darkred]
                > > > then obtaining a specific unique identified for a control ? Sample[/color][/color][/color]
                code[color=blue][color=green]
                > > or[color=darkred]
                > > > reference articles would be appreciated.
                > > > > Thanks,
                > > > > Jim
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • James McGivney

                  #9
                  Re: ASP.NET equilivant of VB6 control array

                  Thanks for your help. I'll try it in C
                  Jim

                  Comment

                  • James McGivney

                    #10
                    Re: ASP.NET equilivant of VB6 control array

                    I'd like to ask one more related question. From this thread I have learned how to detect which button was clicked and how to use this information to change the properties of the button. But, suppose I find out the 5th button was pressed, now how to I add an image to my 5th ImageButton. Assume that the clicked button is Button5 and the imageButton I want to change is ImageButton5. How do I get the info for imageButton5 ? VB6's index property would make this simple. How do I accomplish this in C#.net
                    Thanks for your help
                    Jim

                    Comment

                    • Scott M.

                      #11
                      Re: ASP.NET equilivant of VB6 control array

                      Public Sub CommonEventHand er (sender as object, e as eventargs) Handles
                      button1.click, button2.click, button3.click
                      'Let's assume that web form button controls are what this event handler
                      handles
                      dim testButton as system.web.ui.b utton

                      'It's safe to cast sender as a system.web.ui.b utton because we know that
                      this is the only type of control this sub handles
                      testButton = CType(sender, system.web.ui.b utton)

                      'Check some property of the button that will identify it uniquely
                      'In this case, we'll use its name since all controls must have a unique
                      name

                      Select Case testButton.name
                      Case "btnItem1"
                      'Now that you know "btnItem1" was clicked, do whatever you need
                      to

                      Case "btnItem2"
                      'Now that you know "btnItem2" was clicked, do whatever you need
                      to

                      Case "btnItem3"
                      'Now that you know "btnItem3" was clicked, do whatever you need
                      to

                      End Select

                      End Sub


                      "James McGivney" <acura66@acura6 6.com> wrote in message
                      news:BF3002EA-BC6F-4BB4-8F29-1F8C648145A6@mi crosoft.com...[color=blue]
                      > I'd like to ask one more related question. From this thread I have[/color]
                      learned how to detect which button was clicked and how to use this
                      information to change the properties of the button. But, suppose I find out
                      the 5th button was pressed, now how to I add an image to my 5th ImageButton.
                      Assume that the clicked button is Button5 and the imageButton I want to
                      change is ImageButton5. How do I get the info for imageButton5 ? VB6's
                      index property would make this simple. How do I accomplish this in C#.net ?[color=blue]
                      > Thanks for your help.
                      > Jim[/color]


                      Comment

                      Working...