每当我尝试运行它时,我都会收到此错误: 非静态字段,方法或属性'System.Windows.Forms.FileDialog.FileName.get'(CS0120)
需要对象引用我对如何修复感到困惑,因为我见过其他论坛,但他们与我没有共同的问题。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration;
namespace Test
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void MainFormLoad(object sender, EventArgs e)
{
ContextMenu cMenu = new ContextMenu();
MenuItem one = new MenuItem("test", new EventHandler(TestClick));
cMenu.MenuItems.Add(one);
Minecraft.ContextMenu = cMenu;
//Minecraft.Image = new Bitmap(System.Configuration);
}
void newClick(object sender, EventArgs e)
{
OpenFileDialog gamedir = new OpenFileDialog();
if (gamedir.ShowDialog() == DialogResult.OK)
{
try
{
}
catch
{
}
}
}
void TestClick(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
Minecraft.Image = new Bitmap(openFile.FileName);
}
}
void Button1Click(object sender, EventArgs e)
{
try
{
Process.Start(@"D:\Games\Minecraft\Minecraft");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Button3Click(object sender, EventArgs e)
{
}
void Button2Click(object sender, EventArgs e)
{
}
void Label1Click(object sender, EventArgs e)
{
}
void Dark_CrusadeClick(object sender, EventArgs e)
{
Process.Start(@"D:\Games\Dark crusade\DarkCrusade.exe");
}
void Add_GameClick(object sender, EventArgs e)
{
int top = 60;
int left = 405;
for(int i = 0; i<1; i++)
{
OpenFileDialog newgame = new OpenFileDialog();
string FilePath = OpenFileDialog.FileName;
if (newgame.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Button button = new Button();
button.Top = top;
button.Left = left;
button.Height = 265;
button.Width = 189;
this.Controls.Add(button);
top += button.Height + 2;
}
}
}
}
}
答案 0 :(得分:0)
OpenFileDialog newgame = new OpenFileDialog();
string FilePath = OpenFileDialog.FileName;
访问OpenFileDialog
时,您在静态上下文中引用FileName
;您需要使用newgame
OpenFileDialog
实例才能检索FileName属性:
OpenFileDialog newgame = new OpenFileDialog();
string FilePath = newgame.FileName;