Solved

C# counting characters in textbox - tricky question

Posted on 2011-03-24
8
717 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now