Wednesday, 29 August 2012

How to add dynamic controls in VB.NET or C# windows forms | How to add textbox at runtime | How to dynamically add control with loop at Runtime

                       Hi Friends, in this article I will explain about How to add dynamic controls in VB.NET or C# windows forms or  How to add textbox at runtime or How to dynamically add control with loop at Run time.
                       Suppose if we want to add textboxes in the loop then we use this process. Suppose if we to display some number of textboxes then we can’t take the textboxes before run the code. For that time we can display textboxes at runtime. Not only textboxes we can any controls like labels, buttons, checkboxes etc.
                                      I already explained in the previous article How to Read data from Excel in VB.NET or C# windows forms | Export data from Excel to Text file 
                                      All the controls on a form are stored in the Controls collection. For this we have to use the System.Windows.Forms. All the controls are inherited from System.Windows.Forms. For this we have to use the System.Windows.Forms class.

Example: In this we add multiple textboxes dynamically.
Copy and paste the below code and execute then we will get 10 textboxes dynamically.

Imports System.Windows.Forms
Public Class Form1
    Dim i As Integer, txt As System.Windows.Forms.TextBox, lbl As System.Windows.Forms.Label
    Dim btn As System.Windows.Forms.Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i = 1 To 10  'for 1 until 10, create 10 textboxes
            'create dynamic textboxes
            txt = New System.Windows.Forms.TextBox  'create an instance of a TextBox control.
            txt.Name = "txt" & i 'name for the textboxes (txt1, txt2, etc...
            Me.Controls.Add(txt) 'add the textboxes to me.contols
            txt.Location = New System.Drawing.Point(160, 10 + (i * 25)) 'location points
Next
End Sub
End Class

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
Public Class Form1
{
     int i;
     System.Windows.Forms.TextBox txt;
     System.Windows.Forms.Label lbl;
     System.Windows.Forms.Button btn;
     private void Form1_Load(System.Object sender, System.EventArgs e)
     {
          //for 1 until 10, create 10 textboxes
          for (i = 1; i <= 10; i++) {
              //create dynamic textboxes
              txt = new System.Windows.Forms.TextBox();
              //create an instance of a TextBox control.
              txt.Name = "txt" + i;
              //name for the textboxes (txt1, txt2, etc...
              this.Controls.Add(txt);
              //add the textboxes to me.contols
              txt.Location = new System.Drawing.Point(160, 10 + (i * 25));
              //location points
          }
     }
    Public Form1()
     {
          Load += Form1_Load;
     }
}

 In the above code we declare textbox as  txt As System.Windows.Forms.TextBox,i.e.textbox is inherited from the System.Windows.Forms.TextBox.

If we dont give the location points then the textbox is not display so we have give the location points ,in the above i take it as   txt.Location = New System.Drawing.Point(160, 10 + (i * 25))

We can apply the all properties of textbox to this box like  txt.Name = "txt" & i
Txt.text=”Aspdotnet-Kishore
If we write  Me.Controls.Add(txt).It will add the textbox to the form.
The output of the above code will like below figure.......


No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.