Frage

Mein Windows-Formular enthält ​DevExpress.XtraGrid.GridControl An Form1 In ähnlicher Weise wird es auch als 2. Klasse bezeichnet Form2.Auf Form1 lade ich Daten aus der Datenbank.Wenn ich auf die Rasterzeile doppelklicke, wird sie einem Form2 zugewiesen.An Form1 gridControl1_DoubleClick Ereignis IsHandleCreated Prop ist wahr (Form2 wird geerbt von Form1)

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

also habe ich eine Eigenschaft wie auf Form1 erstellt

    public GridControl GridControl1
    {
        get { return gridControl1; }

    }

aber wenn ich die Display()-Methode von Form2 aufrufe und überprüfe, dass die IsHandleCreated-Requisite auf Form2 falsch ist.

public void Display()
    {
        if (handleCreated)
        {

        }           
    }

Vollständiger Code wie unten **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 ist handleCreated immer falsch. Ich weiß nicht warum?Bitte hilf mir

War es hilfreich?

Lösung

Das liegt daran, dass Sie form2 Das Objekt wird nur initialisiert.Das Steuerelement erhält sein Handle erst, nachdem ein Fenster erstellt wurde, in dem dieses Steuerelement angezeigt wird.Sie müssen also anrufen form2.Show() oder form2.ShowDialog() und danach prüfen gridControl1.IsHandleCreated.
Sie können dieses Verhalten einfach testen, indem Sie diesen Code verwenden:

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

obj.Show();
MessageBox.Show("Shown: " + obj.handleCreated);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top