******Can anyone explain me what do you mean by CreateControl

from:
http://social.msdn.microsoft.com/Forums/en/winforms/thread/eebdfa0d-696e-43c4-bcfc-7aabe027a44f

    Hi..i would like to know what is CreateControl() method.I have gone through msdn but i dint get any clear idea about its functioning.I would be glad if someone can explain it in simple words with an example.Thanks & Regards Nazima Mahmood
        ReplyReply
        QuoteQuote

Answers

    Wednesday, November 16, 2011 10:26 AM
    Avatar of Bob Wu-MT
    Bob Wu-MT
    Chinasoft
    (MSFT CSG)
    2,010
     Answer
    Sign In to Vote
    0
    Sign In to Vote

    Hi Nazima,

    CreateControl() method is used to force a handle to be created for the control and its child controls, which means it create a windows handle.

    So, what is windows handle?

    Wiki give a great Explanation about handle. Widows handle is similar, it is  a handle which hides a real memory address from the API user, but allowing the system to reorganize physical memory transparently to the program(From: http://stackoverflow.com/questions/902967/what-is-a-windows-handle ).

    In my opinion, windows handle is an identifier of a control. MSDN also mentioned: simply calling a control's constructor does not create the Handle.

    To prove this, please try the code below:

    TabControl control = new TabControl();

    TabPage page = new TabPage();

    page.Name = "Some Name";

    page.Text = "Some Text";

    page.Tag = new UserControl();

    //control.CreateControl();          

    control.TabPages.Insert(0, page);

     

    If we try to get the value of control.TabPages.Count and control.TabCount, both of them are zero.

    If we call control.CreateControl(); method in the code, both of them are one.

    The reason is that the new usercontol call the constructor,which will not create the handle, and the windows can’t find it without a handle in first situation. If we call the CreateControl() method, a handle is created and we get the right result.

    Best Regards,

      
    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
        Marked As Answer byNazimaMonday, November 21, 2011 8:01 AM
         
        ReplyReply
        QuoteQuote

All Replies

    Wednesday, November 16, 2011 10:26 AM
    Avatar of Bob Wu-MT
    Bob Wu-MT
    Chinasoft
    (MSFT CSG)
    2,010
     Answer
    Sign In to Vote
    0
    Sign In to Vote

    Hi Nazima,

    CreateControl() method is used to force a handle to be created for the control and its child controls, which means it create a windows handle.

    So, what is windows handle?

    Wiki give a great Explanation about handle. Widows handle is similar, it is  a handle which hides a real memory address from the API user, but allowing the system to reorganize physical memory transparently to the program(From: http://stackoverflow.com/questions/902967/what-is-a-windows-handle ).

    In my opinion, windows handle is an identifier of a control. MSDN also mentioned: simply calling a control's constructor does not create the Handle.

    To prove this, please try the code below:

    TabControl control = new TabControl();

    TabPage page = new TabPage();

    page.Name = "Some Name";

    page.Text = "Some Text";

    page.Tag = new UserControl();

    //control.CreateControl();          

    control.TabPages.Insert(0, page);

     

    If we try to get the value of control.TabPages.Count and control.TabCount, both of them are zero.

    If we call control.CreateControl(); method in the code, both of them are one.

    The reason is that the new usercontol call the constructor,which will not create the handle, and the windows can’t find it without a handle in first situation. If we call the CreateControl() method, a handle is created and we get the right result.

    Best Regards,

      
    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
        Marked As Answer byNazimaMonday, November 21, 2011 8:01 AM
         
        ReplyReply
        QuoteQuote
    Monday, November 21, 2011 8:02 AM
    Avatar of Nazima
    Nazima
    30
     
    Sign In to Vote
    0
    Sign In to Vote
    Thank You..Thanks & Regards Nazima Mahmood

你可能感兴趣的:(EXPLAIN)