Solved

C# counting characters in textbox - tricky question

Posted on 2011-03-24
8
752 Views
Last Modified: 2012-05-11
Hi there;

I want to count the characters in the textbox but:

the count is just the result of the active cursor position. How can I do that?

abcd|efghijkl
So the count is 4 until/before cursor.

Note that this is for textbox.

Kind regards.
0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:razza_b
ID: 35206071
why dont you use something like...

int xPos = textBox1.Cursor.HotSpot.X;
xPos = textBox1.Text.Length;
label = new label();
label.text = (string)xPos;
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 35206080
You just need to know the cursor position really, which you can get using:
int count = textBox1.SelectionStart;

Open in new window

0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35206142
Hi

 textBox1.Text.Length;

should be enough to get the character count
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35206177
He's asking for the character count up to the current cursor position, not the length of the whole content.
0
 
LVL 1

Expert Comment

by:razza_b
ID: 35206229
What about something like this??

if (e.KeyChar == '\'')
            {
                e.Handled = true;
                int start = textBox1.SelectionStart;
                string left = textBox1.Text.Substring(0, textBox1.SelectionStart);
                string right = textBox1.Text.Substring(start, textBox1.Text.Length - start);
                textBox1.Text = left + "\\\'" + right;
                textBox1.SelectionStart = start + 2;
               
            }
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35208140
razza_b:
what is that backslash for?

Kind regards.
0
 
LVL 1

Expert Comment

by:razza_b
ID: 35208346
sorry that was just something that someone used and i thougt you could modify it to suit your needs, you can use any check that you think would be useful in your code.....

if (textbox1.text != "")
{
      int start = textBox1.SelectionStart;
      string left = textBox1.Text.Substring(0, textBox1.SelectionStart);
      string right = textBox1.Text.Substring(start, textBox1.Text.Length - start);
      int findPos = (int)left + right;
      int pos = findPos;
      .......
}

try along the lines of that.
0
 
LVL 1

Assisted Solution

by:razza_b
razza_b earned 250 total points
ID: 35208433
in fact you just need...

if (textbox1.text != "")
{
      int start = textBox1.SelectionStart;
      string left = textBox1.Text.Substring(0, textBox1.SelectionStart);
      int pos = left ;
}
or as carl_tawn says : int count = textBox1.SelectionStart;


:)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MailAddress in vb 4 36
Problem to copy file 14 54
C# DataTable (in memory) vs SQL Lookups 4 37
Fastest way to get file count in MS Server 2012 3 13
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question