C#Visual Studio Windows窗体错误CS1501

时间:2017-07-02 18:02:54

标签: c# winforms visual-studio-2015

我正在尝试制作一个pc诊断工具,我在命令提示符下这样做,我试图在Windows窗体中这样做我正在使用的IDE是Visual Studio 2015对不起如果问题很明显或无关紧要我是C#的新手我环顾四周,但找不到任何有用的方法来处理这个错误 -thanks Mahmood

以下是工作命令提示符代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;

namespace mahmoodspcdiagnostictool_m.p.d.t_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("To begin diagnostic type start diagnostic");
            string startdiagnostic = Console.ReadLine();
            if (startdiagnostic == "start diagnostic")
            {
                PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

                PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available MBytes");

                Console.WriteLine("Cpu Usage: Avalible RAM:");
                while (true)
                {
                    Thread.Sleep(2500);
                    Console.Write("{0}%", perfCpuCount.NextValue());
                    Console.WriteLine("     {0} MB", perfMemCount.NextValue());
                }
            }

        }
    }
}

以下是无效的Windows窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace MahmoodsPCDiagnosticTool_M.P.D.T__GUI_Edition_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

        PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available MBytes");

        //When the start button is clicked this code is to be executed
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox4.AppendText("{0}%\r\n", perfCpuCount.NextValue());
            richTextBox5.AppendText("{0} MB\r\n", perfMemCount.NextValue());
        }

       private void Form1_Enter(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This is a pc diagnostic tool it was made by Mahmood Badr.");
        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox4_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的示例显示您使用的是richTextBox,它没有AppendText(string, string)方法,只有AppendText(string)方法。

这有效:

PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available MBytes");

//When the start button is clicked this code is to be executed
private void button1_Click(object sender, EventArgs e)
{
    richTextBox4.AppendText($"{perfCpuCount.NextValue()}%{Environment.NewLine}");
    richTextBox5.AppendText($"{perfMemCount.NextValue()} MB{Environment.NewLine}");
}

*与C#7.0