Thank you but I still have some problem as I explain here.
1 application with form1
1 dll which is holding some usercontrol etc...
The idea is to use the user control dll to alter the Form1 controls
When Form1 load then I call and initialize my usercontrol dll
First I notice that the controls have too be public and not private >>> is that correct?
Can this work with private or do I have to change all the default setting of every control to public?
Second it look like I dont really have an handle of the source class (Form1 from the usercontrol dll) >>>>
When this is run within the form1_Load all together it works fine, but not when the code reside into the dll.
It must be something very stupid that I'm doing here. But I'm loosing my hair way too fast on that one.
Thank for not letting going hairless.
I marked the code where it stop working.
FORM1
private void Form1_Load(object sender, System.EventArgs e)
{
// The control Button1 is well set in the Form1 has it is used correctly.
inTouch.Tools.Property.GetControlProperties getProp = new inTouch.Tools.Property.GetControlProperties(this, this.Button1,"BackColor","ffffff80");
}
USERCONTROL DLL
namespace inTouch.Tools.Property
{
public class GetControlProperties
{
public GetControlProperties(Form myForm, Control myControl , string myProperty, object myPropValue)
{
// I assume here that this is BackColor
// then I receive a color for myPropValue
System.Reflection.FieldInfo fi = Type.GetType(myForm.GetType().ToString()).GetField (myControl.ToString()) ;
> STOP HERE WITH THIS ERROR (Description: Object reference not set to an instance of an object.)
object o = fi.GetValue(myControl);
<<<<<<
System.Reflection.PropertyInfo pi = o.GetType().GetProperty(myProperty);
string text = pi.GetValue(myControl, null).ToString();
MessageBox.Show(text.ToString());
// NOW Set the new Property
o.GetType().GetProperty(myProperty).SetValue(o,Col or.FromArgb((int)myPropValue), null);
}
}
}
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:Oh****************@TK2MSFTNGP10.phx.gbl...
Hi again Nicolas,
Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
therefore the correct construction is:
FieldInfo fi = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1") ;
This line return a FieldInfo instance, now to get the value of that instance you could use:
object o = fi.GetValue( this);
now you have the value of the field ( textBox1 ) for a particular instance ( this )
to get the value of the property "Text" of it you have to do something similar:
PropertyInfo pi = o.GetType() .GetProperty("Text") ;
And finally evaluate it:
string text = pi.GetValue( textBox1, null).ToString();
Hope you understand it, otherwise just post back ;)
In short you could do this:
string text = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();
Hope this help,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
message news:OP**************@TK2MSFTNGP12.phx.gbl...
The lat part is not working why ????????
Please help.........
using System;
namespace ConsoleApplication4
{
class reflection
{
public const string mstr = "hellpppo world!";
public System.Windows.Forms.TextBox textBox1;
[STAThread]
static void Main(string[] args)
{
this.textBox1 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)) , ((System.Byte)(64)), ((System.Byte)(0)));
this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)) , ((System.Byte)(192)), ((System.Byte)(0)));
this.textBox1.Location = new System.Drawing.Point(24, 48);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 12;
this.textBox1.Text = "textBox1";
this.Controls.Add(this.textBox1);
System.Reflection.FieldInfo fi =
Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");
string temp = (string)fi.GetValue(null);
MessageBox.Show("get string {0}", temp);
/////////// THIS IS NOT WORKING \\\\\\\\\\\
Type.GetType("ConsoleApplication4.reflection") ..GetField("textbox1");
string temp = (string)fi.GetValue("Text");
MessageBox.Show("get string {0}", temp);
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////
ERROR Object Type cannot be converted to target type
}
}
}