Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows form
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 29-Mar-13 0:46am
Comments
Mahesh_Bhosale at 29-Mar-13 5:55am
   
Can you share the piece of code, where you create an instance for user control..?
ankitjha23 at 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);

}
ankitjha23 at 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
good
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
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
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2014
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