error with string index going below 0


hope explanation in comments enough give basic idea of intended purpose , error.

      public string[] tbchatold = new string[19]; // array store userinput      int = 0; // counter ofr event below.            private void tbchat_enter(object sender, keypresseventargs e)      {        //purpose of snip-it 1: display text tryped in textbox tbchat richtextbox windowchat.        //now wrote code following stores userinput empty index in array tbchatold[]        //using counter int declayed above. im hoping when = end of index starts storing        //at index 0 again. in giving user 20 stored strings thay can go to.        //i "think" part of code above explanation.        string userinput;        if(e.keychar == (char)keys.enter)        {                    userinput = tbchat.text;          windowchat.text += userinput + "\r\n";          if (i != 19)          {            tbchatold[i] = userinput;            i++;          }          else if (i == 19)          {            = 0;            tbchatold[i] = userinput;          }          tbchat.text = "";          tbchat.focus();        }      }      int idnext = 0;      private void tbchat_altq(object sender, keyeventargs e)      {        //my purpose here give user ability cycle threw previous input, intention once         //reached index of 0, loop around either last stored string or 19 depending if index        //had string stored in them.          //ok here im having problem. im no means decent programmer, , far cry beginner :)        //so may written wrong.        //the error im getting during debug "commented arrow" below keeps going -2, of course causing a        //outofrangeexception,          if (e.keycode == keys.up)        {          int idnext = --i;          if (idnext != 0)          {            if (tbchat.text == tbchatold[idnext]) // <-------------error-------------, far, more on down.            {              --idnext;              tbchat.text = tbchatold[idnext];              }            else if (tbchat.text != tbchatold[idnext])            {              tbchat.text = tbchatold[idnext];              --idnext;            }          }          else if (idnext == 0)          {            if (tbchat.text == tbchatold[idnext])            {              idnext = --i;              tbchat.text = tbchatold[idnext];            }            else if (tbchat.text != tbchatold[idnext])            {              tbchat.text = tbchatold[idnext];                idnext = --i;            }          }                  }        }  


when user input reaches 19, becomes zero. , in other function decrementing seems incorrect. instead of assigning idnext --i, assign total length.

  private void tbchat_altq(object sender, keyeventargs e)  {      int idnext = tbchatold.length - 1;      ...  }      

may helps.


please mark post answer if solved problem. happy programming!


Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to Share webservice object to all user