首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取另一个按钮中已打开的文件,直接单击事件。(即,在单击按钮时没有打开文件对话框)

如何读取另一个按钮中已打开的文件,直接单击事件。(即,在单击按钮时没有打开文件对话框)
EN

Stack Overflow用户
提问于 2016-04-13 05:55:22
回答 2查看 41关注 0票数 0
代码语言:javascript
复制
        void OpenWithDialog()
        {
            var ofd = new OpenFileDialog();

            ofd.Filter = "Triangle polygon file|*.poly";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                OpenPolyFile(ofd.FileName);
            }
        }

        void OpenPolyFile(string file)
        {
            var geometry = TriangleNet.IO.FileReader.ReadPolyFile(file);
            // ...
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

如何在button1中直接读取文件?

EN

回答 2

Stack Overflow用户

发布于 2016-04-13 06:01:52

您想要的是您的几何对象可以在OpenPolyFile()以外的其他作用域中访问。因此,您可以简单地使两个方法都可以访问几何图形声明,例如,在后面的表单代码中声明它。

代码语言:javascript
复制
// class scoped variables
       [ThePolyFileType] (pick the right one here :) geometry = null;

        void OpenWithDialog()
        {
            var ofd = new OpenFileDialog();

            ofd.Filter = "Triangle polygon file|*.poly";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                OpenPolyFile(ofd.FileName);
            }
        }

        void OpenPolyFile(string file)
        {
            geometry = TriangleNet.IO.FileReader.ReadPolyFile(file);
            // ...
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (geometry != null)
            {
              //do your stuff
            }
        }
票数 0
EN

Stack Overflow用户

发布于 2016-04-13 06:12:01

如何读取另一个按钮中已打开的文件,直接单击事件。(即,在单击按钮时没有打开文件对话框)

我不太清楚您为什么要读两遍它,但是如果这个要求是这样的话,让选定的filename在全球范围内可用并使用它。

代码语言:javascript
复制
private string filename;

void OpenWithDialog()
{
    var ofd = new OpenFileDialog();

    ofd.Filter = "Triangle polygon file|*.poly";

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        OpenPolyFile(ofd.FileName);
        filename = ofd.FileName
    }
}

现在您已经打开了button_clcik中可用的文件名,您可以使用该文件并再次读取。

代码语言:javascript
复制
private void button1_Click(object sender, EventArgs e)
{
    // now you can read the file.
    //File.ReadAllText(filename); //OR

     TriangleNet.IO.FileReader.ReadPolyFile(file); 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36589725

复制
相关文章

相似问题

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