Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c甚至无法解决剪贴板错误_C#_Winforms_Clipboard - Fatal编程技术网

C# c甚至无法解决剪贴板错误

C# c甚至无法解决剪贴板错误,c#,winforms,clipboard,C#,Winforms,Clipboard,我试图用一段简单的代码从剪贴板上获取信息。我做了很多搜索,但没有一篇帖子解决了我的问题。我已确保使用System.Windows.Forms; 下面是代码。我还缺什么吗 //Rextester.Program.Main是代码的入口点。不要改变它。 //适用于Microsoft R.NET Framework 4.5的编译器版本4.0.30319.17929 using System; using System.Collections.Generic; using System.Linq; usi

我试图用一段简单的代码从剪贴板上获取信息。我做了很多搜索,但没有一篇帖子解决了我的问题。我已确保使用System.Windows.Forms; 下面是代码。我还缺什么吗

//Rextester.Program.Main是代码的入口点。不要改变它。 //适用于Microsoft R.NET Framework 4.5的编译器版本4.0.30319.17929

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Rextester
{
    public class Program
    {



        public static void Main(string[] args)
        {
            Clipboard.SetText(txtCopy.Text);
            txtPaste.Text = Clipboard.GetText();

        }
    }
}
下面是我运行代码时的错误

Error(s):
(22:13) The name 'Clipboard' does not exist in the current context
(22:31) The name 'txtCopy' does not exist in the current context
(23:13) The name 'txtPaste' does not exist in the current context
(23:29) The name 'Clipboard' does not exist in the current context

您已经更改了windows窗体应用程序的Program.main方法,并且没有从中启动任何窗体。这个方法里面应该有这样的东西

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
这是在运行程序时调用的方法,不会初始化任何表单。

您需要在文件开头引用System.Windows.Forms,以解决剪贴板丢失错误。如果您想查看,它的MSDN页面是

您也从未定义txtCopy和txtPaste变量,因此这些变量也显示为缺少的引用

我假设txtCopy和txtPaste是表单中的某种输入?如果这是演员,你没有初始化你的表单,因为你从来没有创建过一个,所以不会生成任何东西。也就是说,即使添加了初始化,也无法从main方法中访问控件,因为对象将超出范围,因为它们包含在Form类本身中

假设您使用的是VS基本模板,您应该按照以下方式构造代码,以实现您的功能

Program.cs:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
表格1.cs

public Form1()
{

    InitializeComponent();

    /* Once you call the InitializeComnents method you will be able to access controls added in design view */
    Clipboard.SetText(txtCopy.Text);
    txtPaste.Text = Clipboard.GetText();
}

记住你的使用语句

这些错误正是他们所说的。这些对象定义在哪里?@DStanley剪贴板位于命名空间中,OP不能向我们显示给出错误的代码。我只知道他的代码中不存在txtCopy和txtPaste。编辑:我还必须添加对表单的引用,并将[StatThread]属性添加到此控制台应用程序,以使OP的代码正常工作。。我认为他试图提出一个最小的问题,但没有做得很好。@Quantic你说得对。我在看WPF剪贴板。我怀疑OP复制并将Winform代码粘贴到控制台应用程序中。