Reputation: 25
I have several textbox in a form. I have written many of the codes and store them in database As:
Textbox1.Location = New System.Drawing.Point(609, 3)
Textbox2.Location = New System.Drawing.Point(659, 3)
Textbox1.BackColor = System.Drawing.Color.Green
Textbox2.BackColor = System.Drawing.Color.Blue
TextboxX.AnyProperty = PropertyValue
The reason of writing code in database is that if user wants textbox at specific location or wants to change any property of the textbox, our programmers change the code in Database for that textbox instead of recompiling the software.
Is there any way to execute these codes from database to change the properties of textbox accordingly?
Upvotes: 2
Views: 1593
Reputation: 19843
You need to serialize (xml or binary) and store the values to database, then in OnCreate event handler of form read the values from database and set to them accordingly using Reflection
.
let see the structure of database table
This is my suggestion
Create table FormData
(ID int,
FormFullTypeName varchar(500),
ControlName varchar(500),
PropertyName varchar(100),
Value varchar(max))
in OnCreate
of form query this table based on form's full type name and then loop through records and find the specific Control
and then find the corresponding Property
in control by Reflection
and then deserialize the value and set the value to the Property again by Reflection
.
for example I assume that you have set this variables from your db
var controlName = 'yourcontrolName from db';
var propertyName = 'your property name from db';
object value = //the deserialized value from db;
var control = findControlByName(controlName);
control.GetType().GetProperty(propertyName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).SetValue(control, value, new object[] { });
for implementing findControlByName take a look at this link Get a Windows Forms control by name in C#
I hope this helps you
Upvotes: 2