views:

194

answers:

3

Hi All

I am using VS2008 with both VB.NET and C#

I have created a simple custom user control and have added some public functions to it

my problem is that if I don't drag and drop the user control from the toolbox onto a winform but instead I try to create one dynamically via code - I am not able to access the public functions and properties

I can't understand why there would be a difference but htere seems to be.

would appreciate any help on the matter

thanks

A: 

Are you using the LoadControl method to load the control?

ie.

MyControl ctrl = (MyControl)LoadControl("~/MyControl.ascx"); 
John Boker
I add a using statement at the top of the form class e.gusing myUserControlsI define a variable as type user control at the top of my form class e.gUserControl myUC;then in the load event i assign it to my custom usercontrol like thismyUC = new MyUserControls.myCustomControl();this.controls.add(myUC);myUC.show();everything up to this point works fineI see the user control on the form but for some reason if i want to access a public function like:myUC.ShowDetails()the autocomplete doesn't show me the function at all Hope this makes sense
Avner
+1  A: 

You are defining your variable as type UserControl. As such, intellisense is only going to show you the members of the UserControl class, even if it is in fact of type myCustomControl.

Change your initial declaration to myCustomControl myUC; and you should be good to go.

Jay
works like a charm - thanks a lot
Avner
A: 

You neeed to type cast to the usercontrol type then it works For Ex :

public partial class uctls_contact_uctlCompanyViewContacts : System.Web.UI.UserControl { public bool ReadOnlyMode { get; set; } }

UserControl uctlCompanyViewContacts = (UserControl)Page.LoadControl("uctlCompanyViewContacts.ascx"); uctlCompanyViewContacts.ID = "uctlCompanyViewContacts"; panelCompanyViewContacts.Controls.Add(uctlCompanyViewContacts); ((uctls_contact_uctlCompanyViewContacts)uctlCompanyViewContacts).ReadOnlyMode = true;

sai Rani