Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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_Button_Methods - Fatal编程技术网

C# 在c语言中访问两个按钮之间的数据#

C# 在c语言中访问两个按钮之间的数据#,c#,winforms,button,methods,C#,Winforms,Button,Methods,我的问题已经在标题中了。让我们澄清一下,基本上我制作了一个简单的程序。在这个程序中有两个按钮和一个文本框,一个用于选择文件夹,另一个用于查看文本框。这是我的密码: private void button1_Click(object sender, EventArgs e) { var filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder"); } private v

我的问题已经在标题中了。让我们澄清一下,基本上我制作了一个简单的程序。在这个程序中有两个按钮和一个文本框,一个用于选择文件夹,另一个用于查看文本框。这是我的密码:

private void button1_Click(object sender, EventArgs e)
        {
            var filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");

        }
private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Lines = filenames

        }

但是文件名用红色下划线。这意味着我无法从按钮2访问“filenames”元素。有人能告诉我如何访问它吗。如评论中所述,谢谢,应该是这样的:

string[] filenames;

private void button1_Click(object sender, EventArgs e)
{
    filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");
}

private void button2_Click(object sender, EventArgs e)
{
    textBox1.Lines = filenames
}
作为字符串数组的文件名:。

也许阅读有关范围的内容是好的。

如评论中所述,应该是这样的:

string[] filenames;

private void button1_Click(object sender, EventArgs e)
{
    filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");
}

private void button2_Click(object sender, EventArgs e)
{
    textBox1.Lines = filenames
}
作为字符串数组的文件名:。

也许阅读有关作用域的内容会更好。

您已经在button1 click事件处理程序方法中声明并初始化了filenames变量。因此,它只能在该方法(范围)内访问。如果还需要从外部访问它,则需要在类级别范围内声明变量(在本例中为文件名)

string[] filenames;

public Form1()
{
    InitializeComponent();            
}

private void button1_Click(object sender, EventArgs e)
{
    //var filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");
    filenames = Directory.GetFiles(@"C:/Users/Example/Desktop/folder");
}

private void button2_Click(object sender, EventArgs e)
{
    textBox1.Lines = filenames;
}
推荐读数…


您已经在button1 click事件处理程序方法中声明并初始化了FileName变量。因此,它只能在该方法(范围)内访问。如果还需要从外部访问它,则需要在类级别范围内声明变量(在本例中为文件名)

string[] filenames;

public Form1()
{
    InitializeComponent();            
}

private void button1_Click(object sender, EventArgs e)
{
    //var filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");
    filenames = Directory.GetFiles(@"C:/Users/Example/Desktop/folder");
}

private void button2_Click(object sender, EventArgs e)
{
    textBox1.Lines = filenames;
}
推荐读数…


文件名数组应首先声明为全局数组,以便在注释中所述的两个按钮的内部单击事件中使用。此外,我们不能全局声明类型为var的变量

string[] filenames;

private void button1_Click(object sender, EventArgs e)
{
   filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");
}

private void button2_Click(object sender, EventArgs e)
{
   textBox1.Lines = filenames
}

文件名数组应首先全局声明,以使用注释中所述的两个按钮的内部单击事件。此外,我们不能全局声明类型为var的变量

string[] filenames;

private void button1_Click(object sender, EventArgs e)
{
   filenames = Directory.GetFiles("C:/Users/Example/Desktop/folder");
}

private void button2_Click(object sender, EventArgs e)
{
   textBox1.Lines = filenames
}

无法从其他方法访问局部变量。但是您可以在第一个方法之外将其声明为字段(
string[]filenames
),然后在第一个方法中分配给它。您需要在第一个按钮单击事件处理程序的范围之外定义
filenames
。您可能需要阅读一些关于无法从其他方法访问局部变量的基础知识。但是您可以在第一个方法之外将其声明为字段(
string[]filenames
),然后在第一个方法中分配给它。您需要在第一个按钮单击事件处理程序的范围之外定义
filenames
。你可能想读一些基础知识,这会给你一个错误。不能使用var声明对象。只能分配它们。让我来修。@Doruk啊,是的!我不知道它的心,所以我包括链接。谢谢你的修正,这会给你一个错误。不能使用var声明对象。只能分配它们。让我来修。@Doruk啊,是的!我不知道它的心,所以我包括链接。我想这个答案需要一些解释。我想这个答案需要一些解释