Solved

C# counting characters in textbox - tricky question

Posted on 2011-03-24
8
746 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 48
VB.NET 2008 - SQL Timeout 9 34
C#.net split string and then check appropriate checkboxlist 4 26
Adding items to a C# list incrementally 5 28
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

789 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