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中直接读取文件?
发布于 2016-04-13 06:01:52
您想要的是您的几何对象可以在OpenPolyFile()以外的其他作用域中访问。因此,您可以简单地使两个方法都可以访问几何图形声明,例如,在后面的表单代码中声明它。
// 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
}
}发布于 2016-04-13 06:12:01
如何读取另一个按钮中已打开的文件,直接单击事件。(即,在单击按钮时没有打开文件对话框)
我不太清楚您为什么要读两遍它,但是如果这个要求是这样的话,让选定的filename在全球范围内可用并使用它。
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中可用的文件名,您可以使用该文件并再次读取。
private void button1_Click(object sender, EventArgs e)
{
// now you can read the file.
//File.ReadAllText(filename); //OR
TriangleNet.IO.FileReader.ReadPolyFile(file);
}https://stackoverflow.com/questions/36589725
复制相似问题