首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Function buttonCreate_Click中使用Function MenuItem

如何在Function buttonCreate_Click中使用Function MenuItem
EN

Stack Overflow用户
提问于 2012-06-20 18:41:34
回答 1查看 71关注 0票数 0

我是C#的新手。我的问题是:我已经创建了一个MenuStrip。我想用ButtonCreate_Click在目录路径下创建一个文件夹。那么如何在函数buttonCreate中使用路径呢?这有可能吗?

代码语言:javascript
运行
复制
    private void buttonCreate_Click(object sender, EventArgs e)
    {

        string MyFileName = "Car.txt";

        string newPath1 = Path.Combine(patheto, MyFileName);
        //Create a folder in the active Directory
        Directory.CreateDirectory(newPath1);
    }

    private void DirectoryPathToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string patheto = @"C:\temp";
        Process.Start(patheto);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-20 18:46:14

因为您在菜单项单击事件中声明了patheto,所以它只是该作用域的一个局部变量。如果为窗体创建属性,则可以在窗体范围内使用该属性。如下所示:

代码语言:javascript
运行
复制
private string patheto = @"C:\temp";

private void buttonCreate_Click(object sender, EventArgs e)
{

    string MyFileName = "Car.txt";

    string newPath1 = Path.Combine(patheto, MyFileName);
    // Create a folder in the active Directory
    Directory.CreateDirectory(newPath1);
}

private void DirectoryPathToolStripMenuItem_Click(object sender, EventArgs e)
{
    Process.Start(patheto);
}

这意味着您的变量patheto可以在表单中的任何位置访问。您必须记住的是,无论您在何处声明变量,它们都只能在该函数/类或子函数/方法中访问。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11117839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档