有人可以协助解决此代码中涉及openfile showdialog的错误吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace North_America
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//The GetFileName method gets a filename from the
//user and assignes it to the variable passed as
//an argument.
private void GetFileName(out string selectedFile)
{
if (openFile.ShowDialog() == DialogResult.OK)
{
selectedFile = openFile.FileName;
}
else
{
selectedFile = "";
}
}
//The GetCountries method accpets a filename as an
//argument. It opens the specified file and displays
//its contents in the countriesListBox control.
private void GetCountries (string filename)
{
try
{
//Delcare a variable to hold a country name.
string countryName;
//Declare a StreamReader variable
StreamReader inputFile;
//Open the file to get a streamreader object.
inputFile = File.OpenText(filename);
//Clear anything cueerntly in the listbox.
countriesListBox.Items.Clear();
//Read the file's contents.
while (!inputFile.EndOfStream)
{
//Get a country name.
countryName = inputFile.ReadLine();
//Add the counry name to the ListBox.
countriesListBox.Items.Add(countryName);
}
//Close the file.
inputFile.Close();
}
catch (Exception ex)
{
//Display an error message.
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void countriesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void getCountriesButton_Click(object sender, EventArgs e)
{
string filename; //To hold the filename
//Get the filename from the user.
GetFileName(out filename);
//Get the countries from the file.
GetCountries(filename);
}
private void exitButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
}
}
如何解决此问题?