[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# counting characters in textbox - tricky question

Posted on 2011-03-24
8
Medium Priority
?
770 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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