Count Number fo Texts in richTextbox

Experts
I am trying to stop user to enter more than 50 chars.
please see the code below.
The problem I am facing are
1.As the 50th char is inserted the messagebox is firing but as I am pressing the OK of messagebox then there after it is taking the last pressed char also.
2.I have tried the same code with the keypress event but same problem is there.

here is the code

public Int32 stop = 0;
 private void richTextBox_ActionItems_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (stop == 1)
            {
                MessageBox.Show("You have exceeded the MAX limit");
            }
        }
        public Int32 stop = 0;
        private void richTextBox_ActionItems_KeyUp(object sender, KeyEventArgs e)
        {
            int noofChar = 0;
            noofChar = richTextBox_ActionItems.Text.Length;
            if (((50 - noofChar) == 0) || ((50 - noofChar) < 0))
            {
                MessageBox.Show("You have exceeded the MAX limit");
                stop = 1;
                return;
            }
            else
            {
                label16.Text = (50 - noofChar).ToString();
            }
        }


Thanking you,
ANINDYAAsked:
Who is Participating?
 
Vikram Singh SainiConnect With a Mentor Software Engineer cum AD DeveloperCommented:
Hi,

Please check the code attach. I think it should work according to your requirements.

And let us know the result back.

Regards,
VSS

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            int charactersLeft = 50 - richTextBox1.Text.Length;

            if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete ||
                e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode==Keys.Up ||e.KeyCode==Keys.Down)
            {
                e.SuppressKeyPress = false;
                richTextBox1.ReadOnly = false;
            }

            if (richTextBox1.Text.Length > 51 && e.KeyCode != Keys.Back && e.KeyCode!=Keys.Delete 
                && e.KeyCode!=Keys.Left && e.KeyCode!=Keys.Right && e.KeyCode!=Keys.Up && e.KeyCode!=Keys.Down)
            {
                string text = richTextBox1.Text;
                e.SuppressKeyPress = true;

                DialogResult result = MessageBox.Show("You have exceeded limit of characters!");
                if (result == DialogResult.OK)
                {
                    if (richTextBox1.Text.Length > 51)
                    {
                        richTextBox1.ReadOnly = true;                        
                    }
                }

            }
            else if (richTextBox1.Text.Length < 51)
            {
                label1.Text = "You have " + charactersLeft.ToString() + " characters left out of 50 characters!";                
            }           
        }

Open in new window

0
 
käµfm³d 👽Commented:
Use the KeyDown event and use the e.Handled and e.SuppressKey members to keep the key from passing on to the textbox.
0
 
ANINDYAAuthor Commented:
Expert kaufmed
is the following you want to say ----

public Form_AddInteractions(int id, int intID, int interactionMode)
        {
            InitializeComponent();
            _opportunityid = id;
            _interactionmode = interactionMode;
            _interactionid=intID;


            RichTextBox Rtb = new RichTextBox ();
            this.Controls.Add(Rtb);
            Rtb.KeyPress += new KeyPressEventHandler(KeyPress);

        }


private void richTextBox_ActionItems_KeyDown(object sender, KeyEventArgs e)
        {
           

            if (e.Handled == true)
            {
                int noofChar = 0;
                noofChar = richTextBox_ActionItems.Text.Length;
                if (((50 - noofChar) == 0) || ((50 - noofChar) < 0))
                {
                    MessageBox.Show("You have exceeded the MAX limit");
                    stop = 1;
                    return;
                }
                else
                {
                    label16.Text = (500 - noofChar).ToString();
                }
               
            }
            if (stop == 1)
            {
                MessageBox.Show("You have exceeded the MAX limit");
            }

           
        }
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
käµfm³d 👽Commented:
Almost:
private void richTextBox_ActionItems_KeyDown(object sender, KeyEventArgs e)
{
	if (richTextBox_ActionItems.Text.Length == 50)
	{
	    MessageBox.Show("You have exceeded the MAX limit");
	    stop = 1;
	    e.Handled = true;
	    e.SuppressKeyPress = true;
	    return;
	}
	else
	{
	    label16.Text = (500 - noofChar).ToString();
	}
}

Open in new window

0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

Please check code.

Regards,
VSS

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            int charactersLeft = 50 - textBox1.Text.Length;

            if (e.KeyCode == Keys.Back)
            {
                e.SuppressKeyPress = false;
            }

            if (textBox1.Text.Length >= 50 && e.KeyCode!=Keys.Back)
            {
                e.SuppressKeyPress = true;
                label1.Text = "You have exceeded limit of characters!";
            }
            else if (textBox1.Text.Length < 50)
            {
                label1.Text = "You have " + charactersLeft.ToString() + " characters left out of 50 characters!";
            }
        }

Open in new window

0
 
käµfm³d 👽Commented:
Sorry, I made some incorrect modification to your original code. I've added back your logic and included what I was suggesting here:
private void richTextBox_ActionItems_KeyDown(object sender, KeyEventArgs e)
{
	int noofChar = 0;
        noofChar = richTextBox_ActionItems.Text.Length;

        if (((50 - noofChar) == 0) || ((50 - noofChar) < 0))
	{
	    MessageBox.Show("You have exceeded the MAX limit");
	    stop = 1;
	    e.Handled = true;
	    e.SuppressKeyPress = true;
	    return;
	}
	else
	{
	    label16.Text = (500 - noofChar).ToString();
	}
}

Open in new window

0
 
ANINDYAAuthor Commented:
Expert Kaufmed
Thanks for the reply .
But sir your code is not working as it is showing the same thing as I had done.
That is as I am clicking the OK of the messagebox then it is taking the character which last pressed.
I have tried many times but the result is same.
Thanking you ,
Anindya
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi ANINDYA,

You didn't respond to code I attach.

Regards,
VSS
0
 
ANINDYAAuthor Commented:
Expert vs00saini
Thanks for the reply .

I would like to say that your code is working perfectly.

But sir the following code is not working

int charactersLeft = 50 - richTextBox_ActionItems.Text.Length;

            if (e.KeyCode == Keys.Back)
            {
                e.SuppressKeyPress = false;
            }

            if (richTextBox_ActionItems.Text.Length >= 50 && e.KeyCode != Keys.Back)
            {
                e.SuppressKeyPress = true;
                MessageBox.Show("You have exceeded limit of characters!");
                //label18.Text = "You have exceeded limit of characters!";
            }
            else if (richTextBox_ActionItems.Text.Length < 50)
            {
                //label18.Text = "You have " + charactersLeft.ToString() + " characters left out of 50 characters!";
            }


Now here in this case the result is same as of my code and kaufmed code .
That is
That is as I am clicking the OK of the messagebox then it is taking the character which last pressed.
I have tried many times but the result is same.

Thanking you
Anindya

0
 
ANINDYAAuthor Commented:
Expert VS00saini
Messagebox.show(); case when I am using in your code is not working .
If I do the lebel as you have done in your code then it is working perfectly.
Thanking you,
Anindya
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

I am trying for MessageBox also. I think it should also work.

Regards,
VSS
0
 
ANINDYAAuthor Commented:
Expert vs00saini
Thanks  a  lot for the reply and also for giving the beautiful code .
I do not know why I can not give you points .
Each time I am giving you points it  is showing error message.
Please wait for a while.
I am grateful to you that you found some time for me and replied .
It is working fine.
I have a small request that is if you can do a bit of favor to me then please visit the following link as I have another qeury which I have put in EE.
here is the URL
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26433570.html
Take regards
Thanking you
Anindya Chatterjee
Bangalore
India
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

Thanks for appreciation.

Regards,
VSS
0
 
ANINDYAAuthor Commented:
Expert VS00saini
this is the error message coming .
So please wait a bit.
Thanking you again
Anindya
error.JPG
0
 
ANINDYAAuthor Commented:
Expert VS00saini
I have forwarded this matter to the Administrator of the EE.
Please see the link below:
http://www.experts-exchange.com/Community_Support/General/Q_26433612.html
Take regards
Thanking  you,
Anindya Chatterjee
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi Anindya Chatterjee,

I understand that sometimes technical glitches comes in.

But there is no problem in it. As the problem solves you can allot points.

Thanks for sharing that piece of info with me. Now keep on coding; they will inform you as matter resolves.

Regards,
VSS
0
 
Naman GoelSoftware engineer 1Commented:
You can apply this simple code for this
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (richTextBox1.Text.Length == 50 && e.KeyCode != Keys.Back)
            {
                MessageBox.Show("You have exceeded the MAX limit");
                e.SuppressKeyPress = true;
                richTextBox1.Undo();
            }
            else
            {
                e.SuppressKeyPress = false;
            }
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.