Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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,
Ankit
Posted
Updated 14-Nov-19 18:53pm
Comments
Mahesh_Bhosale 29-Mar-13 5:55am    
Can you share the piece of code, where you create an instance for user control..?
ANKIT_JHA 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();
MessageBox.Show(SoNbr);
lstClosedSO.ShowListGrid();
ClosedSOUserControl CSOUCobj = new ClosedSOUserControl();
CSOUCobj.Controls.Add(CSOUCobj);

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

1 solution

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:
C#
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
parentPanel.Controls.Add(myTextBox);

// the panel itself was added in the same way;
// all the way to the Form, which is also a Control...


[EDIT]

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.resumelayout.aspx[^].

[END EDIT]

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.

—SA
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900