You are here:   Blogs > Zune HD Development Blog
  |  Login
 Blog List    
   
  
 Archive    
   
  
 View Blog    
Nov 6

Written by: host
11/6/2009 1:51 PM 

Zune HD - Form Class

First, I wanted to talk about the Form class.  This class is used to host all the Zune HD Forms controls.  It is similar to the Form class that is provided in the Windows desktop development environment, for those familiar with that.

Creating a Reference to Zune.Forms.dll

If you have not downloaded the Zune.Forms.dll and referenced it in your project or the source code and set a project reference, you should do this now.  The Zune.Forms.dll and source code are available on CodePlex.
Once referenced, put this line along with the others at the top of the Game1.cs file.
using Zune.Forms;

What About My Game1.cs File?

The Game1.cs file is created when you create a new project.  For Zune HD Forms to function properly, we need to make a minor change to this file.  Note, that in the Game1.cs file, there is a line that looks like this:
public classGame1 : Game
This class inherits from Microsft.Xna.Framework.Game class, and is required for your Zune HD application to run.  In to utelize the Zune HD Forms controls, you must change this line to this:
public classGame1 : Form
At this point, you are ready to begin using controls within the Zune.Forms namespace.  I am not going to go into detail on every control at this time (that will come later), but here is an example of creating a TextBox:
public Game1()
{
                graphics = new GraphicsDeviceManager(this);
      Content.RootDirectory = "Content";
 
      // Frame rate is 30 fps by default for Zune.
      TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
 
      TextBox tbFirstName = new TextBox(this);
      tbFirstName.Location = new Vector2(10, 10);
      tbFirstName.Size = new Vector2(100, 25);
      tbFirstName.Text = "First Name:";
}
 
That’s it! This code will display a TextBox control on your Form.

How Does the Form Class Work?

The Form class does the heavy lifting. As shown above, the code to create a new control is simple. How it actually draws and receives user inputs is another story. Let’s take a look at some of the work the Form class does.
First, note that the Form class inherits from the Game class.
public class Form : Game
The game class contains some methods that we can override and use to our advantage. First, is the LoadContent method. This method is responsible for loading things like images and fonts. Here is how it looks in the Form class:
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
      foreach (Control ctl in _controls)
      ctl.LoadContent();
 
      foreach (Control ctl in _sysControls)
            ctl.LoadContent();
 
_alphaBackground = Drawing.CreateRectangle(GraphicsDevice, 272, 480, new Color(Color.LightGray, 175))
}
 
The important thing to note is this line:
foreach (Control ctl in _controls)
ctl.LoadContent();
 
This code loops through each Control in the Form’s Control collection, and calls the LoadContent() method. This allows each control to have its own content that can be loaded.
This same concept is carried over into other overridable methods like the Draw and Update methods.
This explains the Form control at a high level. I will add additional information about this control, and others in the future.

Tags:

20 comment(s) so far...

Re: Zune HD Forms - Form Class

You've put something together that really seems interesting. I've tried using the Zune.Forms.dll from code plex and the sample code from this post. Once I try to run the application, I receive an error window stating that "An unhandled exception of type 'System.NullReferenceException' occurred in Zune.Forms.dll."

I'm using Visual Studio Team System 2008 Development Edition on a WinXP Pro SP3 x86 machine. I've installed XNA Game Studio 3.1 along with the Zune Extensions.

Is there something I'm doing wrong? I appreciate any feedback you can provide.

By Zune Newbie on   12/6/2009 3:46 PM

Re: Zune HD Forms - Form Class

This is due to the fact that Zune.Forms.dll is looking for resources that do not exist (i.e. Content items). Try downloading the Content files from zuneforms.codeplex.com/sourcecontrol/changeset/view/39385?projectName=ZuneForms#What you are looking for is under ZuneForms -> ZuneForms -> ContentAll content should be downloaded and then added to the content node in your project. As an alternative, I have updated the download at zuneforms.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=35777. It is now a StarterKit that can be used which DOES include the content.

By host on   12/23/2009 10:18 AM

Problem with Starterkit

Hello,

I need to do a survey for my school and plan on using the Zune Forms on the Zune.
I've downloaded the starterkit but when running my project on the zune it says :
"An unhandled exception of type 'System.NullReferenceException' occurred in Zune.Forms.dll"
I've pasted the code you show in this article so to create a textbox but that exception jumps up everytime and it concerns the line "SpriteBatch.Begin();".
Would be great if you could tell me how to fix that no time because I have a limited amount of time for my survey.

Thanks alot,

Gabriel

By Gabriel on   1/26/2010 1:27 PM

Re: Zune HD Forms - Form Class

Gabriel,

There have been many changes made to this project, and the starterkit has not been updated. Your best bet is to download the source code from here:

zuneforms.codeplex.com/SourceControl/list/changesets

By host on   1/26/2010 2:15 PM

Re: Zune HD Forms - Form Class

Hello,

Thank you very much for your very prompt response!
I downloaded the last version and it runs althought it appears there is a problem when trying to type in data in the textbox, the screen freezes.
Is Microsoft planning to adapt the windows form to the Zune forms, that is what you're doing right now?

Congrats for what you're doing. I'll try to use the Zune Forms for my survey.

Gabriel

By Gabriel on   1/26/2010 6:02 PM

Re: Zune HD Forms - Form Class

I am not sure what Microsoft's plans are in relationship to future Zune HD Development. I have written several applications using Zune Forms (for myself at the moment...). I hope that Microsoft will allow developers to utelize an app store similar to Apple's app store at some point in the future. If they do this, I would imagine they would make XNA a little more Zune HD friendly.

By host on   1/27/2010 9:58 AM

Re: Zune HD Forms - Form Class

I downloaded the file, but I can't find the .dll Could someone tell me what I am doing wrong? I have everything working execpt zune forms.

By John on   2/3/2010 3:36 PM

Re: Zune HD Forms - Form Class

John, if you download the source code, you should just have to build the project to create the .dll.

By host on   2/3/2010 4:39 PM

Re: Zune HD Forms - Form Class

Thanks, I have it now.

By John on   2/4/2010 5:48 PM

Re: Zune HD Forms - Form Class

Hi, I have downloaded your zuneforms, it seems great, but everytime I try to enter text my zune freezes up and I have to exit. Is there something I'm doing wrong?

By Connor on   2/15/2010 6:45 PM

Re: Zune HD Forms - Form Class

Hi, I have downloaded your zuneforms, it seems great, but everytime I try to enter text my zune freezes up and I have to exit. Is there something I'm doing wrong?

By Connor on   2/15/2010 6:59 PM

Re: Zune HD Forms - Form Class

I downloaded the source code but when i try to reference it i can't find the .dll file. What do i do?

By Elliott on   3/21/2010 8:37 PM

Re: Zune HD Forms - Form Class

I'm having trouble using the sample you've provided.

I've downloaded your latest source and compiled, then referenced the dll in my project.

I changed game1.cs type to be a Form as you described, and added the text box sample code, but when running the project I get a null reference exception around SpriteBatch.Begin(); in Zune.Forms.Form.

I've also added all the content (images/spritefonts) from the template project.

Any ideas?

By Adam Walker on   8/5/2010 10:08 AM

Re: Zune HD Forms - Form Class

I haven't worked on this code in some time, but if I remember correctly, there was a bug where the SpriteBatch was not being initialized causing the null reference exception. You may need to set the SpriteBatch object to an instance in the source.

By admin on   8/5/2010 11:45 AM

Re: Zune HD Forms - Form Class

Thanks for getting back to me so quickly!

Could you give me an example of how I need to "set the SpriteBatch object to an instance in the source."?

That'd be awesome!

By Adam Walker on   8/5/2010 11:56 AM

Re: Zune HD Forms - Form Class

It seems that Loadcontent is not being hit in Forms.Form. I initilized the spritebatch in the draw method, which got me past the first error, now theres an error in textbox with _font being null :(

By Adam Walker on   8/5/2010 12:19 PM

Re: Zune HD Forms - Form Class

It seems that Loadcontent is not being hit in Forms.Form. I initilized the spritebatch in the draw method, which got me past the first error, now theres an error in textbox with _font being null :(

By Adam Walker on   8/5/2010 12:20 PM

Re: Zune HD Forms - Form Class

On further inspection it seems that load isnt firing in any of the classes, so nothing is being initialized :(

Is there a simple fix for this?

By Adam Walker on   8/5/2010 12:25 PM

Zune HD Forms -- KeyBoard Event Handler

Hi for everyone jejeje. In this moment i try to use the Zune Form HD, but i have a biggg problem, the keyboard event it's not capture the touch event and put in the textbox the letter pressed, the zune it's frozen

I try to find a solution, but it's first time working with xna library and zune hd apps

By Eduardo Rotundo on   2/27/2011 11:44 AM

Re: Zune HD Forms - Form Class

The problem is the KeyBoard Event Handler, not respond a TouchEvent

By Eduardo Rotundo on   2/27/2011 1:43 PM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel