Create a Button

Buttons can be used in multiple ways that can help provide user interaction in your game. Creating a Button is just as simple as creating a Form. The following code places a Button on your games viewport.

        protected override void Initialize()
        {
            Button button = new Button(this, "Button", null);
            this.Components.Add(button);

            base.Initialize();

            button.Show();
        }

Buttons can be moved around just like a Form and clicked.

The null argument provided in the button constructor can be omitted if you are building the game for Windows PC. You can instead do the following

            Button button = new Button(this, "Button");

Both of these are used when a Button is placed directly on the viewport. The third argument is used when you are placing the Control on a Form which we will cover later. Since we are not placing on a Form, we don't need it so it is omitted in the 2nd code example. The 1st code example however is Windows Phone 7 compatible. Since you want to create the button without a Form on a Windows Phone, you supply a null argument instead.

Last edited Jan 19, 2012 at 10:29 PM by Scionwest, version 1

Comments

No comments yet.