Rich text box error...
-
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
-
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
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. -
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
-
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. -
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
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 methodAppendText
. TheControls
collection holdsControl
s, notRichTextBox
es, 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 yourRichTextBox
isn'tControls[0]
anymore - an InvalidCastException will result. I think it's better to explicitely expose theRichTextBox
as a property of f2.Regards, mav -- Black holes are the places where God divided by 0...
-
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 methodAppendText
. TheControls
collection holdsControl
s, notRichTextBox
es, 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 yourRichTextBox
isn'tControls[0]
anymore - an InvalidCastException will result. I think it's better to explicitely expose theRichTextBox
as a property of f2.Regards, mav -- Black holes are the places where God divided by 0...
-
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
Modify your class form2 to contain
public RichTextBox MyRichTextBox
{
get {
return richTextBox1;
}
}and use
f2.MyRichTextBox
instead off2.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...
-
Modify your class form2 to contain
public RichTextBox MyRichTextBox
{
get {
return richTextBox1;
}
}and use
f2.MyRichTextBox
instead off2.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...
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 -
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, VinayOnce 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
asForm
but in fact it's an instance ofChildForm
. The classChildForm
does have a publicRichTextBox
namedMyRichTextBox
, the classForm
doesn't. So to get your code to work you should changepublic Form f1;
topublic ChildForm f1;
and your DataReceived event handler should readstring 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...