How to Access Custom Control in LightSwitch



Prologue:

My previous article discussed adding a custom control to a LightSwitch screen. In this part II, we shall see how to access the custom control added to the LightSwitch screen in Part I.

We can take the LightSwitch demo application we created in part I of this article series. As we discussed in part I, create a demo application.

We have added the custom control; if you want to do something with the control, we need to access the custom control.

Editing Activated Method:

To access the custom control, we have to go to code-behind of the screen.

LSCusCont1.gif

As shown in the figure, go to the "Activated" method of the "CreateNewPerson" screen.

This method will be executed when the screen is active. So after loading the screen we can access the custom control so that we can do the stuff with the custom control.

Accessing Custom Control:

From the code snippet given below, we know how to access the custom control.


LSCusCont2.gif

Here,

  • The "FindControl" method is used to find the control within the screen.
  • The "FindControl" method returns "IContentItemProxy" which has the event "ControlAvailable" which will be fired if the control is available. If the control is available then we are displaying a message.

So the "ControlAvailable" event can do all the stuff.

Hit F5 to see the application in action.

Application in Action:


LSCusCont3.gif

If you press F5, you will see a message box saying that "control has been added".

Summary:

In this article part II, we have seen how to access the custom control which was added to the LightSwitch screen.

Thanks for spending your precious time here. Please provide your valuable feedback and comments, which enable me to give a better article the next time.

Thanks.

Next Recommended Readings