makeing the names of control during run-time

Results 1 to 2 of 2

Thread: makeing the names of control during run-time

  1. #1
    Join Date
    Dec 1969

    Default makeing the names of control during run-time

    i <BR>i want to create the labels,comboxes, text boxes etc all on run time... <BR>i.e. EVEN THEIR NAMES are created on runtime...cause i dont know how many of them will be created.. <BR>it all happens in a loop and every loop creates a label and a combobox and a textbox and is created all with different names..even names are MADE on runtime.... <BR>hope someone can help...tks.. <BR>i know how to make controls on run-time where i provide the names like <BR>Label myLabel = new Label(); <BR>here i already provide the names.. <BR>but what i am looking ofr is that i want to MAKE the names during runtime and GIVE those names to the controls to create them... <BR>but the prob is when i do this<BR>dim name as string<BR>for i = 1 to 100<BR>name = "control" & i.ToString(somehting like..u know hwat i mean)<BR>&#039;now we have name = say, control1000<BR>now<BR>name = new label()<BR><BR>now istead of assigning control1000 as the name of the control, it assigns name as the name for the label..<BR>thats my prob...<BR>can u pls help..??<BR>tks.. <BR><BR>tks..

  2. #2
    Join Date
    Dec 1969

    Default The variable name...

    ...and the control name do *NOT* have to match, you know.<BR><BR>Here&#039;s an example of how you can do something like this in a windows app. Doing it with ASP.NET will be a bit different, but not conceptually so.<BR><BR>(showing only the relevant parts of the code for a simple form)<BR><BR> Public Sub New()<BR> MyBase.New()<BR><BR> &#039;This call is required by the Windows Form Designer.<BR> InitializeComponent()<BR><BR> &#039;Add any initialization after the InitializeComponent() call<BR> [hl="yellow"]Me.AddMore()[/hl]<BR> End Sub<BR><BR>Notice that there are *NO* form field in this form!<BR><BR> &#060;System.Diagnostics.DebuggerNonUserCode()&#06 2; Private Sub InitializeComponent()<BR> &#039;<BR> &#039;Form1<BR> &#039;<BR> Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)<BR> Me.ClientSize = New System.Drawing.Size(828, 426)<BR> Me.Name = "Form1"<BR> Me.Text = "Form1"<BR> End Sub<BR><BR>And then here is where I add 12 labels and 12 text boxes dynamically, each getting a separate name:<BR><BR>[hl="lightyellow"] Private Sub AddMore()<BR> Dim cnt As Integer<BR> For cnt = 1 To 12<BR> Me.SuspendLayout()<BR> <BR> &#039; See? I actually use only *one* VB variable name<BR> &#039; for *all* of the added form controls!<BR> Dim lbl As New System.Windows.Forms.Label<BR> lbl.Location = New System.Drawing.Point(30, 20 + 24 * cnt)<BR> lbl.Name = "Label" & cnt<BR> lbl.TabIndex = cnt * 2<BR> lbl.Text = "Name " & cnt<BR><BR> Dim txt As New System.Windows.Forms.TextBox<BR> txt.Location = New System.Drawing.Point(90, 20 + 24 * cnt)<BR> txt.Name = "TextBox" & cnt<BR> txt.Size = New System.Drawing.Size(433, 19)<BR> txt.TabIndex = cnt * 2 + 1<BR> txt.Text = "John Doe " & cnt<BR><BR> Me.Controls.Add(txt)<BR> Me.Controls.Add(lbl)<BR> Next<BR> Me.ResumeLayout(False)<BR> Me.PerformLayout()<BR> End Sub<BR>[/hl]

Posting Permissions

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