Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reflection Question

Reflection Question

Scheduled Pinned Locked Moved C#
helpdatabasedata-structuresregexjson
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrColeyted
    wrote on last edited by
    #1

    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

    P J 2 Replies Last reply
    0
    • M MrColeyted

      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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Perhaps the first form should have a public property that yields a list of the names of the controls?

      M 1 Reply Last reply
      0
      • P PIEBALDconsult

        Perhaps the first form should have a public property that yields a list of the names of the controls?

        M Offline
        M Offline
        MrColeyted
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M MrColeyted

          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

          J Offline
          J Offline
          J a a n s
          wrote on last edited by
          #4

          MrColeyted wrote:

          richTextBox1.Text += "\n" + prop.Name + "\t\t:" + prop.GetValue(t, null);

          You are trying to find the property value from the type, you have tried to get it from the object itself :)

          richTextBox1.Text += "\n" +
                prop.Name + "\t\t:" + prop.GetValue(controls[index], null);

          *jaans

          M 1 Reply Last reply
          0
          • J J a a n s

            MrColeyted wrote:

            richTextBox1.Text += "\n" + prop.Name + "\t\t:" + prop.GetValue(t, null);

            You are trying to find the property value from the type, you have tried to get it from the object itself :)

            richTextBox1.Text += "\n" +
                  prop.Name + "\t\t:" + prop.GetValue(controls[index], null);

            *jaans

            M Offline
            M Offline
            MrColeyted
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups