0

リモートコンピューターがオンラインかどうかを確認するpingテストを実装しています。コンピューターのIPを入力するテキストボックスと、押すとすべてのコンピューターにpingを実行してオンラインかどうかを確認するボタンがあります。オンラインまたはオフライン(緑または赤)を反映するように線の色を変更したいと思います。現在のコードでは、失敗するとテキストボックス全体の色が赤に変わります。

私の目標は、コンピューターの1つがpingテストに失敗した場合、他のコンピューターがpingを受信した場合、他のコンピューターは緑のままで、赤で表示されることです。

ありがとう。

private void button_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();
    foreach (var line in txtcomputers.Lines)
    {
        string strhost = line;
        if (strhost.Length > 0)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();
            options.DontFragment = true;
            // Create a buffer of 32 bytes of data to be transmitted.  
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            try
            {
                PingReply reply = pingSender.Send(strhost, timeout, buffer, options);
                if (reply.Status == IPStatus.Success)
                    txtcomputers.ForeColor = Color.Green;
                else
                    txtcomputers.ForeColor = Color.Red;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
4

2 に答える 2

1

Jonが提案したようにRichTextBoxを使用する場合は、、、を使用して、各行の開始文字インデックスを取得する必要がありSelectionStartます。これがあなたのために働くかどうか見てください。SelectionLengthSelectionColorGetFirstCharIndexFromLine

private void button_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();
    Color originalColor = txtcomputers.SelectionColor; ;

    for (int i = 0; i < txtcomputers.Lines.Count(); i++)
    {
        var line = txtcomputers.Lines[i];
        string strhost = line;
        if (strhost.Length > 0)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();
            options.DontFragment = true;
            // Create a buffer of 32 bytes of data to be transmitted.   
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            try
            {
                PingReply reply = pingSender.Send(strhost, timeout, buffer, options);
                txtcomputers.SelectionStart = txtcomputers.GetFirstCharIndexFromLine(i);
                txtcomputers.SelectionLength = strhost.Length;

                if (reply.Status == IPStatus.Success)
                {
                    txtcomputers.SelectionColor = Color.Green;
                }
                else
                {
                    txtcomputers.SelectionColor = Color.Red;
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            txtcomputers.SelectionLength = 0;
        }
    }
    txtcomputers.SelectionColor = originalColor;
}
于 2012-08-19T02:02:44.023 に答える
0

TextBoxに複数の色のテキストを含めることはできないと思います。(少なくともWindowsフォームでは。GUIが使用しているプラ​​ットフォームを指定していません。)

あなたはRichTextBox代わりに見るべきです、それは間違いなくそれを可能にします。

于 2012-08-18T21:18:02.107 に答える