Click here to Skip to main content
14,743,331 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hi Friends,

Here is my code to illustrate the problem:

namespace NetworkTool.Ping
{
  public partial class Network_Tool_Form : Form
  {
    public Network_Tool_Form()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {}

     string iP = "192.168.1.1";


    private void pingButton_Click(object sender, EventArgs e)
    {
      pingsSent = 0;
      pingResultRichTextBox.Clear();
      pingResultrichTextBox.Text += "Pinging " + iP + " with 32 bytes of data:\r\n\r\n";
      
       //HERE THE MESSAGE DISPLAYED ON RICHTEXTBOX CORRECTLY.  :-\ 
      SendPing();
      //IT GIVES ME "", ALTHOUGH IT WAS THE SAME PREVIOUS MESSAGE. :confused: 
      pingResultrichTextBox.Text += messageDisplay;
    }

     private int pingsSent;
     AutoResetEvent resetEvent = new AutoResetEvent(false);
     private string messageDisplay = "";

        private void SendPing()
        {
            Ping pingSender = new Ping();              
            pingSender.PingCompleted += new PingCompletedEventHandler (pingSender_Complete);
            byte[] packetData = Encoding.ASCII.GetBytes("................................");
            PingOptions packetOptions = new PingOptions(64, true);
            pingSender.SendAsync(iP, 12000, packetData, packetOptions, pingObject.resetEvent);
        }

        private void pingSender_Complete(object sender, PingCompletedEventArgs e)
        {
          if (e.Error != null)
          {
            // AT THIS point MY RICHTEXTBOX DISPLAY THE MESSAGE.  :-\ 
            pingResultrichTextBox.Text += "An error occured: ";

            //BUT IF I PASSED THE MESSAGE TO THE STRING VARIABLE AND TRY TO DISPLAY THE STRING VARIABLE IT DOSEN"T GOT THE MESSAGE.  :doh:              
            messageDisplay = "An error occured: ";
            ((AutoResetEvent)e.UserState).Set();

          }
        }
  }
}




[orig title]
I face a problem for displaying a text on RICH TEXT BOX.
Posted
Updated 18-Jan-11 0:45am
v5

Your code is unclear. There doesn't appear to be any display mechanism implemented. You're merely setting a string to the value "An error occured: " (and "occurred" is spelled wrong, BTW). Are you trying to say that the rich text box doesn't display its message when you set another string?

You may have to invoke a delegate to update the UI since this is an event from an async operation. This might help:

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx[^]
   
Comments
Moustafa Safwat 18-Jan-11 5:56am
   
Actually, I'm Sorry. I didn't post all of my code to make it easy for read. but below is the code. [DELETED CODE BLOCK]
#realJSOP 18-Jan-11 7:36am
   
I DELETED THE CODE BLOCK BECAUSE IT WAS TOO LONG. Did you even look at/try the link I gave you
Moustafa Safwat 20-Jan-11 12:28pm
   
First great thanks for you.
As I told you before the block is too long, so I posted the important part of it in the first time :).
The link also was extremely useful.
It is because SendPing sends ping using pingSender.SendAsync and exits immediately. At this time there is nothing in messageDisplay.

1) Use SendAsync and update richtextbox there itself.
2) Use Send and update richtextbox depending on PingReply.

Your event will work only with SendAsync.
System.Net.NetworkInformation.PingCompletedEventHandler PingCompleted
Occurs when an asynchronous operation to send an Internet Control Message Protocol (ICMP) echo message and receive the corresponding ICMP echo reply message completes or is canceled.
   
v2
Comments
Moustafa Safwat 18-Jan-11 6:00am
   
Sounds Great. I agree with you that this may be the cause of the problem but really I tried pingSender.Send as "commented on John Simmons / outlaw reply" but it doesn't work.
could you please check it, and tell me where is the mistake that may I did.

Thank you for helping :).
Moustafa Safwat 18-Jan-11 6:15am
   
You are smart :). I got your point, but it didn't work becuase it still has the intiall variable value "".
I know that I got alot of your time, but actually I apprtiate that alot. I have anther question to work around this "how could to set property richtextbox.text from anther class?"
Prerak Patel 18-Jan-11 7:22am
   
My mistake. I should have checked the event first. There's no use if you use Send. Updated the answer. May be it helps now.
Moustafa Safwat 20-Jan-11 12:17pm
   
Sorry my brother for late on reply. But My ISP in my country had a problem with their internet infrastructure :s.

I did a work around and it worked; I'll list it below to be helpful for others in similar cases:
First: I added a Property to the Form.Designer class to set and get richTextBox.Text value.
Second: I added public static instance with type Designer class.
Then: I added object with type Designer class.
Then: I passed
Moustafa Safwat 20-Jan-11 12:23pm
   
...object reference value to the instance.
Then: I passed Application.Run(reference) "that has Main() method";
Then: I cerated object with type Designer class on the ping class and assign it instance that created on Main() class;
Finally: I used the property of the object to add the .text on richtextbox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900