Count Number of Characters using C#
Submitted by donbermoy on Thursday, June 5, 2014 - 19:43.
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:
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.
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.
Full source code:
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: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- string strLength = TextBox1.Text;
- MessageBox.Show("Number of Characters: " + strLength.Length.ToString());
- using System.Diagnostics;
- using System;
- using System.Windows.Forms;
- using System.Collections;
- using System.Drawing;
- using Microsoft.VisualBasic;
- using System.Data;
- using System.Collections.Generic;
- namespace Count_Number_of_Characters
- {
- public partial class Form1
- {
- public Form1()
- {
- InitializeComponent();
- if (defaultInstance == null)
- defaultInstance = this;
- }
- public void Button1_Click(System.Object sender, System.EventArgs e)
- {
- string strLength = TextBox1.Text;
- MessageBox.Show("Number of Characters: " + strLength.Length.ToString());
- }
- }
- }
Add new comment
- 66 views