Click here to Skip to main content
12,293,511 members (62,448 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.9K views
Posted

XNAMessageBox, NumberOnlyTextbox, and Allowing DoubleBuffering in WinForms

, 24 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
XNAMessageBox, NumberOnlyTextbox, and allowing DoubleBuffering in WinForms.
XnaMessageBox Library Code:
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:
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:
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
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)

Share

About the Author

shelby67
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.

You may also be interested in...

Comments and Discussions

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 24 Nov 2011
Article Copyright 2011 by shelby67
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid