Posts in category .net - page 22

XNA Game Studio Beta 2 has Xbox 360 support

Screenshot of the XNA Game Studio 360 registration windowYesterday I predicted the ability to run XNA code on our Xbox 360’s today.

We’re not quite there yet but we are a lot closer than most people believed this time yesterday.

Contrary to preliminary info on beta 2 of XNA Game Studio before it release it DOES in fact contains the necessary pieces for 360 development.

Specifically the registration window inside XNA Game Studio, shown here, the XnaTrans.exe tool for transferring builds over to the 360 and a surprise XNA Remote Performance Monitor for Xbox 360 tool.

I believe we have all the software we need right now. All we need Microsoft to do is to enable the Xbox 360 side – most likely a menu option alongside the Connect to media PC that’ll throw the connection key up on the screen that the PC side needs.

If anyone from the XNA team wants to turn mine on for testing my gamertag is DamienG ;-)

Update

It would seem the XNA team are prohibited from allowing beta code to run on retail Xbox 360’s :( Let’s hope they get to a 1.0 release sooner rather than later. A comprehensive look at the other beta 2 changes can be found at the XNA Team Blog.

[)amien

WinForms tricks & tips

TreeView right-mouse button select the node

I’m not sure why it doesn’t do this as standard but a simple event handler should do the trick:

private void treeView_MouseDown(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Right) {
    TreeNode node = treeView.GetNodeAt(e.X, e.Y);
    if (node != null)
        treeView.SelectedNode = node;
    }
}

Adding text to a text box

People have asked (in the IRC #CSharp) why adding text to a TextBox is so slow and flickery. Normally they are trying:

textBox1.Text += myNewText;

The problem with this is that it copies all the Text from the text box then adds myNewText to it and copies the whole result back. This is because strings in .NET are immutable, i.e. can’t be changed, and so adding one string to another always results in this overhead (and hence the existence of the StringBuilder class).

The solution is to abandon the slow, inefficient string concatenation and use the method AppendText thusly:

textBox1.AppendText(myNewText);

Which is fast and efficient whilst also being available to TextBox, RichTextBox and MaskedTextBox (by virtue of being a method of BaseTextBox).

They usually also ask how to make the text box scroll to the end. Just use the following line:

textBox1.ScrollToCaret();

Don’t forget keyboard input

Check those tab orders and accelerator keys!

Creating dynamic controls

If you’re ever unsure how to work with a dynamic control just create it in Visual Studio’s Designer and then head into the .designer.cs file and examine the code it generates.

Windows Forms FAQ

There are many other hints, tips and solutions in the Windows Forms FAQ.

[)amien

Compile XNA for your Xbox 360 tomorrow?

There’s a possibility that tomorrow will see the announcement of XNA Game Studio Beta 2 with support for compiling and running applications on your Xbox 360.

Originally this was scheduled for the final 1.0 release and would involve a $99 annual fee for the privilege but what better way to get hype and excitement than to offer it free for a month or two while it’s being polished and tested during the beta phase?

Before you think I’m dreaming consider that Microsoft released the Xbox 360 Halloween firmware update this morning complete with XNA support, admittedly overshadowed by the announcement of 1080p support – another thorn in Sony’s leaky side.

The official release notes made it clear that the XNA support was tied into future availability and subject to subscriptions. The page on Xbox.com just comes out and plain says “now”.

Dave over at LetsKillDave says they’ll be an announcement about XNA Game Studio Beta 2 in the next 24 hours although he has previously stated that the demonstrated technique for deploying games to the 360 won’t be available until 1.0.

Guess we’ll have to wait and see.

[)amien

Parameterising the IN clause of an SQL SELECT in .NET

I’m a fan of parameterized queries with a strong dislike for building SQL (or other magic strings). Encoding, escaping errors, localization formatting problems and injection can run rampant when you think everything is a string.

Problem

Today I found myself a quandary as I needed to SELECT records based on a list of values I had. e.g.

SELECT * FROM Products WHERE ProductCode IN ('ABC123', 'DEF456', 'GHI789')

At first glance the relevant parameterized version might look like:

SELECT * FROM Products WHERE ProductCode IN (@productlist)

The problem here however is that if you put a comma-separate list of items into a string parameter named @productlist then it sends this to the database server:

SELECT * FROM Products WHERE ProductCode IN ('ABC123, DEF456, GHI789')

That’s not what we want at all.

This hack isn’t pretty and it has some limitations:
  • Only works with named parameters
  • Could upset some DB providers that don’t like having command parameters removed or command text modified
  • Parameter name to replace must be totally unique – i.e. not exist as a subset of another parameter name
  • Only preserves the basic IDbCommand declared properties

It should however work across DB providers and types.

Usage

The previous example would mean we use exactly the expected parameterized version with @productlist in place.

Add the parameter as you’d expect but instead of assigning a string/numeric to it assign something IEnumerable.

Finally call this method against the command and parameter before you execute it for the ‘magic’ to happen:

The ‘magic’

public void ExpandDbArrayParameter(IDbCommand cmd, IDbDataParameter parameter) {
  if (parameter.Value is IEnumerable) {
    int index = 0;
    StringBuilder newParameterSQL = new StringBuilder();
    foreach(Object value in (IEnumerable) parameter.Value) {
      String valueParameterName = String.Format("{0}{1}", parameter.ParameterName, ++index);
      IDataParameter valueParameter = cmd.CreateParameter();
      valueParameter.DbType = parameter.DbType;
      valueParameter.Direction = parameter.Direction;
      valueParameter.ParameterName = valueParameterName;
      valueParameter.SourceColumn = parameter.SourceColumn;
      valueParameter.SourceVersion = parameter.SourceVersion;
      valueParameter.Value = value;
      cmd.Parameters.Add(valueParameter);

      if (index == 1)
        newParameterSQL.Append(valueParameterName);
      else
        newParameterSQL.Append("," + valueParameterName);
      }
      cmd.Parameters.Remove(parameter);
      cmd.CommandText = cmd.CommandText.Replace(parameter.ParameterName, newParameterSQL.ToString());
    }
}

[)amien