A question regarding the Name property

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

    #1

    A question regarding the Name property

    I am reading a book that says that the "name" property can be altered only
    at design time and cannot be modified at runtime.

    Please explain this given the code below...

    If you click Button3... fred will appear as the Name of Button1, however,
    the handler for Button1 works still works as well...


    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    MsgBox("HELP")

    End Sub

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    Me.Button1.Name = "fred"
    Dim i As Integer = 0
    Dim str As String

    While i < Me.Controls.Cou nt
    Dim c As Control = Me.Controls(i)
    str = c.Name
    MsgBox(str)
    i = i + 1

    End While
    End Sub



  • Cor Ligthert [MVP]

    #2
    Re: A question regarding the Name property

    Rob,

    I am afraid that I don't understand your question complete, but ever control
    has a collection of controls. (So that one has that again). That collection
    has nothing to do with indiviuel names of collection items.

    Cor


    "Rob" <robc1@yahoo.co mschreef in bericht
    news:HYCdnUV5Cr SwCtrbnZ2dnUVZ_ tadnZ2d@comcast .com...
    >I am reading a book that says that the "name" property can be altered only
    >at design time and cannot be modified at runtime.
    >
    Please explain this given the code below...
    >
    If you click Button3... fred will appear as the Name of Button1, however,
    the handler for Button1 works still works as well...
    >
    >
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    MsgBox("HELP")
    >
    End Sub
    >
    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    Me.Button1.Name = "fred"
    Dim i As Integer = 0
    Dim str As String
    >
    While i < Me.Controls.Cou nt
    Dim c As Control = Me.Controls(i)
    str = c.Name
    MsgBox(str)
    i = i + 1
    >
    End While
    End Sub
    >
    >
    >

    Comment

    • rowe_newsgroups

      #3
      Re: A question regarding the Name property

      On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
      I am reading a book that says that the "name" property can be altered only
      at design time and cannot be modified at runtime.
      >
      Please explain this given the code below...
      >
      If you click Button3... fred will appear as the Name of Button1, however,
      the handler for Button1 works still works as well...
      >
      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      MsgBox("HELP")
      >
      End Sub
      >
      Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button3.Click
      Me.Button1.Name = "fred"
      Dim i As Integer = 0
      Dim str As String
      >
      While i < Me.Controls.Cou nt
      Dim c As Control = Me.Controls(i)
      str = c.Name
      MsgBox(str)
      i = i + 1
      >
      End While
      End Sub
      I am reading a book that says that the "name" property can be altered only
      at design time and cannot be modified at runtime.
      What book is this? I would like to have a conversation with the author
      as you can change control names at runtime with no problems.
      the handler for Button1 works still works as well...
      This is because it's handling the Click event for the control Button1,
      not the control named "Button1." If you look at the designer generated
      code, you will see that when you add a button to the designer it
      generates something like this:

      Private Sub InitializeCompo nent()
      Me.Button1 = New System.Windows. Forms.Button

      '
      'Button1
      '
      Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
      Me.Button1.Name = "Button1"
      Me.Button1.Size = New System.Drawing. Size(75, 23)
      Me.Button1.TabI ndex = 0
      Me.Button1.Text = "Button1"
      Me.Button1.UseV isualStyleBackC olor = True
      End Sub

      Friend WithEvents Button1 As System.Windows. Forms.Button

      This declares a button, and then in the InitialiazeComp onent method it
      sets it's properties - including Name. And since this method should
      run whenever a form is instantiated, this proves that runtime
      assigning of the Name property is perfectly fine.

      Thanks,

      Seth Rowe

      Comment

      • Rob

        #4
        Re: A question regarding the Name property

        Hi Cor,

        Well, the book states that you cannot alter the "name" property of a control
        at runtime...

        Isn't the clicking of a Button considered "run-time" ?

        If so, look at the code for Button3... It changes the name of Button1 to
        "fred", then reports that the "name" of Button1 is "fred".

        However, the code for Button1 still executes (Handles Button1.click). .. if
        the name were really changed to fred, then it should not execute.

        Just a little confused...

        Thanks,
        Rob



        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
        news:udAMTcelHH A.4904@TK2MSFTN GP05.phx.gbl...
        Rob,
        >
        I am afraid that I don't understand your question complete, but ever
        control has a collection of controls. (So that one has that again). That
        collection has nothing to do with indiviuel names of collection items.
        >
        Cor
        >
        >
        "Rob" <robc1@yahoo.co mschreef in bericht
        news:HYCdnUV5Cr SwCtrbnZ2dnUVZ_ tadnZ2d@comcast .com...
        >>I am reading a book that says that the "name" property can be altered only
        >>at design time and cannot be modified at runtime.
        >>
        >Please explain this given the code below...
        >>
        >If you click Button3... fred will appear as the Name of Button1, however,
        >the handler for Button1 works still works as well...
        >>
        >>
        > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        >System.EventAr gs) Handles Button1.Click
        > MsgBox("HELP")
        >>
        > End Sub
        >>
        > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
        >System.EventAr gs) Handles Button3.Click
        > Me.Button1.Name = "fred"
        > Dim i As Integer = 0
        > Dim str As String
        >>
        > While i < Me.Controls.Cou nt
        > Dim c As Control = Me.Controls(i)
        > str = c.Name
        > MsgBox(str)
        > i = i + 1
        >>
        > End While
        > End Sub
        >>
        >>
        >>
        >
        >

        Comment

        • Rob

          #5
          Re: A question regarding the Name property

          Seth,

          I want to make sure I have stated correctly and in the proper context...

          From the book...

          Regarding common properties of controls (to quote)...

          Name - "Represents the name used to refer to the control in code. This
          property can be altered only at design time and cannot be modified at run
          time."

          So I guess the control has some "unique attribute" other than the "name"
          property that uniquely identifies it. Maybe that is the attribute that
          cannot be changed ?

          Anyway, I created 2 buttons on a form (used VS2003 for this), then modified
          the code as follows... I expected it to crash due to the duplicate Button1
          instantiation, instead, it appeared to apply a "last line of code wins"
          logic set... as the sole button that appeared had its text property set to
          "Button2".


          Me.Button1 = New System.Windows. Forms.Button
          ' Formerly Button 2 below
          Me.Button1 = New System.Windows. Forms.Button
          Me.SuspendLayou t()
          '
          'Button1
          '
          Me.Button1.Loca tion = New System.Drawing. Point(88, 8)
          Me.Button1.Name = "Button1"
          Me.Button1.TabI ndex = 0
          Me.Button1.Text = "Button1"
          '
          'Button2
          ' Changed the following....
          Me.Button1.Loca tion = New System.Drawing. Point(80, 56)
          Me.Button1.Name = "Button1"
          Me.Button1.TabI ndex = 1
          Me.Button1.Text = "Button2"

          Thanks,
          Rob



          "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
          news:1179142282 .908849.89850@w 5g2000hsg.googl egroups.com...
          On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
          >I am reading a book that says that the "name" property can be altered
          >only
          >at design time and cannot be modified at runtime.
          >>
          >Please explain this given the code below...
          >>
          >If you click Button3... fred will appear as the Name of Button1, however,
          >the handler for Button1 works still works as well...
          >>
          > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          >System.EventAr gs) Handles Button1.Click
          > MsgBox("HELP")
          >>
          > End Sub
          >>
          > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
          >System.EventAr gs) Handles Button3.Click
          > Me.Button1.Name = "fred"
          > Dim i As Integer = 0
          > Dim str As String
          >>
          > While i < Me.Controls.Cou nt
          > Dim c As Control = Me.Controls(i)
          > str = c.Name
          > MsgBox(str)
          > i = i + 1
          >>
          > End While
          > End Sub
          >
          >
          >I am reading a book that says that the "name" property can be altered
          >only
          >at design time and cannot be modified at runtime.
          >
          What book is this? I would like to have a conversation with the author
          as you can change control names at runtime with no problems.
          >
          >the handler for Button1 works still works as well...
          >
          This is because it's handling the Click event for the control Button1,
          not the control named "Button1." If you look at the designer generated
          code, you will see that when you add a button to the designer it
          generates something like this:
          >
          Private Sub InitializeCompo nent()
          Me.Button1 = New System.Windows. Forms.Button
          >
          '
          'Button1
          '
          Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
          Me.Button1.Name = "Button1"
          Me.Button1.Size = New System.Drawing. Size(75, 23)
          Me.Button1.TabI ndex = 0
          Me.Button1.Text = "Button1"
          Me.Button1.UseV isualStyleBackC olor = True
          End Sub
          >
          Friend WithEvents Button1 As System.Windows. Forms.Button
          >
          This declares a button, and then in the InitialiazeComp onent method it
          sets it's properties - including Name. And since this method should
          run whenever a form is instantiated, this proves that runtime
          assigning of the Name property is perfectly fine.
          >
          Thanks,
          >
          Seth Rowe
          >

          Comment

          • rowe_newsgroups

            #6
            Re: A question regarding the Name property

            On May 14, 8:22 am, "Rob" <r...@yahoo.com wrote:
            Seth,
            >
            I want to make sure I have stated correctly and in the proper context...
            >
            From the book...
            >
            Regarding common properties of controls (to quote)...
            >
            Name - "Represents the name used to refer to the control in code. This
            property can be altered only at design time and cannot be modified at run
            time."
            >
            So I guess the control has some "unique attribute" other than the "name"
            property that uniquely identifies it. Maybe that is the attribute that
            cannot be changed ?
            >
            Anyway, I created 2 buttons on a form (used VS2003 for this), then modified
            the code as follows... I expected it to crash due to the duplicate Button1
            instantiation, instead, it appeared to apply a "last line of code wins"
            logic set... as the sole button that appeared had its text property set to
            "Button2".
            >
            Me.Button1 = New System.Windows. Forms.Button
            ' Formerly Button 2 below
            Me.Button1 = New System.Windows. Forms.Button
            Me.SuspendLayou t()
            '
            'Button1
            '
            Me.Button1.Loca tion = New System.Drawing. Point(88, 8)
            Me.Button1.Name = "Button1"
            Me.Button1.TabI ndex = 0
            Me.Button1.Text = "Button1"
            '
            'Button2
            ' Changed the following....
            Me.Button1.Loca tion = New System.Drawing. Point(80, 56)
            Me.Button1.Name = "Button1"
            Me.Button1.TabI ndex = 1
            Me.Button1.Text = "Button2"
            >
            Thanks,
            Rob
            >
            "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
            >
            news:1179142282 .908849.89850@w 5g2000hsg.googl egroups.com...
            >
            On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
            I am reading a book that says that the "name" property can be altered
            only
            at design time and cannot be modified at runtime.
            >
            Please explain this given the code below...
            >
            If you click Button3... fred will appear as the Name of Button1, however,
            the handler for Button1 works still works as well...
            >
            Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            System.EventArg s) Handles Button1.Click
            MsgBox("HELP")
            >
            End Sub
            >
            Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
            System.EventArg s) Handles Button3.Click
            Me.Button1.Name = "fred"
            Dim i As Integer = 0
            Dim str As String
            >
            While i < Me.Controls.Cou nt
            Dim c As Control = Me.Controls(i)
            str = c.Name
            MsgBox(str)
            i = i + 1
            >
            End While
            End Sub
            >
            I am reading a book that says that the "name" property can be altered
            only
            at design time and cannot be modified at runtime.
            >
            What book is this? I would like to have a conversation with the author
            as you can change control names at runtime with no problems.
            >
            the handler for Button1 works still works as well...
            >
            This is because it's handling the Click event for the control Button1,
            not the control named "Button1." If you look at the designer generated
            code, you will see that when you add a button to the designer it
            generates something like this:
            >
            Private Sub InitializeCompo nent()
            Me.Button1 = New System.Windows. Forms.Button
            >
            '
            'Button1
            '
            Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing. Size(75, 23)
            Me.Button1.TabI ndex = 0
            Me.Button1.Text = "Button1"
            Me.Button1.UseV isualStyleBackC olor = True
            End Sub
            >
            Friend WithEvents Button1 As System.Windows. Forms.Button
            >
            This declares a button, and then in the InitialiazeComp onent method it
            sets it's properties - including Name. And since this method should
            run whenever a form is instantiated, this proves that runtime
            assigning of the Name property is perfectly fine.
            >
            Thanks,
            >
            Seth Rowe
            Again, what book are you reading? I'm starting to think you are
            reading a VB6 book - not one for Visual Basic .Net.

            Thanks,

            Seth Rowe

            Comment

            • rowe_newsgroups

              #7
              Re: A question regarding the Name property

              On May 14, 7:41 am, "Rob" <r...@yahoo.com wrote:
              Hi Cor,
              >
              Well, the book states that you cannot alter the "name" property of a control
              at runtime...
              >
              Isn't the clicking of a Button considered "run-time" ?
              >
              If so, look at the code for Button3... It changes the name of Button1 to
              "fred", then reports that the "name" of Button1 is "fred".
              >
              However, the code for Button1 still executes (Handles Button1.click). .. if
              the name were really changed to fred, then it should not execute.
              >
              Just a little confused...
              >
              Thanks,
              Rob
              >
              "Cor Ligthert [MVP]" <notmyfirstn... @planet.nlwrote in messagenews:udA MTcelHHA.4904@T K2MSFTNGP05.phx .gbl...
              >
              Rob,
              >
              I am afraid that I don't understand your question complete, but ever
              control has a collection of controls. (So that one has that again). That
              collection has nothing to do with indiviuel names of collection items.
              >
              Cor
              >
              "Rob" <r...@yahoo.com schreef in bericht
              news:HYCdnUV5Cr SwCtrbnZ2dnUVZ_ tadnZ2d@comcast .com...
              >I am reading a book that says that the "name" property can be altered only
              >at design time and cannot be modified at runtime.
              >
              Please explain this given the code below...
              >
              If you click Button3... fred will appear as the Name of Button1, however,
              the handler for Button1 works still works as well...
              >
              Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
              System.EventArg s) Handles Button1.Click
              MsgBox("HELP")
              >
              End Sub
              >
              Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
              System.EventArg s) Handles Button3.Click
              Me.Button1.Name = "fred"
              Dim i As Integer = 0
              Dim str As String
              >
              While i < Me.Controls.Cou nt
              Dim c As Control = Me.Controls(i)
              str = c.Name
              MsgBox(str)
              i = i + 1
              >
              End While
              End Sub
              However, the code for Button1 still executes (Handles Button1.click). .. if
              the name were really changed to fred, then it should not execute.
              Read my earlier post. The handles clause refers to the object, not the
              object's name.

              Thanks,

              Seth Rowe

              Comment

              • Rob

                #8
                Re: A question regarding the Name property

                Microsoft .Net Framework 2.0 Windows-based Client Development
                Self paced Training kit for Exam 70-526
                Page 53

                Thanks,
                Rob



                "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                news:1179147333 .360623.280730@ w5g2000hsg.goog legroups.com...
                On May 14, 8:22 am, "Rob" <r...@yahoo.com wrote:
                >Seth,
                >>
                >I want to make sure I have stated correctly and in the proper context...
                >>
                >From the book...
                >>
                >Regarding common properties of controls (to quote)...
                >>
                >Name - "Represents the name used to refer to the control in code. This
                >property can be altered only at design time and cannot be modified at run
                >time."
                >>
                >So I guess the control has some "unique attribute" other than the "name"
                >property that uniquely identifies it. Maybe that is the attribute that
                >cannot be changed ?
                >>
                >Anyway, I created 2 buttons on a form (used VS2003 for this), then
                >modified
                >the code as follows... I expected it to crash due to the duplicate
                >Button1
                >instantiatio n, instead, it appeared to apply a "last line of code wins"
                >logic set... as the sole button that appeared had its text property set
                >to
                >"Button2".
                >>
                >Me.Button1 = New System.Windows. Forms.Button
                >' Formerly Button 2 below
                > Me.Button1 = New System.Windows. Forms.Button
                > Me.SuspendLayou t()
                > '
                > 'Button1
                > '
                > Me.Button1.Loca tion = New System.Drawing. Point(88, 8)
                > Me.Button1.Name = "Button1"
                > Me.Button1.TabI ndex = 0
                > Me.Button1.Text = "Button1"
                > '
                > 'Button2
                > ' Changed the following....
                > Me.Button1.Loca tion = New System.Drawing. Point(80, 56)
                > Me.Button1.Name = "Button1"
                > Me.Button1.TabI ndex = 1
                > Me.Button1.Text = "Button2"
                >>
                >Thanks,
                >Rob
                >>
                >"rowe_newsgrou ps" <rowe_em...@yah oo.comwrote in message
                >>
                >news:117914228 2.908849.89850@ w5g2000hsg.goog legroups.com...
                >>
                On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
                >I am reading a book that says that the "name" property can be altered
                >only
                >at design time and cannot be modified at runtime.
                >>
                >Please explain this given the code below...
                >>
                >If you click Button3... fred will appear as the Name of Button1,
                >however,
                >the handler for Button1 works still works as well...
                >>
                > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
                >As
                >System.EventAr gs) Handles Button1.Click
                > MsgBox("HELP")
                >>
                > End Sub
                >>
                > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e
                >As
                >System.EventAr gs) Handles Button3.Click
                > Me.Button1.Name = "fred"
                > Dim i As Integer = 0
                > Dim str As String
                >>
                > While i < Me.Controls.Cou nt
                > Dim c As Control = Me.Controls(i)
                > str = c.Name
                > MsgBox(str)
                > i = i + 1
                >>
                > End While
                > End Sub
                >>
                >I am reading a book that says that the "name" property can be altered
                >only
                >at design time and cannot be modified at runtime.
                >>
                What book is this? I would like to have a conversation with the author
                as you can change control names at runtime with no problems.
                >>
                >the handler for Button1 works still works as well...
                >>
                This is because it's handling the Click event for the control Button1,
                not the control named "Button1." If you look at the designer generated
                code, you will see that when you add a button to the designer it
                generates something like this:
                >>
                Private Sub InitializeCompo nent()
                Me.Button1 = New System.Windows. Forms.Button
                >>
                '
                'Button1
                '
                Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
                Me.Button1.Name = "Button1"
                Me.Button1.Size = New System.Drawing. Size(75, 23)
                Me.Button1.TabI ndex = 0
                Me.Button1.Text = "Button1"
                Me.Button1.UseV isualStyleBackC olor = True
                End Sub
                >>
                Friend WithEvents Button1 As System.Windows. Forms.Button
                >>
                This declares a button, and then in the InitialiazeComp onent method it
                sets it's properties - including Name. And since this method should
                run whenever a form is instantiated, this proves that runtime
                assigning of the Name property is perfectly fine.
                >>
                Thanks,
                >>
                Seth Rowe
                >
                Again, what book are you reading? I'm starting to think you are
                reading a VB6 book - not one for Visual Basic .Net.
                >
                Thanks,
                >
                Seth Rowe
                >

                Comment

                • Rob

                  #9
                  Re: A question regarding the Name property

                  As far as a unique control identifier...

                  I understand what you are saying....

                  ' The following does invoke an error...
                  Friend WithEvents Button1 As System.Windows. Forms.Button
                  Friend WithEvents Button1 As System.Windows. Forms.Button




                  "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                  news:1179147333 .360623.280730@ w5g2000hsg.goog legroups.com...
                  On May 14, 8:22 am, "Rob" <r...@yahoo.com wrote:
                  >Seth,
                  >>
                  >I want to make sure I have stated correctly and in the proper context...
                  >>
                  >From the book...
                  >>
                  >Regarding common properties of controls (to quote)...
                  >>
                  >Name - "Represents the name used to refer to the control in code. This
                  >property can be altered only at design time and cannot be modified at run
                  >time."
                  >>
                  >So I guess the control has some "unique attribute" other than the "name"
                  >property that uniquely identifies it. Maybe that is the attribute that
                  >cannot be changed ?
                  >>
                  >Anyway, I created 2 buttons on a form (used VS2003 for this), then
                  >modified
                  >the code as follows... I expected it to crash due to the duplicate
                  >Button1
                  >instantiatio n, instead, it appeared to apply a "last line of code wins"
                  >logic set... as the sole button that appeared had its text property set
                  >to
                  >"Button2".
                  >>
                  >Me.Button1 = New System.Windows. Forms.Button
                  >' Formerly Button 2 below
                  > Me.Button1 = New System.Windows. Forms.Button
                  > Me.SuspendLayou t()
                  > '
                  > 'Button1
                  > '
                  > Me.Button1.Loca tion = New System.Drawing. Point(88, 8)
                  > Me.Button1.Name = "Button1"
                  > Me.Button1.TabI ndex = 0
                  > Me.Button1.Text = "Button1"
                  > '
                  > 'Button2
                  > ' Changed the following....
                  > Me.Button1.Loca tion = New System.Drawing. Point(80, 56)
                  > Me.Button1.Name = "Button1"
                  > Me.Button1.TabI ndex = 1
                  > Me.Button1.Text = "Button2"
                  >>
                  >Thanks,
                  >Rob
                  >>
                  >"rowe_newsgrou ps" <rowe_em...@yah oo.comwrote in message
                  >>
                  >news:117914228 2.908849.89850@ w5g2000hsg.goog legroups.com...
                  >>
                  On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
                  >I am reading a book that says that the "name" property can be altered
                  >only
                  >at design time and cannot be modified at runtime.
                  >>
                  >Please explain this given the code below...
                  >>
                  >If you click Button3... fred will appear as the Name of Button1,
                  >however,
                  >the handler for Button1 works still works as well...
                  >>
                  > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
                  >As
                  >System.EventAr gs) Handles Button1.Click
                  > MsgBox("HELP")
                  >>
                  > End Sub
                  >>
                  > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e
                  >As
                  >System.EventAr gs) Handles Button3.Click
                  > Me.Button1.Name = "fred"
                  > Dim i As Integer = 0
                  > Dim str As String
                  >>
                  > While i < Me.Controls.Cou nt
                  > Dim c As Control = Me.Controls(i)
                  > str = c.Name
                  > MsgBox(str)
                  > i = i + 1
                  >>
                  > End While
                  > End Sub
                  >>
                  >I am reading a book that says that the "name" property can be altered
                  >only
                  >at design time and cannot be modified at runtime.
                  >>
                  What book is this? I would like to have a conversation with the author
                  as you can change control names at runtime with no problems.
                  >>
                  >the handler for Button1 works still works as well...
                  >>
                  This is because it's handling the Click event for the control Button1,
                  not the control named "Button1." If you look at the designer generated
                  code, you will see that when you add a button to the designer it
                  generates something like this:
                  >>
                  Private Sub InitializeCompo nent()
                  Me.Button1 = New System.Windows. Forms.Button
                  >>
                  '
                  'Button1
                  '
                  Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
                  Me.Button1.Name = "Button1"
                  Me.Button1.Size = New System.Drawing. Size(75, 23)
                  Me.Button1.TabI ndex = 0
                  Me.Button1.Text = "Button1"
                  Me.Button1.UseV isualStyleBackC olor = True
                  End Sub
                  >>
                  Friend WithEvents Button1 As System.Windows. Forms.Button
                  >>
                  This declares a button, and then in the InitialiazeComp onent method it
                  sets it's properties - including Name. And since this method should
                  run whenever a form is instantiated, this proves that runtime
                  assigning of the Name property is perfectly fine.
                  >>
                  Thanks,
                  >>
                  Seth Rowe
                  >
                  Again, what book are you reading? I'm starting to think you are
                  reading a VB6 book - not one for Visual Basic .Net.
                  >
                  Thanks,
                  >
                  Seth Rowe
                  >

                  Comment

                  • rowe_newsgroups

                    #10
                    Re: A question regarding the Name property

                    On May 14, 9:36 am, "Rob" <r...@yahoo.com wrote:
                    As far as a unique control identifier...
                    >
                    I understand what you are saying....
                    >
                    ' The following does invoke an error...
                    Friend WithEvents Button1 As System.Windows. Forms.Button
                    Friend WithEvents Button1 As System.Windows. Forms.Button
                    >
                    "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                    >
                    news:1179147333 .360623.280730@ w5g2000hsg.goog legroups.com...
                    >
                    On May 14, 8:22 am, "Rob" <r...@yahoo.com wrote:
                    Seth,
                    >
                    I want to make sure I have stated correctly and in the proper context...
                    >
                    From the book...
                    >
                    Regarding common properties of controls (to quote)...
                    >
                    Name - "Represents the name used to refer to the control in code. This
                    property can be altered only at design time and cannot be modified at run
                    time."
                    >
                    So I guess the control has some "unique attribute" other than the "name"
                    property that uniquely identifies it. Maybe that is the attribute that
                    cannot be changed ?
                    >
                    Anyway, I created 2 buttons on a form (used VS2003 for this), then
                    modified
                    the code as follows... I expected it to crash due to the duplicate
                    Button1
                    instantiation, instead, it appeared to apply a "last line of code wins"
                    logic set... as the sole button that appeared had its text property set
                    to
                    "Button2".
                    >
                    Me.Button1 = New System.Windows. Forms.Button
                    ' Formerly Button 2 below
                    Me.Button1 = New System.Windows. Forms.Button
                    Me.SuspendLayou t()
                    '
                    'Button1
                    '
                    Me.Button1.Loca tion = New System.Drawing. Point(88, 8)
                    Me.Button1.Name = "Button1"
                    Me.Button1.TabI ndex = 0
                    Me.Button1.Text = "Button1"
                    '
                    'Button2
                    ' Changed the following....
                    Me.Button1.Loca tion = New System.Drawing. Point(80, 56)
                    Me.Button1.Name = "Button1"
                    Me.Button1.TabI ndex = 1
                    Me.Button1.Text = "Button2"
                    >
                    Thanks,
                    Rob
                    >
                    "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                    >
                    >news:117914228 2.908849.89850@ w5g2000hsg.goog legroups.com...
                    >
                    On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
                    I am reading a book that says that the "name" property can be altered
                    only
                    at design time and cannot be modified at runtime.
                    >
                    Please explain this given the code below...
                    >
                    If you click Button3... fred will appear as the Name of Button1,
                    however,
                    the handler for Button1 works still works as well...
                    >
                    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
                    As
                    System.EventArg s) Handles Button1.Click
                    MsgBox("HELP")
                    >
                    End Sub
                    >
                    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e
                    As
                    System.EventArg s) Handles Button3.Click
                    Me.Button1.Name = "fred"
                    Dim i As Integer = 0
                    Dim str As String
                    >
                    While i < Me.Controls.Cou nt
                    Dim c As Control = Me.Controls(i)
                    str = c.Name
                    MsgBox(str)
                    i = i + 1
                    >
                    End While
                    End Sub
                    >
                    I am reading a book that says that the "name" property can be altered
                    only
                    at design time and cannot be modified at runtime.
                    >
                    What book is this? I would like to have a conversation with the author
                    as you can change control names at runtime with no problems.
                    >
                    the handler for Button1 works still works as well...
                    >
                    This is because it's handling the Click event for the control Button1,
                    not the control named "Button1." If you look at the designer generated
                    code, you will see that when you add a button to the designer it
                    generates something like this:
                    >
                    Private Sub InitializeCompo nent()
                    Me.Button1 = New System.Windows. Forms.Button
                    >
                    '
                    'Button1
                    '
                    Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
                    Me.Button1.Name = "Button1"
                    Me.Button1.Size = New System.Drawing. Size(75, 23)
                    Me.Button1.TabI ndex = 0
                    Me.Button1.Text = "Button1"
                    Me.Button1.UseV isualStyleBackC olor = True
                    End Sub
                    >
                    Friend WithEvents Button1 As System.Windows. Forms.Button
                    >
                    This declares a button, and then in the InitialiazeComp onent method it
                    sets it's properties - including Name. And since this method should
                    run whenever a form is instantiated, this proves that runtime
                    assigning of the Name property is perfectly fine.
                    >
                    Thanks,
                    >
                    Seth Rowe
                    >
                    Again, what book are you reading? I'm starting to think you are
                    reading a VB6 book - not one for Visual Basic .Net.
                    >
                    Thanks,
                    >
                    Seth Rowe
                    As far as a unique control identifier...
                    >
                    I understand what you are saying....
                    Good! The only time I use the Name property is when I am using the
                    FindControl method on dynamically created controls.

                    Thanks,

                    Seth Rowe

                    Comment

                    • rowe_newsgroups

                      #11
                      Re: A question regarding the Name property

                      On May 14, 8:22 am, "Rob" <r...@yahoo.com wrote:
                      Seth,
                      >
                      I want to make sure I have stated correctly and in the proper context...
                      >
                      From the book...
                      >
                      Regarding common properties of controls (to quote)...
                      >
                      Name - "Represents the name used to refer to the control in code. This
                      property can be altered only at design time and cannot be modified at run
                      time."
                      >
                      So I guess the control has some "unique attribute" other than the "name"
                      property that uniquely identifies it. Maybe that is the attribute that
                      cannot be changed ?
                      >
                      Anyway, I created 2 buttons on a form (used VS2003 for this), then modified
                      the code as follows... I expected it to crash due to the duplicate Button1
                      instantiation, instead, it appeared to apply a "last line of code wins"
                      logic set... as the sole button that appeared had its text property set to
                      "Button2".
                      >
                      Me.Button1 = New System.Windows. Forms.Button
                      ' Formerly Button 2 below
                      Me.Button1 = New System.Windows. Forms.Button
                      Me.SuspendLayou t()
                      '
                      'Button1
                      '
                      Me.Button1.Loca tion = New System.Drawing. Point(88, 8)
                      Me.Button1.Name = "Button1"
                      Me.Button1.TabI ndex = 0
                      Me.Button1.Text = "Button1"
                      '
                      'Button2
                      ' Changed the following....
                      Me.Button1.Loca tion = New System.Drawing. Point(80, 56)
                      Me.Button1.Name = "Button1"
                      Me.Button1.TabI ndex = 1
                      Me.Button1.Text = "Button2"
                      >
                      Thanks,
                      Rob
                      >
                      "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                      >
                      news:1179142282 .908849.89850@w 5g2000hsg.googl egroups.com...
                      >
                      On May 13, 6:56 pm, "Rob" <r...@yahoo.com wrote:
                      I am reading a book that says that the "name" property can be altered
                      only
                      at design time and cannot be modified at runtime.
                      >
                      Please explain this given the code below...
                      >
                      If you click Button3... fred will appear as the Name of Button1, however,
                      the handler for Button1 works still works as well...
                      >
                      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                      System.EventArg s) Handles Button1.Click
                      MsgBox("HELP")
                      >
                      End Sub
                      >
                      Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
                      System.EventArg s) Handles Button3.Click
                      Me.Button1.Name = "fred"
                      Dim i As Integer = 0
                      Dim str As String
                      >
                      While i < Me.Controls.Cou nt
                      Dim c As Control = Me.Controls(i)
                      str = c.Name
                      MsgBox(str)
                      i = i + 1
                      >
                      End While
                      End Sub
                      >
                      I am reading a book that says that the "name" property can be altered
                      only
                      at design time and cannot be modified at runtime.
                      >
                      What book is this? I would like to have a conversation with the author
                      as you can change control names at runtime with no problems.
                      >
                      the handler for Button1 works still works as well...
                      >
                      This is because it's handling the Click event for the control Button1,
                      not the control named "Button1." If you look at the designer generated
                      code, you will see that when you add a button to the designer it
                      generates something like this:
                      >
                      Private Sub InitializeCompo nent()
                      Me.Button1 = New System.Windows. Forms.Button
                      >
                      '
                      'Button1
                      '
                      Me.Button1.Loca tion = New System.Drawing. Point(65, 63)
                      Me.Button1.Name = "Button1"
                      Me.Button1.Size = New System.Drawing. Size(75, 23)
                      Me.Button1.TabI ndex = 0
                      Me.Button1.Text = "Button1"
                      Me.Button1.UseV isualStyleBackC olor = True
                      End Sub
                      >
                      Friend WithEvents Button1 As System.Windows. Forms.Button
                      >
                      This declares a button, and then in the InitialiazeComp onent method it
                      sets it's properties - including Name. And since this method should
                      run whenever a form is instantiated, this proves that runtime
                      assigning of the Name property is perfectly fine.
                      >
                      Thanks,
                      >
                      Seth Rowe
                      I expected it to crash due to the duplicate Button1
                      instantiation, instead, it appeared to apply a "last line of code wins"
                      Setting Button1 to a new button just "resets" the button - it won't
                      cause a crash.

                      Thanks,

                      Seth Rowe

                      Comment

                      Working...