Solved

C# counting characters in textbox - tricky question

Posted on 2011-03-24
8
741 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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