Can any one please help me in following in VB.NET2005:

I want that whenever the word setting appears a textbox would be created during runtime i.e here it appears for 4 times so 4 textboxes should be created, if it appears for 5 times then 5 textboxes would be created......
Can it be done using array? If yes then how?


<applicationSettings>
<WindowsApplication1.My.MySettings>
<setting name="Astrology" serializeAs="String">
<value>6</value>
</setting>
<setting name="Cricket" serializeAs="String">
<value>7</value>
</setting>
<setting name="Foreigncurrency" serializeAs="String">
<value>8</value>
</setting>
<setting name="Jobs" serializeAs="String">
<value>9</value>
</setting>
</WindowsApplication1.My.MySettings>
</applicationSettings>

Please help soon

Dani AI

Generated

A simple, reliable approach is: count the number of <setting> elements in your app config and create that many TextBox controls at runtime. That follows the loop idea suggested by , but shows a VB.NET way to get the count from your config and a recommended runtime pattern (use a List(Of TextBox) rather than a fixed array for easier management).

' Put this in Form.Load or another UI-thread initialization routine
Dim cfgPath As String = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Dim doc As New System.Xml.XmlDocument()
doc.Load(cfgPath)

' select <setting> nodes under applicationSettings (adjust XPath if your config differs)
Dim nodes As System.Xml.XmlNodeList = doc.SelectNodes("/configuration/applicationSettings/*/setting")
Dim count As Integer = nodes.Count

' fallback if config layout is different
If count = 0 AndAlso My.Settings.Properties IsNot Nothing Then
    count = My.Settings.Properties.Count
End If

Dim textboxes As New System.Collections.Generic.List(Of TextBox)()
Dim startX As Integer = 10, startY As Integer = 10, spacing As Integer = 26

For i As Integer = 0 To count - 1
    Dim tb As New TextBox()
    tb.Name = "txtDynamic" & i.ToString()
    tb.Width = 220
    tb.Location = New System.Drawing.Point(startX, startY + i * spacing)
    ' optional: prefill from setting name/value:
    ' If nodes.Count > i Then tb.Text = nodes(i).InnerText
    Me.Controls.Add(tb)
    textboxes.Add(tb)
Next

Notes and tips:

  • If you really want an array, use ReDim textArray(count - 1) As TextBox and assign each created TextBox to textArray(i), but List(Of TextBox) is easier to resize and iterate.
  • For automatic layout use a FlowLayoutPanel (TopDown) instead of computing Location manually.
  • To persist values, map each textbox to the corresponding setting name (use nodes(i).Attributes("name").Value) and save to My.Settings or update config as needed.
  • Always create UI controls on the UI thread (Form.Load is fine) and remove/dispose them if you recreate the set later.

This answers 's question while keeping the runtime layout robust and maintainable.

I don't really understand you but I'll answer your thread subject

and I am sorry I'll write code in C# as I don't know VB.net at all :$

private int GetNumberOfControls()
{
//returns the number of control to be created
}

private void CreateTextbox()
{
TextBox textBox;

for(int i=0; i<GetNumberOfControls(); i++)
{
textBox = new TextBox();
textBox.Location = new Point(YourX, YourY);
..... //other properties
..... //other properties
this.Controls.Add(textBox);
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.