Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I declared Some Special Characters :
C#
private Dictionary<string, string> GetDic = new Dictionary<String, String>();


and in Constructor
C#
GetDic.Add("Oem1",";");
GetDic.Add("Tab", "Tab");
GetDic.Add("Space", "Space");


i have a richtextbox and a textbox.. what i am trying to do is if user start typing in richtextbox the same value start visble in textbox but if user pressess TAB / SPACE / ; textbox text will gets cleared and after these special chars if user start typing again the new value will be in textbox as above i said. And for these my code is..

C#
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            //MessageBox.Show(e.KeyData.ToString());
            try
            {
                if (GetDic.ContainsKey(e.KeyData.ToString()) || e.KeyCode == Keys.Enter || e.KeyCode == Keys.OemPeriod)
                {
                    //MessageBox.Show("Matching");
                    textBox1.Text = string.Empty;
                    startIndex = -1;
                    endIndex = 0;
                }
                else
                {
                    if (startIndex <0)
                    {
                        startIndex = richTextBox1.Text.Length - 1;
                        if (startIndex < 0)
                        {
                            startIndex = 0;
                        }
                    }
                    endIndex +=1;
                    if (endIndex < 0)
                    {
                        endIndex = 0;
                    }
                       textBox1.Text = richTextBox1.Text.Substring(startIndex, endIndex);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


Error I am Getting in same line and in new line too..

SQL
Index and length must refer to a location within the string.
Parameter name: length


Please Give Me some help Thank you....
Posted
Updated 11-Jun-13 22:55pm
v2
Comments
Anjanee Kumar Singh 12-Jun-13 2:44am    
Pls tell me if my question is not so clear because i m waiting for som answer...

Thank you...
Tom Wauters 12-Jun-13 5:02am    
Do you really need to use a Dictionary?
Anjanee Kumar Singh 12-Jun-13 5:04am    
I was told to use this...

It is quite clear: the String.Substring[^] method expects startindex and length not startindex and endindex.
 
Share this answer
 
v2
Comments
Anjanee Kumar Singh 12-Jun-13 5:06am    
Sir i am reading text after every special character i.e defined in (GetDic) . that why i use startindex and endindex
but i am getting this error on pressing all the keys excepts pressing alphabet key of keyboard
Zoltán Zörgő 12-Jun-13 5:20am    
That makes no difference - index is not length! Put a breakpoint at textBox1.Text = richTextBox1.Text.Substring(startIndex, endIndex); and see what are in the three values.
Anjanee Kumar Singh 12-Jun-13 5:19am    
by putting length on the place of endIndex getting error
Index and length must refer to a location within the string.
Zoltán Zörgő 12-Jun-13 5:21am    
It is about the value! If you think of index you mean index, if you think of length, you can't think of index.
Debug and see!!!!
Anjanee Kumar Singh 12-Jun-13 5:37am    
While dubug it's not giving me any error. But when i start typing very fast at some point it giving me same error..
This might be it, but the key value pairs in your dictionary are not correct.
The problem of your error is what Zoltan says.


C#
if (GetDic.ContainsKey(e.KeyData.ToString()) || e.KeyCode == Keys.Enter || e.KeyCode == Keys.OemPeriod)
              {
                  //MessageBox.Show("Matching");
                  textBox1.Text = string.Empty;
                  startIndex += endIndex+1;
                  endIndex = 0;
              }
              else
              {
                  endIndex++;
                  textBox1.Text = richTextBox1.Text.Substring(startIndex, endIndex);
              }
 
Share this answer
 
Comments
Anjanee Kumar Singh 12-Jun-13 5:57am    
With ur given code i corrected my code and its working fine now... Thank You
Tom Wauters 12-Jun-13 6:03am    
The only problem you've got is that you can't use backspace.
But you can put it in your dictionary.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900