Create textbox at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • subhankarmitra1
    New Member
    • Nov 2007
    • 1

    #1

    Create textbox at runtime

    How to create textbox randomly by written code in a command button?
    Last edited by Killer42; Nov 27 '07, 05:59 AM.
  • AHMEDYO
    New Member
    • Nov 2007
    • 112

    #2
    HI...

    what you mean by randomaly , you mean dynamically???

    Comment

    • AHMEDYO
      New Member
      • Nov 2007
      • 112

      #3
      Hey..

      You have 3 ways to do that I will descript you begin from simple method and terminate by complex one.

      Method 1:

      You can create your run-time controls first by placing Textbox control in your form and then change index property to 0 to create array of textbox control, then you can use it to load new controls at runtime, and you can receive events from all of controls by Text1 control events and use Index parameter to identify your control.

      [CODE=vb]Private Sub Command1_Click( )
      Dim TextControl As TextBox
      ControlID = ControlID + 1
      Load Text1(ControlID )
      Set TextControl = Text1(ControlID )
      With TextControl
      .Left = (Text1(ControlI D - 1).Left + Text1(ControlID - 1).Width) + 10
      .Top = 20
      .Width = 100
      .Height = 20
      .Visible = True
      End With
      End Sub[/CODE]

      '============== =============== =============== ============
      Method 2:

      You can create new controls using Form.Controls object, but you can't receive events from VB IDE about these controls, to handle events you must work a lot using API functions.

      [CODE=vb]Private Sub Command1_Click( )
      Dim TextControl As TextBox
      Dim PrevTextControl As TextBox
      ControlID = ControlID + 1
      Set TextControl = Form1.Controls. Add("Vb.TextBox ", "Text" & ControlID)
      If (ControlID > 1) Then
      Set PrevTextControl = Form1.Controls( "Text" & (ControlID - 1))
      Else
      Set PrevTextControl = TextControl
      End If
      With TextControl
      .Left = (PrevTextContro l.Left + PrevTextControl .Width) + 10
      .Top = 20
      .Width = 50
      .Height = 20
      .Visible = True
      End With
      End Sub[/CODE]

      '============== =============== =============== ============
      Method 3:

      Using API only to create new textbox controls at run-time and sure you will work a lot to receive events and just to change the text in the textbox controls, you can use CreateWindows API Function to do that.

      GOOD LUCK
      Last edited by Killer42; Nov 27 '07, 06:04 AM.

      Comment

      • pratikm23
        New Member
        • Apr 2010
        • 9

        #4
        Originally posted by AHMEDYO
        Hey..

        You have 3 ways to do that I will descript you begin from simple method and terminate by complex one.

        Method 1:

        You can create your run-time controls first by placing Textbox control in your form and then change index property to 0 to create array of textbox control, then you can use it to load new controls at runtime, and you can receive events from all of controls by Text1 control events and use Index parameter to identify your control.

        [CODE=vb]Private Sub Command1_Click( )
        Dim TextControl As TextBox
        ControlID = ControlID + 1
        Load Text1(ControlID )
        Set TextControl = Text1(ControlID )
        With TextControl
        .Left = (Text1(ControlI D - 1).Left + Text1(ControlID - 1).Width) + 10
        .Top = 20
        .Width = 100
        .Height = 20
        .Visible = True
        End With
        End Sub[/CODE]

        '============== =============== =============== ============
        Method 2:

        You can create new controls using Form.Controls object, but you can't receive events from VB IDE about these controls, to handle events you must work a lot using API functions.

        [CODE=vb]Private Sub Command1_Click( )
        Dim TextControl As TextBox
        Dim PrevTextControl As TextBox
        ControlID = ControlID + 1
        Set TextControl = Form1.Controls. Add("Vb.TextBox ", "Text" & ControlID)
        If (ControlID > 1) Then
        Set PrevTextControl = Form1.Controls( "Text" & (ControlID - 1))
        Else
        Set PrevTextControl = TextControl
        End If
        With TextControl
        .Left = (PrevTextContro l.Left + PrevTextControl .Width) + 10
        .Top = 20
        .Width = 50
        .Height = 20
        .Visible = True
        End With
        End Sub[/CODE]

        '============== =============== =============== ============
        Method 3:

        Using API only to create new textbox controls at run-time and sure you will work a lot to receive events and just to change the text in the textbox controls, you can use CreateWindows API Function to do that.

        GOOD LUCK
        Hello sir i have used ur method 1 which is excellent. it lets me add a text box on runtime but i do not know a particular way to add the textboxes besides each other. I mean that if i have to add three text boxes using a command button ADD at runtime they get added behind each other. How can i resolve this using method 1.

        Comment

        • pratikm23
          New Member
          • Apr 2010
          • 9

          #5
          Originally posted by AHMEDYO
          Hey..

          You have 3 ways to do that I will descript you begin from simple method and terminate by complex one.

          Method 1:

          You can create your run-time controls first by placing Textbox control in your form and then change index property to 0 to create array of textbox control, then you can use it to load new controls at runtime, and you can receive events from all of controls by Text1 control events and use Index parameter to identify your control.

          [CODE=vb]Private Sub Command1_Click( )
          Dim TextControl As TextBox
          ControlID = ControlID + 1
          Load Text1(ControlID )
          Set TextControl = Text1(ControlID )
          With TextControl
          .Left = (Text1(ControlI D - 1).Left + Text1(ControlID - 1).Width) + 10
          .Top = 20
          .Width = 100
          .Height = 20
          .Visible = True
          End With
          End Sub[/CODE]

          '============== =============== =============== ============
          Method 2:

          You can create new controls using Form.Controls object, but you can't receive events from VB IDE about these controls, to handle events you must work a lot using API functions.

          [CODE=vb]Private Sub Command1_Click( )
          Dim TextControl As TextBox
          Dim PrevTextControl As TextBox
          ControlID = ControlID + 1
          Set TextControl = Form1.Controls. Add("Vb.TextBox ", "Text" & ControlID)
          If (ControlID > 1) Then
          Set PrevTextControl = Form1.Controls( "Text" & (ControlID - 1))
          Else
          Set PrevTextControl = TextControl
          End If
          With TextControl
          .Left = (PrevTextContro l.Left + PrevTextControl .Width) + 10
          .Top = 20
          .Width = 50
          .Height = 20
          .Visible = True
          End With
          End Sub[/CODE]

          '============== =============== =============== ============
          Method 3:

          Using API only to create new textbox controls at run-time and sure you will work a lot to receive events and just to change the text in the textbox controls, you can use CreateWindows API Function to do that.

          GOOD LUCK
          I got it!!.Theres absoluetly no problem with the code thank u.

          Comment

          Working...