/// <summary> /// Creates a nation and adds it to the list of nations. /// </summary> private void CreateNation() { var nationDialog = new NationDialog(); if (nationDialog.ShowDialog() == DialogResult.OK) { var nation = new Nation(nationDialog.Nation, nationDialog.Password); this.AddNation(nation); } }
/// <summary> /// Imports a list of puppets from a text file. /// </summary> private void ImportFile() { if (importOpenFileDialog.ShowDialog() == DialogResult.OK) { FileStream fileStream = null; StreamReader streamReader = null; try { fileStream = new FileStream(importOpenFileDialog.FileName, FileMode.Open); streamReader = new StreamReader(fileStream); while (!streamReader.EndOfStream) { var line = streamReader.ReadLine(); var values = line.Split(','); var nation = new Nation(values[0], values[1]); this.AddNation(nation); } streamReader.Close(); } catch (Exception) { MessageBox.Show("An error occurred while opening or parsing data from the file. Do you have sufficient permissions to access the file?", "NationStates Nation Manager"); } finally { if (streamReader != null) { streamReader.Close(); } } } }
/// <summary> /// Adds a nation to the list of nations. /// </summary> /// <param name="nation">The <see cref="Nation"/> object representing the nation to be added.</param> private void AddNation(Nation nation) { this.Nations.Add(nation); var nationItem = new ListViewItem(nation.Name); nationItem.Tag = nation; nationItem.ImageIndex = (int)ListViewItemIcon.Pending; nationItem.SubItems.Add(new ListViewItem.ListViewSubItem(nationItem, "?")); nationItem.SubItems.Add(new ListViewItem.ListViewSubItem(nationItem, "?")); nationItem.SubItems.Add(new ListViewItem.ListViewSubItem(nationItem, "Attempting to retrieve this nation's information...")); listView.Items.Add(nationItem); for (var i = this.Workers.Count - 1; i >= 0; i--) { if (this.Workers[i].Item1 == nationItem) { this.Workers[i].Item2.CancelAsync(); this.Workers.RemoveAt(i); } } BackgroundWorker worker = new BackgroundWorker(); worker.WorkerSupportsCancellation = true; worker.DoWork += this.RetrieveStatus; worker.RunWorkerCompleted += this.CompleteListViewItemUpdate; this.Workers.Add(new Tuple<ListViewItem, BackgroundWorker>(nationItem, worker)); worker.RunWorkerAsync(nationItem); }