Dan*_*sen 0 c# dictionary filedialog winforms
我不确定如何使用我创建的字典,当我点击一个按钮,这意味着我无法从另一个功能中引用它.这可能是非常基本的,但我根本不记得这是怎么做的.
这是打开文件对话框的按钮,然后读取文件中的每一行,并将内容存储在字典中:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Mod Pack Configuration file|*.mcf";
openFileDialog1.Title = "Load Mod Pack Configuration File";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != "")
{
Dictionary<string, string> loadfile =
File.ReadLines(openFileDialog1.FileName)
.Select(line => line.Split(';'))
.ToDictionary(parts => parts[0], parts => parts[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我加载一个函数,将加载的文件,字符串放在表单内的不同控件中.但是下面的代码不起作用,因为找不到"loaddfile":
public void getDefaultSettings()
{
if (Properties.Settings.Default.modsDestDropDown != "")
{
modsDestDropDown.SelectedIndex = Convert.ToInt32(loadfile['modsDestDropDown']);
}
}
Run Code Online (Sandbox Code Playgroud)
我当然可以在button1 click事件中编写函数,但是因为我在其他地方的程序中使用了这个函数,所以稍后会给我一些麻烦
定义你的字典中class level,外面的你的方法是这样的:
Dictionary<string, string> loadfile;
Run Code Online (Sandbox Code Playgroud)
然后在您的方法中初始化它:
loadfile = File.ReadLines(openFileDialog1.FileName)
.Select(line => line.Split(';'))
.ToDictionary(parts => parts[0], parts => parts[1]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71 次 |
| 最近记录: |