Reputation: 898
Whenever there is a need for a new application setting in my C# project, I add it via PROJECT -> Properties -> Settings. Currently I have about 20 application settings in my C# project, but they are out of order.
To be able to change the settings during runtime I created a simple settings panel by iterating over the settings.
foreach (System.Configuration.SettingsProperty prop in Properties.Settings.Default.Properties)
{
Label caption = new Label();
caption.Text = prop.Name;
caption.Location = new Point(10, this.Height - 70);
caption.Size = new Size(100, 13);
caption.Anchor = AnchorStyles.Left | AnchorStyles.Top;
TextBox textbox = new TextBox();
textbox.Name = prop.Name;
textbox.Text = Properties.Settings.Default[prop.Name].ToString();
textbox.Location = new Point(120, this.Height - 70);
textbox.Size = new Size(this.Width - 140, 23);
textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
if (prop.IsReadOnly)
textbox.ReadOnly = true;
this.Height += 30;
this.Controls.Add(caption);
this.Controls.Add(textbox);
}
It works fine. But the labels are in the same unlogical order as I entered them in the Visual Studio UI.
Is there a way to either rearrange the order of the settings in Visual Studio or sort the System.Configuration.SettingsPropertyCollection during runtime?
Because SettingsPropertyCollection is an IEnumerable I was trying to use LINQ like this:
Properties.Settings.Default.Properties.OrderBy(s => s.Name)
But it didn't compile complaining about missing OrderBy extension for SettingsPropertyCollection.
Upvotes: 3
Views: 2004
Reputation: 89295
Since it implements IEnumerable
instead of IEnumerable<T>
, you'll need to call Cast<T>
before calling OrderBy
:
Properties.Settings
.Default
.Properties
.Cast<System.Configuration.SettingsProperty>()
.OrderBy(s => s.Name)
Upvotes: 5
Reputation: 1038
You can try:
Properties.Settings.Default.Properties.OfType<SettingsProperty>().OrderBy(s => s.Name)
Upvotes: 4