【发布时间】:2016-05-13 10:03:56
【问题描述】:
每当我尝试运行它时,我都会收到此错误: 非静态字段、方法或属性“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;
}
}
}
}
}
【问题讨论】:
-
提供minimal reproducible example 是寻找解决方案的非常好的一步(或者至少是一个好的问题)。包含一大堆空方法的代码墙不是最小示例。
标签: c#