Results 1 to 3 of 3

Thread: Editing a RichTextBox from another class. [RESOLVED]

  1. #1

    Thread Starter
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Editing a RichTextBox from another class. [RESOLVED]

    So far, I have a richTextBox made with Visual Studio (Named richTextBox1). I have a separate class that needs to change the text in the box. The textbox is in class Form1.

    So far, what I have is :

    In class Form1
    Code:
    		public void txtchange(string changeinfo)
    		{
    			richTextBox1.Text += "\n" + changeinfo;
    		}
    and in the other class, I have
    Code:
    Form1.txtchange("blah blah");


    The error it gives me on build is:

    Form1.cs(214): An object reference is required for the nonstatic field, method, or property 'GUIbot.Form1.txtchange(string)'
    Last edited by deranged; Jul 5th, 2005 at 02:22 AM. Reason: resolution found.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Editing a RichTextBox from another class.

    Your methos is declared as "public", so you need to call it on an instance of the Form1 class, not using the class name itself. You use just the class name for members declared as "static" only. You need to call your function on the actual Form1 object that contains the RichTextBox you want to edit. Something like this:
    Code:
    Form1 myForm = new Form1();
    
    myForm.txtchange("blah blah");
    If you are currently creating an instance of the Form1 class and naming it Form1, I suggest you change the name to something more descriptive. It is considered bad practice to name an object instance with the class name, i.e.
    Code:
    Form1 Form1 = new Form1();
    is bad.

    Also, it is obviously up to you but I'd suggest changing the name of your method. "txtchange" suggests a TextBox. "ChangeText" would be a more appropriate name, but this may also imply that it changes the Text property of the form. "ChangeRichText" would be a more appropriate name still. Given that you are actually appending a line of text, rather than just changing the text, "AppendRichTextLine" would probably be the most appropriate name.
    Last edited by jmcilhinney; Jul 4th, 2005 at 07:22 PM.

  3. #3

    Thread Starter
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: Editing a RichTextBox from another class.

    Thanks a lot. That completely solved the problem.

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