/// <summary> /// Creates a default PreferenceList. /// </summary> /// <param name="position"></param> /// <param name="tag"></param> /// <returns></returns> private Panel CreatePreferenceList(Position position, PreferenceList tag) { // List containing the items ListBox list = new ListBox(); list.TabIndex = position.NextTabIndex; list.Name = "preferenceList" + position.TabIndex.ToString(); //list.Size = new Size(position.Width - position.Margin, position.LineHeight * 4); list.Size = new Size(position.Width, position.LineHeight * 4); list.Location = new Point(0, 0); foreach (string item in tag.SortedItems) { list.Items.Add(item); } // Button to move items up Button up = new Button(); up.TabIndex = position.NextTabIndex; up.Name = "preferenceListUp" + position.TabIndex.ToString(); up.Location = new Point(position.Margin, list.Height); up.Size = new Size((int)(list.Width / 2 - position.Margin * 1.5), position.LineHeight); up.Text = "+"; up.Enabled = false; // Button to move items down Button down = new Button(); down.TabIndex = position.NextTabIndex; down.Name = "preferenceListDown" + position.TabIndex.ToString(); down.Location = new Point(up.Location.X + up.Size.Width + position.Margin, up.Location.Y); down.Size = up.Size; down.Text = "-"; down.Enabled = false; // Add events list.SelectedIndexChanged += _listboxSelectionChanged; up.Click += _buttonClicked; down.Click += _buttonClicked; // Create PreferenceListDetails PreferenceListDetails details = new PreferenceListDetails(tag, list, up, down); // Set tags list.Tag = details; up.Tag = details; down.Tag = details; // Create and return panel Panel panel = details.GetAsPanel(new Size(list.Width, position.LineHeight * 5), new Point(position.StartColumnOne, position.LinePosition)); panel.TabIndex = position.NextTabIndex; panel.Name = "preferenceListHolder" + position.TabIndex; return(panel); }
private void ListSelectionChanged(object sender, EventArgs e) { if (sender == null) { return; } if (sender is ListBox) { ListBox list = (ListBox)sender; // Handle a PreferenceList if (list.Tag != null && list.Tag is PreferenceListDetails) { PreferenceListDetails details = (PreferenceListDetails)list.Tag; details.ButtonUp.Enabled = (list.SelectedIndex > 0); details.ButtonDown.Enabled = (list.SelectedIndex != -1 && list.SelectedIndex != list.Items.Count - 1); } } }
private void ButtonClicked(object sender, EventArgs e) { if (sender == null) { return; } if (sender is Button) { Button button = (Button)sender; if (button.Tag == null) { return; } // Handle a PreferenceList if (button.Tag is PreferenceListDetails) { PreferenceListDetails details = (PreferenceListDetails)button.Tag; if (details.ListBox.SelectedIndex != -1) { sbyte indexChange; if (button == details.ButtonUp) { indexChange = -1; // one up } else if (button == details.ButtonDown) { indexChange = 1; // one down } else { return; // wrong button? } int selectedIndex = details.ListBox.SelectedIndex; int itemIndex = details.PreferenceList.Ranking[selectedIndex]; details.PreferenceList.Ranking.RemoveAt(selectedIndex); details.PreferenceList.Ranking.Insert(selectedIndex + indexChange, itemIndex); string itemString = details.ListBox.SelectedItem.ToString(); details.ListBox.Items.RemoveAt(selectedIndex); details.ListBox.Items.Insert(selectedIndex + indexChange, itemString); details.ListBox.SelectedIndex = selectedIndex + indexChange; _btnSave.Enabled = true; _btnApply.Enabled = true; } } // Handle a Path else if (button.Tag is PathDetails) { PathDetails details = (PathDetails)button.Tag; switch (details.Path.SelectedPathType) { case Path.PathType.FILE: OpenFileDialog ofd = new OpenFileDialog(); ofd.FileName = details.Path.SelectedPath; if (System.IO.File.Exists(ofd.FileName)) { ofd.InitialDirectory = System.IO.Path.GetDirectoryName(ofd.FileName); } if (ofd.ShowDialog() == DialogResult.OK) { details.TextBox.Text = ofd.FileName; details.Path.SelectedPath = ofd.FileName; _btnSave.Enabled = true; _btnApply.Enabled = true; } break; case Path.PathType.FOLDER: FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.SelectedPath = details.Path.SelectedPath; if (fbd.ShowDialog() == DialogResult.OK) { details.TextBox.Text = fbd.SelectedPath; details.Path.SelectedPath = fbd.SelectedPath; _btnSave.Enabled = true; _btnApply.Enabled = true; } break; default: return; } } } }
/// <summary> /// Creates a default PreferenceList. /// </summary> /// <param name="position"></param> /// <param name="tag"></param> /// <returns></returns> private Panel CreatePreferenceList(Position position, PreferenceList tag) { // List containing the items ListBox list = new ListBox(); list.TabIndex = position.NextTabIndex; list.Name = "preferenceList" + position.TabIndex.ToString(); //list.Size = new Size(position.Width - position.Margin, position.LineHeight * 4); list.Size = new Size(position.Width, position.LineHeight * 4); list.Location = new Point(0, 0); foreach (string item in tag.SortedItems) list.Items.Add(item); // Button to move items up Button up = new Button(); up.TabIndex = position.NextTabIndex; up.Name = "preferenceListUp" + position.TabIndex.ToString(); up.Location = new Point(position.Margin, list.Height); up.Size = new Size((int)(list.Width / 2 - position.Margin * 1.5), position.LineHeight); up.Text = "+"; up.Enabled = false; // Button to move items down Button down = new Button(); down.TabIndex = position.NextTabIndex; down.Name = "preferenceListDown" + position.TabIndex.ToString(); down.Location = new Point(up.Location.X + up.Size.Width + position.Margin, up.Location.Y); down.Size = up.Size; down.Text = "-"; down.Enabled = false; // Add events list.SelectedIndexChanged += _listboxSelectionChanged; up.Click += _buttonClicked; down.Click += _buttonClicked; // Create PreferenceListDetails PreferenceListDetails details = new PreferenceListDetails(tag, list, up, down); // Set tags list.Tag = details; up.Tag = details; down.Tag = details; // Create and return panel Panel panel = details.GetAsPanel(new Size(list.Width, position.LineHeight * 5), new Point(position.StartColumnOne, position.LinePosition)); panel.TabIndex = position.NextTabIndex; panel.Name = "preferenceListHolder" + position.TabIndex; return panel; }