CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2010
    Posts
    9

    Index was outside the bounds of the array

    Hey all,
    I am trying to put a text of textbox1 into richtextbox but apearently i am getting a error saying " Index was outside the bounds of the array."
    The code i am using is very simple, here it is : "richTextBox1->Lines[0]=textBox1->Text;"

    I wish to solve this error and know why i got it cause everything seems fine to me,
    Thanks in advanced.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Index was outside the bounds of the array

    Array returned by Lines property is read-only. You can only assign the whole array to the Lines property:

    richTextBox1->Lines = gcnew String^ [] { textBox1->Text };

  3. #3
    Join Date
    Feb 2010
    Posts
    9

    Re: Index was outside the bounds of the array

    i tried that and i got a error saying " a native array cannot contain this managed type"
    for some reason i dont think this is the right way of doing it, isnt there any other ay to write to a line in a richtextbox?
    Thanks in advanced

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Index was outside the bounds of the array

    you have to use the SetValue method to change a particular line or a range of lines.

    richTextBox1->Lines->SetValue(.....)

    edit: mhmm... somehow it doesn't work either
    Last edited by memeloo; February 18th, 2010 at 01:00 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Index was outside the bounds of the array

    Show your code.

  6. #6
    Join Date
    Feb 2010
    Posts
    9

    Re: Index was outside the bounds of the array

    I did what you said and i tried this for testing : "richTextBox1->Lines->SetValue("test",0);"
    but nothing happens, the richtextbox just stays empty :S ?
    Am i using the setvalue function incorrectly :S ?

  7. #7
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Index was outside the bounds of the array

    I got it

    Code:
    this->richTextBox1->Text =
    	"line1" + Environment::NewLine +
    	"line2" + Environment::NewLine +
    	"line3" + Environment::NewLine;
    
    int iLine = 1;
    String ^before = (String^)richTextBox1->Lines->GetValue(iLine);
    int iFirstChar = richTextBox1->GetFirstCharIndexFromLine(iLine);
    richTextBox1->Select(iFirstChar, before->Length);
    richTextBox1->SelectedText = "it works!";
    String ^after =  (String^)richTextBox1->Lines->GetValue(iLine);
    I'd suggest to change the topic of this thread to "How to change a line in a RichTextBox"
    Last edited by memeloo; February 18th, 2010 at 01:40 PM. Reason: added new topic suggestion; corrected a typo
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  8. #8
    Join Date
    Feb 2010
    Posts
    9

    Re: Index was outside the bounds of the array

    Thanks it works but do you mind explaining the code a little bit ?

  9. #9
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Index was outside the bounds of the array

    first try to find the methods and properties in the documentation RichTextBox and read about them and if you still have problems come back and we'll see
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  10. #10
    Join Date
    Feb 2010
    Posts
    9

    Re: Index was outside the bounds of the array

    ok, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured