Solved

Count Number fo Texts in richTextbox

Posted on 2010-08-26
18
549 Views
Last Modified: 2013-12-17
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,
0
Comment
Question by:ANINDYA
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33538796
Use the KeyDown event and use the e.Handled and e.SuppressKey members to keep the key from passing on to the textbox.
0
 

Author Comment

by:ANINDYA
ID: 33538885
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33538903
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33538907
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33538909
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
 

Author Comment

by:ANINDYA
ID: 33538984
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33538992
Hi ANINDYA,

You didn't respond to code I attach.

Regards,
VSS
0
 

Author Comment

by:ANINDYA
ID: 33538997
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:ANINDYA
ID: 33539005
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33539018
Hi,

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

Regards,
VSS
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 33539108
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
 

Author Comment

by:ANINDYA
ID: 33539189
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33539192
Hi,

Thanks for appreciation.

Regards,
VSS
0
 

Author Comment

by:ANINDYA
ID: 33539199
Expert VS00saini
this is the error message coming .
So please wait a bit.
Thanking you again
Anindya
error.JPG
0
 

Author Comment

by:ANINDYA
ID: 33539224
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33539238
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33540661
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

707 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

21 Experts available now in Live!

Get 1:1 Help Now