Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
I want to know how many dot in a line.User'll enter a string in multipleline richtext.A function will count the number of dots in rows.
If User press back key,reducing the number of dots.
If the dot in another line,does not increase the number of dots.
Posted 27-Jul-12 0:53am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you want to use javascript code then here you go:-
  function CountChar(sender) {
            var str = sender.value;
            var res = str.split("a").length - 1;
            document.getElementById("lblText").innerHTML = res;
        }
Now onkeydown property of TextBox call the function:-
 <asp:TextBox ID="txtText" runat="server" onkeydown="CountChar(this)" />
 
Good luck
  Permalink  
v2
Comments
giatuan2011 at 28-Jul-12 1:09am
   
How to reduce Count when I hit back or delete dots?
Rajesh Biswas at 28-Jul-12 7:24am
   
you can use javascript for that, see my updated solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use this code:-
        string str = txtText.Text;
        int res = str.Length - str.Replace(".", "").Length;
        lblText.Text = res.ToString();
Or you can use linq 'Count' extension method,
        string str = txtText.Text;
        int res = str.ToCharArray().Count(c => c == '.');
        lblText.Text = res.ToString();

 
Good luck
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try funtion in javascript and call this funtion in onkeydown event.
function textCounter(field,countfield,maxlimit)
        {
        //alert(maxlimit);
            if(field.value.length>maxlimit)
            field.value=field.value.substring(0,maxlimit);
            else
            countfield.value=maxlimit - field.value.length;
            if(countfield.value==0)
            alert("Maxlimit is over");
        }
 
Thanks
AShish
  Permalink  
Comments
Wes Aday at 28-Jul-12 0:16am
   
A javascript answer to a C# question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,105
1 DamithSL 5,079
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,617
4 Kornfeld Eliyahu Peter 4,384


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 28 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100