winform显示历史信息的菜单
public Form1()
{
Form1_Load();
InitializeComponent();
}
private void Form1_Load()
{
StreamReader sr = new StreamReader(path + "\\history.ini");
int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2;
while (sr.Peek() >= 0)//读取INI文件
{
ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine());
this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuitem);
i++;
menuitem.Click += new EventHandler(打开ToolStripMenuItem_Click);
}
sr.Close();
}
//将打开文件路径写入ini文件
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
StreamWriter s = new StreamWriter(path + "\\history.ini",true, Encoding.GetEncoding("gb2312"));
s.WriteLine(openFileDialog1.FileName +"\n\t");
s.Flush();
s.Close();
ShowWindows(openFileDialog1.FileName);
}
//作为显示背景
public void ShowWindows(string fileName)
{
Image p = Image.FromFile(fileName);
Form f = new Form();
f.MdiParent = this;
f.BackgroundImage = p;
f.Show();
}
红色字体显示的内容,在运行的时候总是报错“未将对象引用设置到对象的实例” --------------------编程问答-------------------- this.文件ToolStripMenuItem.DropDownItems是null了吧 --------------------编程问答-------------------- --------------------编程问答-------------------- this.文件ToolStripMenuItem.DropDownItems没有子菜单项时这样用就会出现你这样的错误 --------------------编程问答--------------------
是啊,可不是空值 --------------------编程问答--------------------
可是我在文件下拉菜单下面有三个子菜单啊:打开、关闭所有、退出 --------------------编程问答--------------------
那你是动态添加的吗?那必须添加完毕之后用这句话。反正必须要有子菜单写, --------------------编程问答--------------------
我是手工加上的子菜单,是不是属性设置有问题,我再仔细看一下
补充:.NET技术 , C#