• We just launched and are currently in beta. Join us as we build and grow the community.

The Easiest Way to Create a Textbox Programmatically in C#

sadegh.di

Ad Budget Optimizer
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
58
Likes
39
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I will teach you how to create a textbox programmatically in c#. This method has the ability to add an object in the form by clicking a button. It is also demonstrating how to get the collection of Controls within the control. Let's begin.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_3.png

Step 2

Do the form just like shown below.
pstxt.png

Step 3

Press F7 to open the code editor. In the code editor, declare private variables for the location and count control.


  1. private

    int

    count_control =

    0

    ;
  2. private

    Point location_control =

    new

    Point(

    10

    , 50

    )

    ;

Step 4

Create a method for creating a textbox.

  1. private

    void

    createTexbox(

    )
  2. {
  3. // 'INCREMENT THE COUNT_CONTROL.
  4. count_control +=

    1

    ;
  5. //'CHECKING IF THE TEXTBOX HAS REACHED TO 6 OR NOT
  6. if

    (

    count_control <=

    6

    )
  7. {
  8. // initialize a new object
  9. TextBox txt_new =

    new

    TextBox(

    )

    ;
  10. //set a object properties
  11. txt_new.

    Name

    =

    "txt "

    +

    count_control.

    ToString

    (

    )

    ;
  12. txt_new.

    Text

    =

    "textbox "

    +

    count_control.

    ToString

    (

    )

    ;
  13. txt_new.

    Location

    =

    new

    Point(

    location_control.

    X

    +

    10

    , location_control.

    Y

    )

    ;
  14. txt_new.

    Width

    =

    350

    ;
  15. location_control.

    Y

    +=

    txt_new.

    Height

    +

    10

    ;
  16. // create a textbox
  17. Controls.

    Add

    (

    txt_new)

    ;
  18. }

    else
  19. {
  20. DialogResult result =

    MessageBox.

    Show

    (

    "You've reached 6 controls. Do you want to clear controls to start again?"

    , "Proceed"

    , MessageBoxButtons.

    OKCancel

    , MessageBoxIcon.

    Information

    )

    ;
  21. if

    (

    result==

    DialogResult.

    OK

    )
  22. {
  23. //clear the controls
  24. Controls.

    Clear

    (

    )

    ;
  25. //control return to 0
  26. count_control =

    0

    ;
  27. // set a new point for controls
  28. location_control =

    new

    Point(

    10

    , 50

    )

    ;
  29. //create a new textbox
  30. createTexbox(

    )

    ;
  31. // add button again
  32. Controls.

    Add

    (

    button1)

    ;
  33. }
  34. }

  35. }

    s

Step 5

Call the method that you have created in the first load of the form.

  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. createTexbox(

    )

    ;
  4. }


Step 6
Write the following code for creating a textbox when the button is clicked.

  1. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  2. {
  3. createTexbox(

    )

    ;
  4. }

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below

 

442,401

317,942

317,951

Top