Help with dynamically creating controls in a loop

This is a discussion on Help with dynamically creating controls in a loop within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I need to create a number of edit boxes once the user clicks on a button. I'm ok with ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Help with dynamically creating controls in a loop

    Hi,

    I need to create a number of edit boxes once the user clicks on a button. I'm ok with making one but if I want 5, I can't use the loop. What is the proper way to make them?

    The source code crashes at run time, once the user hits Button2:
    Code:
    void CInitialGUIDlg::OnButton2() 
    {
    	// TODO: Add your control notification handler code here
    	
    	CEdit* pEdit = new CEdit;
    	int x1=325, y1=100, x2=425, y2=125, i=1;
    
    	for(i; i<5; i++)
    	{
    		
    		pEdit->Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
          CRect(x1,y1, x2, y2), this, i);
    		y1 = y1 + 50;
    		y2= y1 + 50;
    	}
    ......................
    I want to try and do that with static text later too.

    Thanks,
    AS

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    you have five edits but only alloc memory for one..

    Code:
    CEdit   *pEditArray[5];
    
    for(i=0;i<5;i++)
    {
        pEditArray[i]= new CEdit;
        pEditArray[i]->Create(
    
       //ect
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 07:04 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. Creating a Loop (PLEASE HELP!)
    By ChaosTony in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2004, 09:34 PM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. creating new objects in loop
    By Joe_Gibbs in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2004, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21