Event Triggers

In most cases, you are going to need some sort of custom code for each of your Controls on the screen. X-Forms provides Event Methods for you to use that can help flesh out your GUI and make it do exactly what you need it to do.

To demonstrate executing code when a button is clicked, we will create a Form and a Button and then create a method that the Buttons OnClick event will call. We will then write the code to change the Form to fullscreen when clicked and then back to normal.

        protected override void Initialize()
        {
            StandardForm form = new StandardForm(this, "New Form");
            Button button = new Button(this, "Button", form);
            this.Components.Add(form);

            base.Initialize();

            button.OnClickEvent += new BaseControl.OnClickHandler(button_OnClickEvent);
            form.Show();
        }

        void button_OnClickEvent(BaseUI sender)
        {
            Button button = (Button)sender;
            if (button.Owner.IsFullscreen)
                button.Owner.SetFullScreen(false);
            else
                button.Owner.SetFullScreen(true);
        }

Since all Controls (Buttons are considered Controls) have a Owner property, we can cast the sender argument to a Button field and have access to the button that clicked the event, even though there is no property or fields declared at the class scope level.
You can access the Owning Form of any Control via its Owner property like we have here.

Last edited Jan 20, 2012 at 12:39 AM by Scionwest, version 1

Comments

No comments yet.