I'm not sure why you'd want to do this, but this is achievable using reflection.
Here is an example:
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Fill_Click(object sender, EventArgs e)
{
string propertyMatcher = "cpMythic";
var defaultSettings = new Properties.Settings.Default(); //Get instance of default settings
var settingProperties = defaultSettings.GetType().GetFields(); //Get default setting properties.
foreach (TextBox control in this.Controls.OfType<TextBox>().Where(c => c.Name.StartsWith("cpMythic"))) //Loop through controls, and filter
{
string settingPropertyName = "M" + control.Name.Substring(propertyMatcher.Length);
var settingProperty = settingProperties.FirstOrDefault(p => p.Name == settingPropertyName);
var value = settingProperty.GetValue(defaultSettings); //Get the value of the corresponding setting using reflection
control.Text = value.ToString(); //Set value
}
}
private static class Properties
{
public static class Settings
{
public class Default
{
public string M0 = "Default Value 0";
public string M1 = "Default Value 1";
public string M2 = "Default Value 2";
}
}
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…