文本框不允许输入

时间:2014-04-30 06:48:16

标签: c# winforms textbox xna

我正在使用一个让我在winforms上运行的教程。它基本上给了我窗口,按钮和标签控件。我试图添加一个正在显示的文本框,但它不允许来自用户的任何输入。我究竟做错了什么。这是代码。我把它全部包括在内以便复制/粘贴。在此先感谢您的帮助。 fyi im使用studio 2010 with xna 4.0

namespace AddingWinformControlsTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Texture2D img;
        Panel pnl = new Panel();   
        Label lbl = new Label();
        TextBox txt = new System.Windows.Forms.TextBox();


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

            // allow the mouse to remain visible and allow user resizing ofthe window
            this.IsMouseVisible = true;
            this.Window.AllowUserResizing = true;

            // create and setup the winform controls on the game window
            this.CreateControls();

            // hook into the client size change event so we can update the win form
            // controls and update the viewport on the graphics device
            this.Window.ClientSizeChanged += this.Window_ClientSizeChanged;
        }

        private void CreateControls()
        {
            // get game window as a win form Form class
            Form frm = Control.FromHandle(this.Window.Handle) as Form;
            frm.SuspendLayout();

            // setup the panel control
            this.pnl.Dock = DockStyle.Right;
            this.pnl.Width = 250;

            // createa exit button and add it to the panel
            Button btn = new Button();
            btn.Location = new System.Drawing.Point(10, 10);
            btn.Text = "Exit";
            btn.Click += (sender, e) => { this.Exit(); };

            // add the button to the panel and add the panel to the game window form
            this.pnl.Controls.Add(btn);
            frm.Controls.Add(this.pnl);

            // setup the lable control and add it to the panel control
            this.lbl.Text = "";
            this.lbl.Location = new Point(10, btn.Top + btn.Height + 10);
            this.lbl.AutoSize = true;
            this.pnl.Controls.Add(this.lbl);
            // Text box
            txt.Text = "test";
            txt.Location = new Point(100, 100);
            txt.AcceptsReturn = true;
            txt.AcceptsTab = true;
            txt.Enabled = true;
            txt.ReadOnly = false;
            txt.Size = new System.Drawing.Size(50, 16);
            this.pnl.Controls.Add(this.txt);

            frm.ResumeLayout(false);
            frm.PerformLayout();

        }

        void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            // get the viewport from the graphics device
            var vp = this.GraphicsDevice.Viewport;
            // change the viewport dimensions so that it is not drawn under any of our winform controls
            vp.Width = this.Window.ClientBounds.Width - pnl.Width;
            vp.Height = this.Window.ClientBounds.Height;
            // set the viewport back onto the graphics device
            this.GraphicsDevice.Viewport = vp;
            // update the label to display the rectangle info
            Rectangle rect = new Rectangle(vp.X, vp.Y, vp.Width, vp.Height);
            lbl.Text = "Client: " + this.Window.ClientBounds.ToString() +
                       "\r\n" +
                       "Viewport: " + rect.ToString();
        }

        /// <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()
        {
            this.Window_ClientSizeChanged(this, EventArgs.Empty);

            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);
             // load a sample image that will be drawn the size of the viewport
            this.img = this.Content.Load<Texture2D>("Map2");
        }

        /// <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);

            // draw the image the same size of the viewport
            this.spriteBatch.Begin();
            this.spriteBatch.Draw(this.img, this.GraphicsDevice.Viewport.Bounds, img.Bounds, Color.White);
            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

XNA可能会抑制键盘输入。这需要我一段时间来解决,但是要使用事件KeyPreview和内部设置e.IsInputHandled为true。或使用此代码作为文本框类:

public class RespondingTextBox : System.Windows.Forms.TextBox
{
    public RespondingTextBox()
    {
        PreviewKeyDown += txtBox_PreviewKeyDown;
    }
    void txtBox_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
    {
        e.IsInputKey = true;
    }
}