Results 1 to 12 of 12

Thread: Is it possible to have a control with the same name [Resolved]

  1. #1

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Question Is it possible to have a control with the same name [Resolved]

    Is it possible to have two or more controls with the same name and object type? Like two textboxes on the same form? Do you have to change a property in one of them to distingish which textbox was changed or button?
    Last edited by Porsche944; May 23rd, 2005 at 04:28 PM.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Is it possible to have a control with the same name

    Why don't you just try it.
    I don't live here any more.

  3. #3

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Re: Is it possible to have a control with the same name

    VB .Net Won't let you create a control with a conflicting name. I throught maybe there was an index property or something hidden where you could do this.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Is it possible to have a control with the same name

    There must be a reason why you want to do this. I suspect that there is another way to get to the result you are after without having two controls of the same name. After all, the name is such a trivial piece of things.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Re: Is it possible to have a control with the same name

    It's possible to use the same control I am just trying to avoid moving the text box around a lot because it will have to move a lot. It's ok tho I'll just go ahead and move the control around a lot.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Is it possible to have a control with the same name [Resolved]

    Actually you can add controls with the same name but not through the IDE. You can via code, in fact controls don't really need a name at all.
    VB Code:
    1. Dim txt As New TextBox
    2. txt.Name="txtSame"
    3. Dim txt2 As New TextBox
    4. txt2.Name="txtSame"

  7. #7
    Lively Member heuyen's Avatar
    Join Date
    Jan 2005
    Posts
    64

    Re: Is it possible to have a control with the same name [Resolved]

    hi edneeis what are the reasons why it will not allow to have the same name in design time?
    And why controls don't really need name at all?

    im just curious.
    you can't bind me.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Is it possible to have a control with the same name [Resolved]

    The IDE stops you from doing it because it makes writing code for controls easier if they are all uniquely named. I'm not saying names are bad but really its all about the references to the controls. The IDE calls the variables that hold the references to the controls the same thing as the Name property which is the useful part. Although I, personally, never use the name property again once the variables are set up.

    I'm not sure I explained very well, but I tried. Basically the IDE stops you because it makes the IDE's job easier if this rule is enforced and names are not important because they are just properties on the object. BUT the IDE makes the references (variables) the same as the name so it makes them seem important.

  9. #9
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Is it possible to have a control with the same name [Resolved]

    seems like the distinguishing factor is the variable's name...in Ed's example, they may have the same Name property but they still are two entirely different objects by name. that's interesting none-the-less...i'm gonna experiment with some run-time adding here in a bit myself.

  10. #10
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Is it possible to have a control with the same name [Resolved]

    I just did this:
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    33.     Friend WithEvents btnAdd As System.Windows.Forms.Button
    34.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    35.         Me.TextBox1 = New System.Windows.Forms.TextBox
    36.         Me.btnAdd = New System.Windows.Forms.Button
    37.         Me.SuspendLayout()
    38.         '
    39.         'TextBox1
    40.         '
    41.         Me.TextBox1.Location = New System.Drawing.Point(30, 35)
    42.         Me.TextBox1.Name = "TextBox1"
    43.         Me.TextBox1.Size = New System.Drawing.Size(190, 20)
    44.         Me.TextBox1.TabIndex = 0
    45.         Me.TextBox1.Text = ""
    46.         '
    47.         'btnAdd
    48.         '
    49.         Me.btnAdd.Location = New System.Drawing.Point(230, 35)
    50.         Me.btnAdd.Name = "btnAdd"
    51.         Me.btnAdd.Size = New System.Drawing.Size(35, 23)
    52.         Me.btnAdd.TabIndex = 1
    53.         Me.btnAdd.Text = "Add"
    54.         '
    55.         'Form1
    56.         '
    57.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    58.         Me.ClientSize = New System.Drawing.Size(292, 273)
    59.         Me.Controls.Add(Me.btnAdd)
    60.         Me.Controls.Add(Me.TextBox1)
    61.         Me.Name = "Form1"
    62.         Me.Text = "Form1"
    63.         Me.ResumeLayout(False)
    64.  
    65.     End Sub
    66.  
    67. #End Region
    68.  
    69.     Dim locY As Integer = 35
    70.  
    71.     Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    72.         Dim newControl As New TextBox
    73.         locY += 23
    74.  
    75.         With newControl
    76.             .Name = Me.TextBox1.Text
    77.             .Location = New Point(Me.TextBox1.Location.X, locY)
    78.         End With
    79.  
    80.         Me.Controls.Add(newControl)
    81.  
    82.     End Sub
    83. End Class

    It appears you CAN have the same variable name added to a container control...

  11. #11
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Is it possible to have a control with the same name [Resolved]

    Hi,

    A comment: if you are allowed to assign the same name to an object during design time, then you can write this into the code itself. Consequently it would not be possible to decide which object to move, for example. It is better that you can't do this. For objects created at run time, however, you can't hardcode the name in because the object has not yet been created.

    If you try changing the name at runtime of an object that has been created at design time, you'll find that it doesn't work. For example, create 5 button objects at design time. In buttons 1 and 2, code: msgbox(sender.name). In button 3, code: "Button1.left = button1.left + 16". In button4, code: "Button2.name = "Button1"". And in button5, code "Button2.left = button2.left + 16".
    You can thus check the names of buttons 1 and 2 and use a hardcoded method to move them, then change the names so that they are the same and try moving them both again. They'll still move as though the names hadn't been changed, despite the fact that according to the msgbox, the name of "Button2" is "Button1".

    Quack


    zaza

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Is it possible to have a control with the same name [Resolved]

    Andy in your example newControl doesn't refer to multiple controls at least not at the sametime it keeps changing to the current one instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width