Reflection Question
-
Hello all! I have a college project due tomorrow and I am in dire need of some assistance! The project requires that I create a plain windows form with six controls on it; I have chosen six buttons. Form2 will be the startup form, will have a combobox and a DataGridView (I am using a richTextBox right now, but will change that once the rest works). The names of the controls from Form1 populate the combobox in Form2. By selecting one of the controls from the combobox, all of that controls' properties are then listed in the DataGridView. I have most of it working, but I cannot get the value for each property using the GetValue(object, object[] index) method. I have tried all that I can think of, but cannot get it to work. :mad::confused: The code that I have for Form2 is as follows: Here, the main problem is on the bottom line of code. I have commented there. public partial class Form2 : Form { //Declares an Array of type Controls: Control[] controls; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { //Creates an instance of Form1: Form1 myForm = new Form1(); //Initializes the Control[] by calling the getControls() method on Form1: controls = myForm.getControls(); //Adds the Control names to the ComboBox: foreach (Control c in controls) { myCboBox.Items.Add(c.Name); } } private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); //clears any previous entries: richTextBox1.Clear(); foreach (PropertyInfo prop in t.GetProperties()) { //Here, the GetValue method will not work! I have tried passing everything that I can think of to it //and still it will not work. It keeps telling me that target object does not match. //Please help. richTextBox1.Text += "\n" + prop.Name + "\t\t:" + prop.GetValue(t, null); } } } "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
-
Hello all! I have a college project due tomorrow and I am in dire need of some assistance! The project requires that I create a plain windows form with six controls on it; I have chosen six buttons. Form2 will be the startup form, will have a combobox and a DataGridView (I am using a richTextBox right now, but will change that once the rest works). The names of the controls from Form1 populate the combobox in Form2. By selecting one of the controls from the combobox, all of that controls' properties are then listed in the DataGridView. I have most of it working, but I cannot get the value for each property using the GetValue(object, object[] index) method. I have tried all that I can think of, but cannot get it to work. :mad::confused: The code that I have for Form2 is as follows: Here, the main problem is on the bottom line of code. I have commented there. public partial class Form2 : Form { //Declares an Array of type Controls: Control[] controls; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { //Creates an instance of Form1: Form1 myForm = new Form1(); //Initializes the Control[] by calling the getControls() method on Form1: controls = myForm.getControls(); //Adds the Control names to the ComboBox: foreach (Control c in controls) { myCboBox.Items.Add(c.Name); } } private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); //clears any previous entries: richTextBox1.Clear(); foreach (PropertyInfo prop in t.GetProperties()) { //Here, the GetValue method will not work! I have tried passing everything that I can think of to it //and still it will not work. It keeps telling me that target object does not match. //Please help. richTextBox1.Text += "\n" + prop.Name + "\t\t:" + prop.GetValue(t, null); } } } "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
Perhaps the first form should have a public property that yields a list of the names of the controls?
-
Perhaps the first form should have a public property that yields a list of the names of the controls?
The first form does. It has an array of Controls that are populated by the Controls that are on it. There is a method, GetControls() that returns the array of Controls. Form2 then loops through that array and populates the comboboxlike so: foreach (Control c in controls) { comboBox1.Items.Add(c.Name); } I know that this is working because when the code is ran, if you take away the faulty attempt to call the GetValue(object, object[] index) method, all of the property names are properly displayed in the richTextBox. I need to know the proper way to call the GetValue(object, object[] index) method to get the property values, given the way that I am going about the logic. Thanks. "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
-
Hello all! I have a college project due tomorrow and I am in dire need of some assistance! The project requires that I create a plain windows form with six controls on it; I have chosen six buttons. Form2 will be the startup form, will have a combobox and a DataGridView (I am using a richTextBox right now, but will change that once the rest works). The names of the controls from Form1 populate the combobox in Form2. By selecting one of the controls from the combobox, all of that controls' properties are then listed in the DataGridView. I have most of it working, but I cannot get the value for each property using the GetValue(object, object[] index) method. I have tried all that I can think of, but cannot get it to work. :mad::confused: The code that I have for Form2 is as follows: Here, the main problem is on the bottom line of code. I have commented there. public partial class Form2 : Form { //Declares an Array of type Controls: Control[] controls; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { //Creates an instance of Form1: Form1 myForm = new Form1(); //Initializes the Control[] by calling the getControls() method on Form1: controls = myForm.getControls(); //Adds the Control names to the ComboBox: foreach (Control c in controls) { myCboBox.Items.Add(c.Name); } } private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); //clears any previous entries: richTextBox1.Clear(); foreach (PropertyInfo prop in t.GetProperties()) { //Here, the GetValue method will not work! I have tried passing everything that I can think of to it //and still it will not work. It keeps telling me that target object does not match. //Please help. richTextBox1.Text += "\n" + prop.Name + "\t\t:" + prop.GetValue(t, null); } } } "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
-
My friend, I could kiss you! Thank you so very much. That did it!
"If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra