开发者

C# - Keep track of open child forms

I currently have a parent control (Form1) and a child control (Form2).

Form1 contains a listview that stores a list of of file data (each file is a separate item).

Form2 contains only a textbox.

Upon clicking on one of these listviewitems in Form1, Form2 is opened up and accesses the file's data and loads it into the textbox in Form2 in plain text format.

The issue I'm having is, I would like to be able to detect, upon clicking of a listviewitem, whether that file is already opened in said child form and if so, to activate it (bring it to the front) and if it is not already opened, open it. I'm not sure what the best method of doing this would be since this can involve many child forms being open at on开发者_如何学运维ce. This is not an MDI application. Any ideas on how this could be accomplished are appreciated and samples even more so. Thank you.


What I have done in the past is give each new form a unique tag (based on the file you're viewing in this case), so:

var form = new Form2();
form.Tag = (object)"My Unique Object as a Tag"; // Redundant cast I know, but shows Tag is of type object

Then, when going to open up a window for a file, iterate over all the open forms checking tags like so:

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();

And this should only open up one form per file (or tag really)

Hopefully that helps !


You could simply maintain a Dictionary<ListViewItem,Form>. Each time you open a new form add an entry to the dictionary. If the dictionary already contains the ListViewItem that was clicked as a key then you don't need to open a new form.


Assuming form is created for every selected item you could keep track of opened forms in ListViewItem's tag.

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();
  }
}

EDIT: On the other hand why would you create and switch between forms which have only one Edit, it would be much simpler to just fill TextBox with file contents:

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));
  }
}

And if you wamt to read file contents only once, just save file contents in ListViewItem's tag

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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜