Solved

UserControl how do I get the code in Load event run again when user control gets focus

Posted on 2006-11-10
4
187 Views
Last Modified: 2012-05-05
Hi,

I am using VS 2003 .NET 1.1 Winforms

I have a wizard like interface - main form with a panel and several UserControls.  The main form has next, back and finish button just like most other wizards.  Everything worked great so far.  Now, I have added two last UserControls each representing a page in the wizard.  In the first UserControl, 13 dynamically created checkbox with events hooked.  User select the checkboxes they want then click Next button.  When users select a checkbox, I create an object (let us call it Crop) and stick it into a HashTable and when they unselect a checkbox I remove the object from the HashTable.  This is all good.  The next screen displays data for the objects in the HashTable (this is done in UserControl Load event).

The problem ....

If a user selects 1 checkbox and goes to the next screen we see 1 object data, which is correct.  then if user clicks Back button to the previous screen and selects 3 more checkboxes then clicks Next, the next screen displays data for 1 object, the one user selected first time.  This is because, second time when user clicks next, UserControl Load event doesn't fire because the UserControl is already loaded.  I don't want to unload usercontrol everytime as this means I have to persist data, repopulate it into control as well as changing quite a bit of my design.

Is there a way round this?  I tried grapping the Enter event and the GotFocus event, none of them work for me.  all I need to do is call the 2 methods that build the screen when user comes back having changed some data in the previous screen

I hope someone can sus this out.

Thanks
Hassan
 
0
Comment
Question by:gbzhhu
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:so3
Comment Utility
Why don't you execute the 2 methods when you press the next button. You know when to show the usercontrol that displays data for the objects in the HashTable, right? so call the methods after you show the usercontrol
0
 
LVL 12

Author Comment

by:gbzhhu
Comment Utility
so3,

Thanks for your suggestion.  I didn't think of this to be honest but I should have.  Now that you brought it to my attention there is just one issue

1.  UserControl has a set of methods defined and my methods are not one of these, therefore, the workaround is to implement an interface that defines the two methods in the UserControl.  I think I will give this a go.

Thank you.  I will try this tomorrow when I am at work

Hassan
0
 
LVL 6

Accepted Solution

by:
so3 earned 500 total points
Comment Utility
I don't think that is need for an interface if you use only one Usercontrol do do the job just make those methods public and you will see them outside the class.
If you use those methods for multiple kinds of Usercontrol then you should use an interface but in your case from what i've understood i don't think that you need it.

so3
0
 
LVL 12

Author Comment

by:gbzhhu
Comment Utility
I already had the usercontrol implement an interface to communicate with the main form, so i just added the 2 methods and it looks good

Thank you for your help

Hassan
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now