Count Number of Characters using C#

Today in C# tutorial, i will teach you how to create a program that counts the number of characters using C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Count Number of Characters. 2. Next, add only one Button named Button1 and labeled it as "Count Characters". Add also TextBox named TextBox1 for our input. You must design your interface like this: design 3.Now put this code for your code module. This code is for Button1_Click that will trigger to count the number of characters in your textbox. We will have to initialize first a variable that will hold the value/string inputted in our textbox and we will name it strLength as a String variable that will hold the inputted text of textbox1.
  1. string strLength = TextBox1.Text;
Then we will have to create a messagebox that will display the total number of characters in the inputted textbox. The strLength variable with its length it returns the count of attributes for an element of the textbox. We will have the ToString method also that converts the length of the textbox to its string representation so that it is suitable for display.
  1. MessageBox.Show("Number of Characters: " + strLength.Length.ToString());
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using Microsoft.VisualBasic;
  7. using System.Data;
  8. using System.Collections.Generic;
  11. namespace Count_Number_of_Characters
  12. {
  13. public partial class Form1
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  20. if (defaultInstance == null)
  21. defaultInstance = this;
  22. }
  25. public void Button1_Click(System.Object sender, System.EventArgs e)
  26. {
  27. string strLength = TextBox1.Text;
  28. MessageBox.Show("Number of Characters: " + strLength.Length.ToString());
  29. }
  30. }
  32. }
Output: output Download the source code below and try it! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: Visit and like my page on Facebook at:

Add new comment