C# C-跟踪打开的子窗体

C# C-跟踪打开的子窗体,c#,winforms,C#,Winforms,我目前有一个父控件Form1和一个子控件Form2 Form1包含一个listview,它存储文件数据列表,每个文件都是一个单独的项 Form2只包含一个文本框 单击Form1中的其中一个listviewitems后,Form2将打开并访问文件的数据,并以纯文本格式将其加载到Form2中的文本框中 我遇到的问题是,我希望能够在单击listviewitem时检测该文件是否已在所述子表单中打开,如果已打开,则将其激活,并将其放在前面,如果尚未打开,则将其打开。我不确定这样做的最佳方法是什么,因为这可

我目前有一个父控件Form1和一个子控件Form2

Form1包含一个listview,它存储文件数据列表,每个文件都是一个单独的项

Form2只包含一个文本框

单击Form1中的其中一个listviewitems后,Form2将打开并访问文件的数据,并以纯文本格式将其加载到Form2中的文本框中


我遇到的问题是,我希望能够在单击listviewitem时检测该文件是否已在所述子表单中打开,如果已打开,则将其激活,并将其放在前面,如果尚未打开,则将其打开。我不确定这样做的最佳方法是什么,因为这可能涉及同时打开许多子窗体。这不是MDI应用程序。任何关于如何实现这一点的想法都将受到赞赏,更是如此。谢谢。

我过去所做的是根据您在本例中查看的文件为每个新表单提供一个唯一的标记,因此:

var form = new Form2();
form.Tag = (object)"My Unique Object as a Tag"; // Redundant cast I know, but shows Tag is of type object
然后,在打开文件窗口时,遍历所有打开的表单检查标记,如下所示:

foreach(var f in Application.OpenForms)
{
   if(f.Tag == tagForFile)
   {
       f.BringToFront();
       return;
   }
}
// Couldn't find one, so open on
var form = new Form2();
form.Tag = tagForFile;
form.Show();
而且每个文件或标签只打开一个表单


希望这有帮助

我过去所做的是根据您在本例中查看的文件为每个新表单提供一个唯一的标记,因此:

var form = new Form2();
form.Tag = (object)"My Unique Object as a Tag"; // Redundant cast I know, but shows Tag is of type object
然后,在打开文件窗口时,遍历所有打开的表单检查标记,如下所示:

foreach(var f in Application.OpenForms)
{
   if(f.Tag == tagForFile)
   {
       f.BringToFront();
       return;
   }
}
// Couldn't find one, so open on
var form = new Form2();
form.Tag = tagForFile;
form.Show();
而且每个文件或标签只打开一个表单


希望这有帮助

你可以简单地维护一本字典。每次打开新表单时,都会向词典中添加一个条目。如果词典已包含作为键单击的ListViewItem,则无需打开新表单。

只需维护词典即可。每次打开新表单时,都会向词典中添加一个条目。如果字典已包含作为键单击的ListViewItem,则无需打开新表单。

假设为每个选定项创建表单,则可以在ListViewItem的标记中跟踪打开的表单

lv.ItemSelectionChanged += lv_ItemSelectionChanged;

private void lv_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
{
  if(e.IsSelected)
  {
    if(e.Item.Tag == null)
    {
      var form = new Form2();
      // init Form2 here
      form.Parent = this.panel1;
      e.Item.Tag = form;
    }
    (e.Item as Form2).BringToFront();
  }
}
编辑: 另一方面,为什么要在只有一次编辑的表单之间创建和切换,只需用文件内容填充文本框就会简单得多:

ListView1.ItemActivate += ListView1_ItemActivate;

private void ListView1_ItemActivate(Object sender, EventArgs e)
{
  if(ListView1.SelectedItems.Count > 0)
  {
    this.form2Instance.ContentsTextBox.Text = File.ReadAllText(this.rootFilesPath + @"\" + ListView1.SelectedItems.Last().Text));
  }
}
如果您想只读取一次文件内容,只需将文件内容保存在ListViewItem的标记中

ListView1.ItemActivate += ListView1_ItemActivate;
private void ListView1_ItemActivate(Object sender, EventArgs e)
{
  if(ListView1.SelectedItems.Count > 0)
  {
    var item = ListView1.SelectedItems.Last();
    if(item.Tag == null)
      item.Tag = File.ReadAllText(this.rootFilesPath + @"\" + item.Text);
    this.form2Instance.ContentsTextBox.Text = (string) item.Tag;
  }
}

假设为每个选定项创建了表单,则可以在ListViewItem的标记中跟踪打开的表单

lv.ItemSelectionChanged += lv_ItemSelectionChanged;

private void lv_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
{
  if(e.IsSelected)
  {
    if(e.Item.Tag == null)
    {
      var form = new Form2();
      // init Form2 here
      form.Parent = this.panel1;
      e.Item.Tag = form;
    }
    (e.Item as Form2).BringToFront();
  }
}
编辑: 另一方面,为什么要在只有一次编辑的表单之间创建和切换,只需用文件内容填充文本框就会简单得多:

ListView1.ItemActivate += ListView1_ItemActivate;

private void ListView1_ItemActivate(Object sender, EventArgs e)
{
  if(ListView1.SelectedItems.Count > 0)
  {
    this.form2Instance.ContentsTextBox.Text = File.ReadAllText(this.rootFilesPath + @"\" + ListView1.SelectedItems.Last().Text));
  }
}
如果您想只读取一次文件内容,只需将文件内容保存在ListViewItem的标记中

ListView1.ItemActivate += ListView1_ItemActivate;
private void ListView1_ItemActivate(Object sender, EventArgs e)
{
  if(ListView1.SelectedItems.Count > 0)
  {
    var item = ListView1.SelectedItems.Last();
    if(item.Tag == null)
      item.Tag = File.ReadAllText(this.rootFilesPath + @"\" + item.Text);
    this.form2Instance.ContentsTextBox.Text = (string) item.Tag;
  }
}