ConfigListBox.cs (4374B)
1 using System; 2 using System.ComponentModel; 3 using System.Windows.Forms; 4 5 namespace LanTool 6 { 7 public partial class ConfigListBox : UserControl 8 { 9 /// <summary> 10 /// spezielle Listbox für die Konfiguration 11 /// </summary> 12 public ConfigListBox() 13 { 14 InitializeComponent(); 15 } 16 17 /// <summary> 18 /// Setzt die Überschrift über die ConfigListBox 19 /// </summary> 20 [Browsable(true)] 21 public string Header 22 { 23 get 24 { 25 return this.ConfigurationLabel.Text; 26 } 27 set 28 { 29 this.ConfigurationLabel.Text = value; 30 } 31 } 32 33 private bool _hasFolderSelectButton = true; 34 /// <summary> 35 /// Gibt an, ob ein Folder-Auswahl-Knopf angeboten wird 36 /// </summary> 37 [Browsable(true)] 38 public bool HasFolderSelectButton 39 { 40 get 41 { 42 return _hasFolderSelectButton; 43 } 44 set 45 { 46 _hasFolderSelectButton = value; 47 this.FolderSelectButton.Visible = _hasFolderSelectButton; 48 this.AddTextBox.Width = HasFolderSelectButton ? 161 : 192; 49 } 50 } 51 52 private bool _hasNewFolderButton = true; 53 /// <summary> 54 /// Gibt an, ob der bei der Ordnerauswahl "Neuen Ordner anlegen" angeboten wird 55 /// </summary> 56 [Browsable(true)] 57 public bool HasNewFolderButton 58 { 59 get 60 { 61 return _hasNewFolderButton; 62 } 63 set 64 { 65 _hasNewFolderButton = value; 66 } 67 } 68 69 private bool _sorted = true; 70 /// <summary> 71 /// Gibt an, ob der bei der Ordnerauswahl "Neuen Ordner anlegen" angeboten wird 72 /// </summary> 73 [Browsable(true)] 74 public bool Sorted 75 { 76 get 77 { 78 return _sorted; 79 } 80 set 81 { 82 _sorted = value; 83 if (_sorted) 84 { 85 UpButton.Visible = false; 86 DownButton.Visible = false; 87 } 88 } 89 } 90 91 /// <summary> 92 /// alle enthaltenen Elemente 93 /// </summary> 94 public ListBox.ObjectCollection Items 95 { 96 get 97 { 98 return this.ConfigurationListBox.Items; 99 } 100 } 101 102 /// <summary> 103 /// Klickt auf den Button "Ordner auswählen" 104 /// </summary> 105 private void FolderSelectButton_Click(object sender, EventArgs e) 106 { 107 using (FolderBrowserDialog fd = new FolderBrowserDialog()) 108 { 109 fd.ShowNewFolderButton = this.HasNewFolderButton; 110 if (fd.ShowDialog(this).Equals(DialogResult.OK)) 111 { 112 this.AddTextBox.Text = fd.SelectedPath; 113 } 114 } 115 } 116 117 /// <summary> 118 /// Click auf den Button "Speichern" 119 /// </summary> 120 private void AddButton_Click(object sender, EventArgs e) 121 { 122 if (!String.IsNullOrEmpty(this.AddTextBox.Text)) 123 { 124 int sel = this.ConfigurationListBox.SelectedIndex; 125 if (sel >= 0) 126 { 127 this.ConfigurationListBox.Items[sel] = this.AddTextBox.Text; 128 this.ConfigurationListBox.SelectedIndex = -1; 129 } 130 else 131 { 132 this.ConfigurationListBox.Items.Add(this.AddTextBox.Text); 133 } 134 this.AddTextBox.Text = ""; 135 } 136 } 137 138 private void UpButton_Click(object sender, EventArgs e) 139 { 140 int sel = this.ConfigurationListBox.SelectedIndex; 141 if (sel > 0) 142 { 143 string wert = this.ConfigurationListBox.Items[sel].ToString(); 144 this.ConfigurationListBox.Items.RemoveAt(sel); 145 this.ConfigurationListBox.Items.Insert(sel - 1, wert); 146 this.ConfigurationListBox.SelectedIndex = sel - 1; 147 } 148 } 149 150 private void DownButton_Click(object sender, EventArgs e) 151 { 152 int sel = this.ConfigurationListBox.SelectedIndex; 153 if (sel >= 0 && sel < this.ConfigurationListBox.Items.Count - 1) 154 { 155 string wert = this.ConfigurationListBox.Items[sel].ToString(); 156 this.ConfigurationListBox.Items.RemoveAt(sel); 157 this.ConfigurationListBox.Items.Insert(sel + 1, wert); 158 this.ConfigurationListBox.SelectedIndex = sel + 1; 159 } 160 } 161 162 private void DeleteButton_Click(object sender, EventArgs e) 163 { 164 int sel = this.ConfigurationListBox.SelectedIndex; 165 this.ConfigurationListBox.SelectedIndex = -1; 166 if (sel >= 0) 167 { 168 this.ConfigurationListBox.Items.RemoveAt(sel); 169 this.ConfigurationListBox.SelectedIndex = -1; 170 this.AddTextBox.Text = ""; 171 } 172 } 173 174 private int _lastSelection = -1; 175 private void ConfigurationListBox_SelectedIndexChanged(object sender, EventArgs e) 176 { 177 if (this.ConfigurationListBox.SelectedIndex == _lastSelection && this.ConfigurationListBox.SelectedIndex != -1) 178 { 179 this.ConfigurationListBox.SelectedIndex = -1; 180 } 181 _lastSelection = this.ConfigurationListBox.SelectedIndex; 182 if (_lastSelection >= 0) 183 { 184 this.AddTextBox.Text = ConfigurationListBox.Items[_lastSelection].ToString(); 185 } 186 } 187 } 188 }