Click here to Skip to main content
13,000,324 members (74,651 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi experts,

I need to call a user control on to a form dynamically. The form contains a list grid with data which is being fetched from database. On click of a particular row the details should be populated into user control and it should be called on form. The user control contains other controls like text boxes, labels, combo box etc.

Thanks in advance,
Posted 28-Mar-13 23:46pm
Mahesh_Bhosale 29-Mar-13 5:55am
Can you share the piece of code, where you create an instance for user control..?
ankitjha23 29-Mar-13 6:13am
private void ListGridList_LinkLabelClicked(object sender, LinkLabelEventArgs linkLabelEventArgs)
int rowNum = linkLabelEventArgs.rowNum;
string SoNbr = ((ListGridList)sender).DataSet.Tables["listItem"].Rows[rowNum]["SoNbr"].ToString().Trim();
ClosedSOUserControl CSOUCobj = new ClosedSOUserControl();

ankitjha23 29-Mar-13 6:14am
CSOUobj is the instance of user control..and ListGridList_LinkLabelClicked event is on the form

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There is no such thing as "call a control". A control is not a method, function, procedure, subroutine, property or operator.
Also, there are not "dynamic" or "static" ways of adding a control. It's always "dynamic". When you use a designer, it simply writes the code for adding controls in forms.

So, the "secret weapon" is simple: if you don't know how to do something in code, try to do it in designer, just to make a code sample. When it is done, look at the auto-generated code (in Forms, it is placed as a child node of the form node in the Solution Explorer of Visual Studio), and learn how to do it.

For example:
Panel parentPanel = new Panel();
//... let's assume the panel is already added
TextBox myTextBox = new TextBox();
Button myButton = new Button();
// take care about layout, name, etc.
// now, events:
myButton.Click += (sender, eventArgs) => { DoSomethingOnButtonClick(); };
// equivalent way good for old C# v.2:
myButton.Click += delegate(object sender, System.EventArgs eventArgs) { DoSomethingElseOnButtonClick(); };
// add them:
myButton.Parent = parentPanel;
// another equivalent way to add
// the panel itself was added in the same way;
// all the way to the Form, which is also a Control...


Now, the order of operation:

You need to insert controls in reverse order relative to the order of TAB navigation. You can also adjust TabOrder values.

If the form is already shown on screen, you still can add controls, but apparently, the last operations should be Controls.Add, to avoid possible flicker. If the form is not shown at the moment of adding control, this rule is not important, but, for the universal approach and better maintenance, I would advice to add controls in last steps, when layout is added.

The regular way to prevent layout flicker is sandwiching code in SuspendLayout/ResumeLayout of the top-level parent control, usually Form:[^],[^].


Again, if you don't know how something else is done, try it with the designer and see how it is done in auto-generated code. But don't copy coding style, end never use auto-generated names, they violate Microsoft naming conventions (because there is no way to observe them in code generation which does not "know" your semantic), always use semantically sensitive names. For adding event handlers, better use anonymous methods.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100