Hi everyone,
I am still new in C# and have created a project as part of my learning process. I have a parent form and 2 child forms (intending to add a couple more child form as I go on). In the parent form I have a serialPort configured, in child form1 I have a RichTextBox,
and in child form2 I have a grouping of RadioButtons. What I am trying to do is to log the activities from the Serial Port (in the Parent Form) and the clickEvents in the RadioButtons (Child Form2) into the RichTextBox which is in child Form1. With regards
to Serial Port activity, it is working and I can update the RichTextBox contents evrytime the SerialPort receive something. Can someone give me a suggestions how to monitor the child form2 activity and have it log in child Form1 and also sent it out into the
SerialPort in the parent form. I try it by passing the parameters from child from2 (radioButton_Click_Events) to child form1 but there is no update on the contents of the RichTextBox, and there is no error thrown. In the same condition when I send a data
into serialPort, I got the update on the RTB in child Form1. I have included a skeleton part of my code below:
IN THE PARENT FORM:
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
Form1 Log = new Form1();
private delegate void SerialDataReceivedHandler(object sender, SerialDataReceivedEventArgs e);
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (this.InvokeRequired)
{
SerialDataReceivedHandler SD = new SerialDataReceivedHandler(this.serialPort_DataReceived);
this.Invoke(SD = new object[] {sender, e} );
return;
}
ReceivedData(); // This is the method that process the data from the recived buffer.
Log.DisplayMessage(myMsg);
<font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
}
</font></font></font><font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
</font></font><font face="Consolas" size="2" color="#0000ff
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small IN CHILD FORM1:
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
public void DisplayMessage(string _displayMsg)
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
{ <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
</font></font></font><font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
</font></font><font face="Consolas" size="2" color="#0000ff
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="color:#0000ff RichTextBox1.SelectedText = string.Empty;
<span style="color:#0000ff RichTextBox1.AppendText(_displayMsg + "n");
<span style="color:#0000ff RichTextBox1.ScrollToCaret();
<span style="color:#0000ff }
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small IN CHILD FORM2:
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="color:#0000ff <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small Form1 Log2 = new Form1();
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small private<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small void<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
RadioButton1_CheckedChanged( <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small object<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
sender, <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small EventArgs<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
e )<font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
{
if (RadioButton1.Checked == true)
{
Log2.DisplayMessage("BUTTON ON");
}
else
<font face="Consolas" size="2 <font face="Consolas" size="2 <font color="#0000ff
Log2.DisplayMessage("BUTTON OFF");
</font></font></font><font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="color:#0000ff }
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
I have no problem displaying the messge coming from Parent Form, but the message coming from Child Form2 does not display in the RichTextBox. I already step through it and verified the message is pass on, but when I display the Child Form1, no message is
added to the existing log and no error thrown. I was expecting a cross thread violation but I did not get one. Please give me some advise what to do. Can someone also verify to me if the Invoke is correctly done or positioned properly? Any help will be
very much appreciated.
<font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
View the full article
I am still new in C# and have created a project as part of my learning process. I have a parent form and 2 child forms (intending to add a couple more child form as I go on). In the parent form I have a serialPort configured, in child form1 I have a RichTextBox,
and in child form2 I have a grouping of RadioButtons. What I am trying to do is to log the activities from the Serial Port (in the Parent Form) and the clickEvents in the RadioButtons (Child Form2) into the RichTextBox which is in child Form1. With regards
to Serial Port activity, it is working and I can update the RichTextBox contents evrytime the SerialPort receive something. Can someone give me a suggestions how to monitor the child form2 activity and have it log in child Form1 and also sent it out into the
SerialPort in the parent form. I try it by passing the parameters from child from2 (radioButton_Click_Events) to child form1 but there is no update on the contents of the RichTextBox, and there is no error thrown. In the same condition when I send a data
into serialPort, I got the update on the RTB in child Form1. I have included a skeleton part of my code below:
IN THE PARENT FORM:
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
Form1 Log = new Form1();
private delegate void SerialDataReceivedHandler(object sender, SerialDataReceivedEventArgs e);
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (this.InvokeRequired)
{
SerialDataReceivedHandler SD = new SerialDataReceivedHandler(this.serialPort_DataReceived);
this.Invoke(SD = new object[] {sender, e} );
return;
}
ReceivedData(); // This is the method that process the data from the recived buffer.
Log.DisplayMessage(myMsg);
<font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
}
</font></font></font><font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
</font></font><font face="Consolas" size="2" color="#0000ff
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small IN CHILD FORM1:
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
public void DisplayMessage(string _displayMsg)
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
{ <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
</font></font></font><font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff
</font></font><font face="Consolas" size="2" color="#0000ff
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="color:#0000ff RichTextBox1.SelectedText = string.Empty;
<span style="color:#0000ff RichTextBox1.AppendText(_displayMsg + "n");
<span style="color:#0000ff RichTextBox1.ScrollToCaret();
<span style="color:#0000ff }
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small IN CHILD FORM2:
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="color:#0000ff <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small Form1 Log2 = new Form1();
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small private<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small void<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
RadioButton1_CheckedChanged( <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small object<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
sender, <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small EventArgs<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
e )<font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
{
if (RadioButton1.Checked == true)
{
Log2.DisplayMessage("BUTTON ON");
}
else
<font face="Consolas" size="2 <font face="Consolas" size="2 <font color="#0000ff
Log2.DisplayMessage("BUTTON OFF");
</font></font></font><font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="color:#0000ff }
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
I have no problem displaying the messge coming from Parent Form, but the message coming from Child Form2 does not display in the RichTextBox. I already step through it and verified the message is pass on, but when I display the Child Form1, no message is
added to the existing log and no error thrown. I was expecting a cross thread violation but I did not get one. Please give me some advise what to do. Can someone also verify to me if the Invoke is correctly done or positioned properly? Any help will be
very much appreciated.
<font face="Consolas" size="2 <font face="Consolas" size="2
</font></font><font face="Consolas" size="2
</font>
View the full article