Creating a TextBox in C#

Creating a TextBox Programmatically in C#

In this turtorial, I will teach you how to create a TextBox programmatically by using C#.net. This topic explains a simple way on how to create a Textbox in the form. The TextBox will be displayed in the specified location according to your condition.

addTextBoxProgFig.4

So, let’s begin:

Open Microsoft Visual Studio 2008 and create a new Windows Form Application for C#. The Form will look like this.

addTextBoxProgFig.1

After creating a Windows Form Application, go to the solution explorer and click the “code view“.

addTextBoxProgFig.3

In the code view, declare the variables for counting the controls that will be added and setting up the location of the controls in the Form.

 //'SET A CLASS VARIABLE TO BE USED DURING THE FORM
 static int count_control = 0;
 static Point location_control= new Point(10, 50);

After declaring variables, create a method for creating a TextBox.

private void CreateATexBox()
 {
 //'INCREMENT THE COUNT_CONTROL.
 count_control += 1;
 //'CHECKING IF THE TEXTBOX HAS REACHED TO 5 OR NOT
 if( count_control <= 5 ){
 //'SET A NEW TEXTBOX
 TextBox new_textbox = new TextBox();
 //'ADD THE PROPERTIES OF THE TEXTBOX.
 new_textbox.Name = "Textbox" + count_control.ToString();
 new_textbox.Text = "Textbox" + count_control.ToString();
 new_textbox.Location = new Point(location_control.X + 10, location_control.Y);
 new_textbox.Width = 340; //'SET THE WIDTH OF THE TEXTBOX
 location_control.Y += new_textbox.Height + 10; //'ADDING A SPACE OF EVERY TEXTBOX
 // 'ADD THE NEW TEXTBOX TO THE COLLECTION OF CONTROLS
 Controls.Add(new_textbox);
 } else {
 
 //'CHECKING IF YOU WANT TO CLEAR THE CONTROLS THAT YOU HAVE ADDED.
 if (MessageBox.Show("You've reached 5 controls. Do you want to clear controls to start again?", 
 "Proceed",MessageBoxButtons.OKCancel,MessageBoxIcon.Information) == DialogResult.OK){
 Controls.Clear(); //'CLEARING THE CONTROL
 count_control = 0; //'RETURNING THE COUNT_CONTROL TO ITS DEFAULT VALUE
 location_control = new Point(10, 50); //'SET A NEW POINT FOR THE CONTROLS
 CreateATexBox(); //'PUT A CONTROL SO THAT YOU CAN ADD ANOTHER CONTROLS
 Controls.Add(button1); //'ADD THE BUTTON1 AGAIN THAT YOU HAVE DRAG ON THE FORM.
 }
 }
 }

Go back to the design view, double click the button and do the following code for creating a TextBox in the Form.

 private void button1_Click(object sender, EventArgs e)
 {
 //'A NEW TEXTBOX WILL BE ADDED EVERYTIME YOU CLICK THE BUTTON
 CreateATexBox();
 }

 

Output:

addTextBoxProgFig.4

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – tnt

ABOUT PROJECTPROJECT DETAILS
Project Name :Creating a TextBox Programmatically in C#
Project Platform :C#
Programming Language Used:C# Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Visual Studio 2019
Project Type :Desktop Application
Database:None
Upload Date and Time:June 18, 2016- 9:37 am

Frequently Asked Questions

What does this C# WinForms control example demonstrate?

Core Windows Forms control (TextBox, CheckBox, RadioButton, ListBox, TreeView, ProgressBar, Button, DateTimePicker, etc.) usage pattern: properties, events, data binding, common gotchas. Foundation skill for building any C# desktop application capstone.

What .NET and SQL Server versions does this project require?

Most projects in this batch use C# WinForms on .NET Framework 4.5+ (the dominant stack for tutorial sites) with SQL Server 2012 Express or higher. A few newer projects use .NET 6/7/8. To run: install Visual Studio 2019 / 2022 (Community edition is free), install SQL Server Express + SSMS, open the .sln file, build, run.

How do I set up the database for this C# project?

Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance (e.g. localhost\SQLEXPRESS). Right-click Databases > Restore Database OR > New Database then import the included .sql script. Update the connection string in App.config (or in code-behind) with your server name + credentials. Rebuild and run.

Can I use this C# project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD form is too narrow for full capstone scope. Add: role-based access (admin/staff/customer login redirect), Crystal Reports or RDLC reports, dashboard with Chart controls, audit log, multi-branch support. Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘object reference not set’?

Three common C# issues: (1) Connection error: SQL Server isn’t running OR connection string in App.config has wrong server name. Open SQL Server Configuration Manager + verify SQL Server (SQLEXPRESS) service is running. (2) NullReferenceException: a control reference or DB column returned NULL, add a check or use ?? operator. (3) Build error ‘The type or namespace could not be found’: missing assembly reference, add via Project > Add Reference.

Where can I find more C# projects with source code?

Browse the C# Projects hub for the full library. For other .NET stacks see VB.NET Projects (300+ Windows Forms systems). For ASP.NET WebForms see ASP.NET Projects. For BSIT capstone idea lists see 150 Best Capstone Project Ideas.

Leave a Comment