Click here to Skip to main content
15,881,687 members
Articles / Programming Languages / C#
Tip/Trick

XNAMessageBox, NumberOnlyTextbox, and Allowing DoubleBuffering in WinForms

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
24 Nov 2011CPOL 14K   2
XNAMessageBox, NumberOnlyTextbox, and allowing DoubleBuffering in WinForms.
XnaMessageBox Library Code:
C#
using System;
using System.Runtime.InteropServices;

namespace Blaze.XNA.MessageBox
{
  public class XNAMessageBox
  {
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static uint MessageBox(IntPtr hWnd, string text, string caption, uint type);

    public void Show(string text, string caption, uint type)
    {
      int num = (int) XNAMessageBox.MessageBox(new IntPtr(0), text, caption, type);
    }
  }
}

Example of how to use:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Blaze.XNA.MessageBox;//Our library

namespace XnaMessageBox_SampleCode
{
    
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        Blaze.XNA.MessageBox.XNAMessageBox testBox = new XNAMessageBox();//Create new XNAMessageBox
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            testBox.Show("Hello World", "Test",0);//Test out our XNAMessageBox
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

NumberOnlyTextBox Code:
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
        return;
    e.Handled = true;
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
     if (e.KeyCode != Keys.Back || this.textBox1.Text.Length == 0)
        return;
     string text = this.textBox1.Text;
     string str = string.Empty;
     text.ToCharArray();
     char[] chArray = new char[text.Length - 1];
     for (int index = 0; index < chArray.Length; ++index)
     {
        chArray[index] = text[index];
        str = str + (object) chArray[index];
     }
     this.textBox1.Text = str;
     this.textBox1.SelectionStart = chArray.Length + 1;
}

DoubleBuffer Library
C#
using System.Reflection;

namespace Blaze.Controls.DoubleBuffer
{
  public class Buffer
  {
    public void SetToDoubleBuffer(object c)
    {
      typeof (object).InvokeMember("DoubleBuffered", BindingFlags.Instance | 
            BindingFlags.NonPublic | BindingFlags.SetProperty, (Binder) null, c, new object[1]
      {
        (object) true
      });
    }
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United States United States
I started learning HTML in 7th grade (2007) then went on to learn any and everything I could.
HTML/CSS/JavaScript/ActionScript/ASP.Net (6 years)
Basic (5 years)
C/C++ (4 years)
Java (4 years)
C# (3 years)
VB (2 years)

Out of all of these I have to say C# is by far my favorite since its wide spread, works for almost any solution and allows me to use Visual Studio.

Comments and Discussions

 
GeneralNumeric Textbox has no provision for , and . . Is this meant... Pin
oorja28-Nov-11 17:34
oorja28-Nov-11 17:34 
Question[My vote of 2] XNAMessage box Pin
Paul Boothroyd29-Nov-11 7:57
Paul Boothroyd29-Nov-11 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.