Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Rich text box error...

Rich text box error...

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IamHuM
    wrote on last edited by
    #1

    Hi… In my application…I am trying to add data received on comport in a rich text box. Rich text box is on second form & my SerialPort.datareceived event is on first form. Here is my sample code… form2 f2 = new form2(); Serialport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { ReceivedData = comport.ReadExisting(); f2.Controls[0].Invoke(new EventHandler(delegate{f2.Controls[0].AppendTextReceivedData);})); } But I am getting error on the staement- f2.Controls[0].AppendText(ReceivedData); Error is - ('System.Windows.Forms.Control' does not contain a definition for 'AppendText') I have only one rich textbox control on form2… What is the problem…why I am unable to access "Appendext" method for the rich text box on ‘form2’ from 'form1' Thanks, Vinay

    C I 2 Replies Last reply
    0
    • I IamHuM

      Hi… In my application…I am trying to add data received on comport in a rich text box. Rich text box is on second form & my SerialPort.datareceived event is on first form. Here is my sample code… form2 f2 = new form2(); Serialport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { ReceivedData = comport.ReadExisting(); f2.Controls[0].Invoke(new EventHandler(delegate{f2.Controls[0].AppendTextReceivedData);})); } But I am getting error on the staement- f2.Controls[0].AppendText(ReceivedData); Error is - ('System.Windows.Forms.Control' does not contain a definition for 'AppendText') I have only one rich textbox control on form2… What is the problem…why I am unable to access "Appendext" method for the rich text box on ‘form2’ from 'form1' Thanks, Vinay

      C Offline
      C Offline
      CooperWu
      wrote on last edited by
      #2

      test two point: 1, is the RichTextBox in f2 accessable? use public test it again. 2, f2.Controls[0] is a control. if you can sure it is a RichTextBox, then test these RichTextBox r = f2.Controls[0] as RichTextBox; if (r != null) { // append text here. } hoping this help.

      I 1 Reply Last reply
      0
      • I IamHuM

        Hi… In my application…I am trying to add data received on comport in a rich text box. Rich text box is on second form & my SerialPort.datareceived event is on first form. Here is my sample code… form2 f2 = new form2(); Serialport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { ReceivedData = comport.ReadExisting(); f2.Controls[0].Invoke(new EventHandler(delegate{f2.Controls[0].AppendTextReceivedData);})); } But I am getting error on the staement- f2.Controls[0].AppendText(ReceivedData); Error is - ('System.Windows.Forms.Control' does not contain a definition for 'AppendText') I have only one rich textbox control on form2… What is the problem…why I am unable to access "Appendext" method for the rich text box on ‘form2’ from 'form1' Thanks, Vinay

        I Offline
        I Offline
        IamHuM
        wrote on last edited by
        #3

        Sorry 1 braket was missing... Statement should me like this... f2.Controls[0].Invoke(new EventHandler(delegate{f2.Controls[0].AppendText(ReceivedData);})); & i am getting error on this staement for AppendText method... Thanks, Vinay

        M 1 Reply Last reply
        0
        • C CooperWu

          test two point: 1, is the RichTextBox in f2 accessable? use public test it again. 2, f2.Controls[0] is a control. if you can sure it is a RichTextBox, then test these RichTextBox r = f2.Controls[0] as RichTextBox; if (r != null) { // append text here. } hoping this help.

          I Offline
          I Offline
          IamHuM
          wrote on last edited by
          #4

          I tried with u told... But my richtextbox in f2 is not accessable from f1...Although declared it as public. & RichTextBox's TabIndex is 0... Thanks for your reply, Vinay

          1 Reply Last reply
          0
          • I IamHuM

            Sorry 1 braket was missing... Statement should me like this... f2.Controls[0].Invoke(new EventHandler(delegate{f2.Controls[0].AppendText(ReceivedData);})); & i am getting error on this staement for AppendText method... Thanks, Vinay

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Hi! If you read the error message (that's usually the prerequisite for finding out what's wrong:-D) you'll see that the compiler tells you that Control doesn't have a method AppendText. The Controls collection holds Controls, not RichTextBoxes, so the error message can be explained easily. Use ((RichTextBox)f2.Controls[0]).AppendText(...) to get rid of the compiler error. But keep in mind that the code above is bad style. It's easy to add another control to the form and suddenly your RichTextBox isn't Controls[0] anymore - an InvalidCastException will result. I think it's better to explicitely expose the RichTextBox as a property of f2.

            Regards, mav -- Black holes are the places where God divided by 0...

            I 1 Reply Last reply
            0
            • M mav northwind

              Hi! If you read the error message (that's usually the prerequisite for finding out what's wrong:-D) you'll see that the compiler tells you that Control doesn't have a method AppendText. The Controls collection holds Controls, not RichTextBoxes, so the error message can be explained easily. Use ((RichTextBox)f2.Controls[0]).AppendText(...) to get rid of the compiler error. But keep in mind that the code above is bad style. It's easy to add another control to the form and suddenly your RichTextBox isn't Controls[0] anymore - an InvalidCastException will result. I think it's better to explicitely expose the RichTextBox as a property of f2.

              Regards, mav -- Black holes are the places where God divided by 0...

              I Offline
              I Offline
              IamHuM
              wrote on last edited by
              #6

              Hi... i tried your suggestion ((RichTextBox)f2.Controls[0]).AppendText(...) but its didnt worked. Can u tell me what you want to say exactly with- "explicitely expose the RichTextBox as a property of f2." Thanks, Vinay

              M 1 Reply Last reply
              0
              • I IamHuM

                Hi... i tried your suggestion ((RichTextBox)f2.Controls[0]).AppendText(...) but its didnt worked. Can u tell me what you want to say exactly with- "explicitely expose the RichTextBox as a property of f2." Thanks, Vinay

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #7

                Modify your class form2 to contain

                public RichTextBox MyRichTextBox
                {
                get {
                return richTextBox1;
                }
                }

                and use f2.MyRichTextBox instead of f2.Controls[0]. And by the way "its didnt worked" is not helpful at all - if you don't tell us what the problem/error message is exactly we cannot help you.

                Regards, mav -- Black holes are the places where God divided by 0...

                I 1 Reply Last reply
                0
                • M mav northwind

                  Modify your class form2 to contain

                  public RichTextBox MyRichTextBox
                  {
                  get {
                  return richTextBox1;
                  }
                  }

                  and use f2.MyRichTextBox instead of f2.Controls[0]. And by the way "its didnt worked" is not helpful at all - if you don't tell us what the problem/error message is exactly we cannot help you.

                  Regards, mav -- Black holes are the places where God divided by 0...

                  I Offline
                  I Offline
                  IamHuM
                  wrote on last edited by
                  #8

                  Thanks but...its not working... Here is some more information ...Can u please help me...? /////////////////////////////////////////////////////////////////////////////// /*** This is Main Form code ***/ public SerialPort comport = new SerialPort(); public Form f1; f1 = new ChildForm(); f1.MdiParent = this; comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); private void btn_Senddata_Click(object sender, EventArgs e) { comport.open(); comport.Write(“This is the text to be transmitted”); } public void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { string ReceivedData = comport.ReadExisting(); ReceiveLog(LogMsgType.Incoming, ReceivedData); if(f1.Controls[0].InvokeRequired) { f1.Controls[0].Invoke(new EventHandler(delegate { f1.Controls[0].AppendText(ReceivedData); })); } /*** This is Child Form (f1) code ***/ RichTextBox MyRichTextBox = new RichTextBox();//this is decalred ‘public’ /////////////////////////////////////////////////////////////////////////////// What is the problem,i am not able to understand... Thanks, Vinay

                  M 1 Reply Last reply
                  0
                  • I IamHuM

                    Thanks but...its not working... Here is some more information ...Can u please help me...? /////////////////////////////////////////////////////////////////////////////// /*** This is Main Form code ***/ public SerialPort comport = new SerialPort(); public Form f1; f1 = new ChildForm(); f1.MdiParent = this; comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); private void btn_Senddata_Click(object sender, EventArgs e) { comport.open(); comport.Write(“This is the text to be transmitted”); } public void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { string ReceivedData = comport.ReadExisting(); ReceiveLog(LogMsgType.Incoming, ReceivedData); if(f1.Controls[0].InvokeRequired) { f1.Controls[0].Invoke(new EventHandler(delegate { f1.Controls[0].AppendText(ReceivedData); })); } /*** This is Child Form (f1) code ***/ RichTextBox MyRichTextBox = new RichTextBox();//this is decalred ‘public’ /////////////////////////////////////////////////////////////////////////////// What is the problem,i am not able to understand... Thanks, Vinay

                    M Offline
                    M Offline
                    mav northwind
                    wrote on last edited by
                    #9

                    Once again: Please read the error messages you get and try to understand them. If you don't understand them, post them here so that we can help you! With the code snipplet you posted I think the problem could be that you declared f1 as Form but in fact it's an instance of ChildForm. The class ChildForm does have a public RichTextBox named MyRichTextBox, the class Form doesn't. So to get your code to work you should change public Form f1; to public ChildForm f1; and your DataReceived event handler should read

                    string ReceivedData = comport.ReadExisting();
                    ReceiveLog(LogMsgType.Incoming, ReceivedData);
                    if(f1.MyRichTextBox.InvokeRequired)
                    {
                    this.Invoke(new EventHandler(delegate
                    { f1.MyRichTextBox.AppendText(ReceivedData); }
                    ));
                    } else {
                    f1.MyRichTextBox.AppendText(ReceivedData);
                    }

                    Regards, mav -- Black holes are the places where God divided by 0...

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups