Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Control Problems 1

Status
Not open for further replies.

JoDaCoda

Programmer
Dec 11, 2003
18
US
I'm not sure if I'm doing something wrong or not, but every time I change something in my user control, all my controls get changed from Public to Protected. I have a user control which has menu buttons on it. If I add a button, all my other button's declarations switch from Public to Protected. I also have to double click each control to link the source code back to the button again. Here are my declarations that change:

Public MustInherit Class menu
Inherits System.Web.UI.UserControl
Public WithEvents btnInspector As System.Web.UI.WebControls.Button
Public WithEvents btnPosting As System.Web.UI.WebControls.Button
Public WithEvents btnReport As System.Web.UI.WebControls.Button
Public WithEvents btnInsurance As System.Web.UI.WebControls.Button
Public WithEvents btnTransaction As System.Web.UI.WebControls.Button
Public WithEvents btnUtility As System.Web.UI.WebControls.Button
Public WithEvents btnViolation As System.Web.UI.WebControls.Button
Public WithEvents btnOptions As System.Web.UI.WebControls.Button
Public WithEvents btnOwners As System.Web.UI.WebControls.Button
Public WithEvents btnBoiler As System.Web.UI.WebControls.Button

Now if I change anything in the control, such as add a new button, my code is automatically changed to this:

Public MustInherit Class menu
Inherits System.Web.UI.UserControl
Protected WithEvents btnInspector As System.Web.UI.WebControls.Button
Protected WithEvents btnPosting As System.Web.UI.WebControls.Button
Protected WithEvents btnReport As System.Web.UI.WebControls.Button
Protected WithEvents btnInsurance As System.Web.UI.WebControls.Button
Protected WithEvents btnTransaction As System.Web.UI.WebControls.Button
Protected WithEvents btnUtility As System.Web.UI.WebControls.Button
Protected WithEvents btnViolation As System.Web.UI.WebControls.Button
Protected WithEvents btnOptions As System.Web.UI.WebControls.Button
Protected WithEvents btnOwners As System.Web.UI.WebControls.Button
Protected WithEvents btnBoiler As System.Web.UI.WebControls.Button

If I don't set the buttons as public, I get an error stating that the button's events can't be raised because the button is Private. It runs fine once I relink the source code to the buttons and switch all the controls back to Public. It's just a real pain to have to do that every time I make a change.
 
I guess VS.NET is trying to encapsulate the controls, which is the better way of doing things.

Try making your event handlers private in your control. If you want the form to handle your user control's events, you can also raise an event within your control or bubble it up.

 
Thanks. That fixed it. I figured it was poor programming on my part. I just didn't know the proper way of doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top