富文本框问题 [英] Rich Text Box Problem

查看:79
本文介绍了富文本框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

这是我的代码来说明问题:

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.

推荐答案

您的代码不清楚.似乎没有实现任何显示机制.您只是在将字符串设置为值发生错误:"(且发生"拼写错误,顺便说一句).您是要说富文本框在设置另一个字符串时不会显示其消息吗?

您可能必须调用委托来更新UI,因为这是来自异步操作的事件.这可能会有所帮助:

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx [ ^ ]
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[^]


这是因为SendPing使用pingSender.SendAsync发送ping并立即退出.目前,messageDisplay中没有任何内容.

1)使用SendAsync并在那里更新richtextbox.
2)根据PingReply使用发送和更新Richtextbox.

您的事件仅适用于SendAsync.
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.


这篇关于富文本框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆