0

I have a winform with button and opendialog, here is my code :

[Form1.cs]:

    using System;
    using System.Windows.Forms;

    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Class1 obj=new Class1();
                obj.get_info(this);
            }
        }
    }

[class1.cs]:

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Windows.Forms;

    namespace WindowsFormsApplication4
    {
        class Class1
        {

            private IEnumerable<Component> EnumerateComponents(Form frm)
            {
                return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                       where typeof(Component).IsAssignableFrom(field.FieldType)
                       let component = (Component)field.GetValue(frm)
                       where component != null
                       select component;
            }

            public void get_info(Form frm)
            {
                foreach (Component c in EnumerateComponents(frm))
                {
                    if (c.GetType() == typeof(OpenFileDialog))
                    {
                        MessageBox.Show("Detected OpenFileDialog");
                    }
                }
            }
        }
    }

why it does not work ?

I have visited these links below but I could not utilize them to solve my problem :

Access form component from another class Accessing Form's Controls from another class How to access a visual component from another form in c#

thanks

  • Break up the LINQ block into separate statements that assign their results to a variable, and inspect those variables. – CodeCaster May 25 '16 at 10:21
  • @CodeCaster : can you please provide example or post your answer. I am a super newbie :) – newbie.coder May 25 '16 at 10:25
  • What do you mean by "it does not work"? Bad return value? Exception thrown? App crashes? – DonBoitnott May 25 '16 at 11:09
  • @DonBoitnott : it does not show messagebox – newbie.coder May 25 '16 at 11:11
  • First off, it looks like you're trying to spot an open instance of an `OpenFileDialog`, but you never produce one, so how would the test ever pass in that example code? – DonBoitnott May 25 '16 at 11:14
  • @DonBoitnott :i want to detect the existence of an openfiledialog component in the main form and i have dragged an openfiledialog in the component part of the form. the above code works fine in the main form1 class but if i do the same in another class like class1 , it does not work. – newbie.coder May 25 '16 at 11:19

1 Answers1

0

You are asking the wrong type to provide your answer. Instead, ask for the frm.GetType():

private IEnumerable<Component> EnumerateComponents(Form frm)
{
    return from field in frm.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
        where typeof(Component).IsAssignableFrom(field.FieldType)
        let component = (Component)field.GetValue(frm)
        where component != null
        select component;
}

It worked in the Form code because inherently, GetType() is equivalent to this.GetType(), with this being the form.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68