Windows Develop Bookmark and Share   
 index > Windows Forms General > Locating and adding controls to panel at run time
 

Locating and adding controls to panel at run time

I have a panel control which may or may not load a user control consisiting of two text boxes in a side by side fashion. Once the form is loaded user may make a call to the same user control that will will add more text boxes to the same panel. The problem I have is after the form load when a user interaction make s a call to the user control it places it on the on the existing text box controls in the panel.

Following method is called from form_load

private void loadPanelFileCount_1()

{

for (int i = 0; i < recordsFileNameCount_1.Count; i++)

{

Book.UserControls.uFileNameWithComments uc = new Book.UserControls.uFileNameWithComments();

uc.Location = new Point(1, 10 + 22 * i);

uc.FileName = recordsFileNameCount_1Idea.ToString();

uc.FileNum = recordFileNumCount_1Idea.ToString();

uc.Comments = recordsFileComments_1Idea.ToString();

pnlChkinFiles.Controls.Add(uc);

}

}

pnlCtrlCounter = pnlChkinFiles.Controls.Count;

}//end method loadPanelCheckoutFiles

Now after the form load is done, user may add more controls in the panel by double clicking on a listview. This method is called from a list view remove items.

private void loadPanelCheckinFiles()

{

UserComments = new ArrayList();

uc.Location = new Point(1, 10 + 22 * locCounter);

uc.FileName = file_name;

uc.FileNum = file_number.ToString();

//uc.UserName =listView1.SelectedItems[0].SubItems[2].Text;

uc.UserName = user_name;// fileChkoutToName[0].ToString();

uc.Comments = file_comments;//uc.rchtxtComments.Text;

uc.TransFlag = transferFlag;

pnlChkinFiles.Controls.Add(uc);

locCounter=locCounter+1;

}//end method loadPanelCheckoutFiles

But what is doing it placing the text boxes on already exisiting text boxes and visually the panel contains only one set of text box fields.

If anyone could help me out in resolving this issue, it will be greatly appreciated.

coolarian  Wednesday, March 21, 2007 11:17 PM

Hi

I guess that the loc_counter must be have the last value of i [recordsFileNameCount_1.Count] before getting out of the form load method so that when you do this

uc.Location = new Point(1, 10 + 22 * locCounter);

in the loadPanelCheckinFiles method, it starts placing after the previous created controls.

Hope that helps.

Thanks

Vivek Ragunathan

Vivek Ragunathan  Thursday, March 22, 2007 12:11 AM
yes,as what Vivek said,you can populate the Control.Location Property (System.Windows.Forms)before adding the new control to the form,for more infor ,look at this link.
Gavin Jin - MSFT  Thursday, March 22, 2007 6:50 AM

Hi

I guess that the loc_counter must be have the last value of i [recordsFileNameCount_1.Count] before getting out of the form load method so that when you do this

uc.Location = new Point(1, 10 + 22 * locCounter);

in the loadPanelCheckinFiles method, it starts placing after the previous created controls.

Hope that helps.

Thanks

Vivek Ragunathan

Vivek Ragunathan  Thursday, March 22, 2007 12:11 AM
yes,as what Vivek said,you can populate the Control.Location Property (System.Windows.Forms)before adding the new control to the form,for more infor ,look at this link.
Gavin Jin - MSFT  Thursday, March 22, 2007 6:50 AM

Thanks a lot Vivek. Your are absolutely right, as per your suggestion I count the number of controls already added after form_load and then just initialize locCounter to this count.

Thank you Gavin, the link you mentioned is really helpful.

coolarian  Thursday, March 22, 2007 1:37 PM

You can use google to search for other answers

Custom Search

More Threads

• How to determinate that other window is under my control.
• Column Selection
• MeasureCharacterRanges very slow when printing
• Fonts or words
• accessing a form and its controls knowing the formname
• RichTextBox with large text and redraw problem
• How can I add an uninstaller in the user's desktop shortcut
• NewWindow2 problem
• Selecting multiple items in a listbox from string in a database.
• connection scheduled