2

I have a form, with a lot of settings pages. All the pages are the same, so I would rather just make a single form, and then pass in the name of the setting its supposed to edit. How would I do this? Lets say the form is called "ConfigForm", I want to be able to call it with something like this:

new ConfigForm("event1").Show();

Simple, this pulls up the ConfigForm, and sends the string "event1". However, knowing that I am looking for the setting "event1", how do I now access this setting? Normally to access a setting, I assume I would use something like this: (event1 is a StringCollection)

string varName = Properties.Settings.Default.passedString[3];

How do I put "event1" in a string, when event1 is stored in the variable "passedString"? In PHP, I would use something like EVAL. How would I do it in C#?

---- EDIT

Previous answers have solved the "settings" problem; but it doesn't answer the underlying question. How do you use a string passed along in a variable as a variable identifier? So if I had a string called "passedString", with the text "event1" stored in it... How would I get that to convert:

this.(passedString).Text = "test";

into

this.event1.Text = "test";
Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78

3 Answers3

1

Default inherits from SettingsBase that has the following property:

Object this[
    string propertyName
] { get; set; }

So you can use []. Don't forget to cast it to StringCollection class like below

((StringCollection)Properties.Settings.Default[passedSetting])[3]

If you want to update your StringColleciton, you can get a reference to it this way:

StringCollection collection = (StringCollection)Properties.Settings.Default[passedSetting];
collection.Add("another value");
collection[3] = "replace";
Properties.Settings.Default[passedSetting] = collection; // We'd rather call the setter.
evpo
  • 2,436
  • 16
  • 23
  • Thats pretty much exactly what I'm looking for when it comes to getting data... What can do I about setting data? – Jason Axelrod Feb 09 '12 at 05:20
  • This works for settings... but how would I do this with other form items? For instance, what if I wanted to edit "this.(passedSetting).Text". Without some sort of EVAL, how would this be possible? – Jason Axelrod Feb 09 '12 at 21:21
  • I understand what you want because I moved from VFP world myself where you had to use EVAL a lot. I haven't used such a thing for many years while working in C#. May be you need to look at http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx In C# I always look for ways to pass a string as a parameter instead of building code and EVALing it. – evpo Feb 09 '12 at 22:29
  • You can also look here but I don't recommend doing it for your task again. http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/3a720901-fdc5-47dc-8057-e06454525c82/ – evpo Feb 09 '12 at 22:47
0

Define a constructor in the form that you want the argument to be passed to.

public class MyForm : Form
{
    protected readonly string setting;

    public MyForm(string setting)
    {
        this.setting = setting;
    }
}


// to open
(new MyForm("event1")).Show();
Matthew
  • 24,703
  • 9
  • 76
  • 110
  • You missed the second half of the question. It might look something like `Properties.Settings.Default[setting] = someControl.Text`. – M.Babcock Feb 09 '12 at 04:59
  • 1
    To be honest I don't even understand the second half of the question. – Matthew Feb 09 '12 at 05:01
  • That doesn't answer my question... I need to access a setting called "event1", without EXPLICITLY defining event1 in the code. "event1" is instead passed along in a string. – Jason Axelrod Feb 09 '12 at 05:02
  • @JasonAxelrod - The configuration provider is basically a dictionary with pretty dynamically generated properties over it, just pass your property name to its indexer property and assign it. Just remember to call Save when you're done if you want it persisted. – M.Babcock Feb 09 '12 at 05:05
  • probably a good idea to add `: this()` to the end of the constructor line, forms generated in Visual Studio typically call `InitializeComponent()` in the default constructor. – Robert Rouhani Feb 09 '12 at 05:16
0

To get/set a setting by string key use Settings.Default.this[] indexer.

To get/set a property of a control within a form use may use this simple technique:

this.Controls["button1"].Text = "someText";

// or
this.Controls.Find("button1", true).First().Text = "someText";

And if it comes to any property of a form or any other object U may use your own special setters or at last reflection.

But avoid using reflection in such simple situation.

Community
  • 1
  • 1
Dmitry Polomoshnov
  • 5,106
  • 2
  • 24
  • 25