|
-
May 23rd, 2005, 09:53 AM
#1
Thread Starter
Addicted Member
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.
-
May 23rd, 2005, 09:56 AM
#2
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.
-
May 23rd, 2005, 03:32 PM
#3
Thread Starter
Addicted Member
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.
-
May 23rd, 2005, 03:39 PM
#4
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
 
-
May 23rd, 2005, 04:27 PM
#5
Thread Starter
Addicted Member
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.
-
May 24th, 2005, 08:30 PM
#6
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:
Dim txt As New TextBox
txt.Name="txtSame"
Dim txt2 As New TextBox
txt2.Name="txtSame"
-
May 24th, 2005, 08:35 PM
#7
Lively Member
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.
-
May 25th, 2005, 08:53 PM
#8
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.
-
May 26th, 2005, 07:24 AM
#9
Frenzied Member
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.
-
May 26th, 2005, 07:34 AM
#10
Frenzied Member
Re: Is it possible to have a control with the same name [Resolved]
I just did this:
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents btnAdd As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(30, 35)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(190, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(230, 35)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(35, 23)
Me.btnAdd.TabIndex = 1
Me.btnAdd.Text = "Add"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim locY As Integer = 35
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim newControl As New TextBox
locY += 23
With newControl
.Name = Me.TextBox1.Text
.Location = New Point(Me.TextBox1.Location.X, locY)
End With
Me.Controls.Add(newControl)
End Sub
End Class
It appears you CAN have the same variable name added to a container control...
-
May 26th, 2005, 12:58 PM
#11
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
-
May 26th, 2005, 10:18 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|