Solved

vb.net dynamic control creation

Posted on 2004-08-02
3
9,746 Views
Last Modified: 2013-12-27
I'm developing an application in VB.NET on compact framework.

I'm creating a form with a variable number of controls, so i must create then at runtime. In vb6 i can create an array of controls and increment it. But not in vb.net.

so i need to create controls (textbox and combobox) giving then a name but how?
i can't declare something like  << dim "textbox_12" as new textbox >> obviously

i tried

        For i = 0 To 10
            Dim newcontrol As New TextBox
            newcontrol.Top = 20 * i
            Me.Controls.Add(newcontrol)
        Next i

and it adds the controls to the form how can i manage then? How do i know what there is in textbox_12?
i could cycle in me.controls, but i can't identify the right control because "name" property is not supported in compact framework...
0
Comment
Question by:illu666
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 11697380
I dont think you can but  here is the hack for EVB maybe you can use that Idea.

http://devbuzz.com/content/zinc_eVB_simulating_dynamic_ctrls_pg1.asp
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 11733159

You can declare all the required controls as private members

when you need the control create an instance for it .write the code for its event handlers .Attach it to eventhandler when you  want it



0
 
LVL 10

Accepted Solution

by:
prakash_prk earned 125 total points
ID: 11733236
Try this code

If you need the name property why are you inherit the control.

------------------------------------------------------

Imports System.IO

Public Class Form2
    Inherits System.Windows.Forms.Form

    Friend WithEvents Button1 As System.Windows.Forms.Button

    Dim TextBoxes(10) As MyTextBox  'Dont Create Instances
    Dim i As Integer = 0

#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)
        MyBase.Dispose(disposing)
    End Sub

    '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.
    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(200, 320)
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.Text = "Button1"
        '
        'Form2
        '
        Me.ClientSize = New System.Drawing.Size(282, 351)
        Me.Controls.Add(Me.Button1)
        Me.Text = "Form1"

    End Sub
#End Region


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBoxes(i) = New MyTextBox
        TextBoxes(i).Location = New Point(10, 10 + i * 20)
        TextBoxes(i).Size = New Size(100, 20)
        TextBoxes(i).Name = "Text" & i
        Me.Controls.Add(TextBoxes(i))
        AddHandler TextBoxes(i).TextChanged, AddressOf TextBoxex_TextChanged
        i += 1
    End Sub


    Private Sub TextBoxex_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim TempVar As MyTextBox
        TempVar = DirectCast(sender, MyTextBox)
        MsgBox(TempVar.Name)
    End Sub

End Class

Public Class MyTextBox
    Inherits TextBox
    Private _Name As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal Value As String)
            _Name = Value
        End Set
    End Property

End Class
------------------------------------------------
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exchange 2013 doesn´t allow mobil phones to connect (iphone and android) 4 110
IT Ticketing Support App 5 70
iOS Simulator for Windows? 2 154
samsung phone too less memory 36 99
This article shows how to setup the internet connectivity on Windows Mobile Emulator.   I assume that you already have Microsoft Visual Studio, Microsoft Windows Mobile SDK's and the emulator installed. The emulator is available with Visual Studi…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now