当我第二次打开在运行时创建的表单时,我不断收到错误消息“无法访问已处置的对象”。“对象名称'Form'” 这里是函数的代码。它由 buttonclick 事件调用。我查看了整个网络,发现有类似问题的人,但是我尝试了这些修复,但没有一个有效。不知道如何解决这个问题。VS 高亮显示 frmFavorites.Show();
先感谢您。
public void frmMyBrowser_ShowFavorites(object sender, EventArgs e)
{
frmFavorites.ShowIcon = false;
frmFavorites.ShowInTaskbar = false;
frmFavorites.Text = "Bookmarks";
frmFavorites.Width = 500;
frmFavorites.Height = 320;
frmFavorites.Controls.Add(lstFavorites);
frmFavorites.Controls.Add(btnRemoveFavorite);
frmFavorites.Controls.Add(btnAddFavorite);
frmFavorites.Controls.Add(txtCurrentUrl);
lstFavorites.Width = 484;
lstFavorites.Height = 245;
btnRemoveFavorite.Location = new Point(397, 255);
btnAddFavorite.Location = new Point(8, 255);
txtCurrentUrl.Location = new Point(110, 255);
txtCurrentUrl.Size = new Size(265, 20);
btnAddFavorite.Text = "Add";
btnRemoveFavorite.Text = "Remove";
txtCurrentUrl.Text = wbBrowser.Url.ToString();
btnAddFavorite.Click += new EventHandler(btnAddFavorite_Click);
btnRemoveFavorite.Click += new EventHandler(btnRemoveFavorite_Click);
frmFavorites.Load += new EventHandler(frmFavorites_Load);
frmFavorites.Show();
frmFavorites.FormClosed += new FormClosedEventHandler(frmFavorites_FormClosed);
StreamReader reader = new System.IO.StreamReader(@Application.StartupPath + "\\favorites.txt");
{
while (!reader.EndOfStream)
{
for (int i = 0; i < 4; i++)
{
string strListItem = reader.ReadLine();
if (!String.IsNullOrEmpty(strListItem))
{
lstFavorites.Items.Add(strListItem);
}
}
}
reader.Close();
}
}
public void btnAddFavorite_Click(object sender, EventArgs e)
{
lstFavorites.Items.Add(wbBrowser.Url.ToString());
}
public void btnRemoveFavorite_Click(object sender, EventArgs e)
{
try
{
lstFavorites.Items.RemoveAt(lstFavorites.SelectedIndices[0]);
}
catch
{
MessageBox.Show("You need to select an item", "Error");
}
}
public void frmFavorites_Load(object sender, EventArgs e)
{
}
public void frmFavorites_FormClosed(object sender, FormClosedEventArgs e)
{
StreamWriter writer = new System.IO.StreamWriter(@Application.StartupPath + "\\favorites.txt");
{
for (int i = 0; i < lstFavorites.Items.Count; i++)
{
writer.WriteLine(lstFavorites.Items[i].ToString());
}
writer.Close();
}
frmFavorites.Close();
}