Question

My windows form contains ​DevExpress.XtraGrid.GridControl on Form1 similar way there is also 2nd class called it as Form2. On Form1 I am loading data from database. When I double click on grid row it assigns to an Form2. on Form1 gridControl1_DoubleClick event IsHandleCreated prop is true (Form2 is Inherited from Form1)

    void gridControl1_DoubleClick(object sender, EventArgs e)
    {
        if (gridControl1.IsHandleCreated)
        {
        }
        Form2 obj = new Form2();
        obj.Display();
    }

so I have created one property like on Form1

    public GridControl GridControl1
    {
        get { return gridControl1; }

    }

but when I call the Display() method of Form2 and check the IsHandleCreated prop on Form2 is false.

public void Display()
    {
        if (handleCreated)
        {

        }           
    }

complete code as below **Form1**

public partial class Form1 : Form
    {

        public GridControl GridControl1
        {
            get { return gridControl1; }

        }

        public bool handleCreated
        {
            get { return gridControl1.IsHandleCreated; }
        }

        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable(20);
            gridControl1.DoubleClick += gridControl1_DoubleClick;

        }

        void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridControl1.IsHandleCreated)
            {
            }
            Form2 obj = new Form2();
            obj.Display();
        }


        private DataTable CreateTable(int rowCount)
        {
            DataTable table = new DataTable();
            table.Columns.Add("String", typeof(string));
            table.Columns.Add("Int", typeof(int));
            table.Columns.Add("Date", typeof(DateTime));
            for (var i = 0; i < rowCount; i++)
            {
                table.Rows.Add(string.Format("Row {0}", i), i, DateTime.Today.AddDays(i));
            }
            return table;
        }
}

**Form2**
public class Form2 : Form1
    {
        public Form2()
        {
        }
        public void Display()
        {
            if (handleCreated)
            {

            }
            //Form1 obj = new Form1();

            //if (obj.handleCreated)
            //{
            //}
        }
    }

In Form2 handleCreated its always false I dont know why? please help me

Was it helpful?

Solution

This is because your form2 object is just only initialized. Control will get its handle only after window is created, which displays this control. So, you need to call form2.Show() or form2.ShowDialog() and after that check for gridControl1.IsHandleCreated.
You can simply test this behavior by using this code:

Form2 obj = new Form2();
MessageBox.Show("Created: " + obj.handleCreated);

obj.Show();
MessageBox.Show("Shown: " + obj.handleCreated);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top