Domanda

I am working from a tutorial that has got me up and running on winforms. it basically gave me the window, button and label controls. i have tried to add a textbox, which is displaying, but it does not allow any input from the user. what am i doing wrong. here is the code. i included it all so it would be easy to copy/paste. thanks in advance for your help. fyi im using 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);
        }
    }
}
È stato utile?

Soluzione

XNA probably suppresses keyboard input. This took me a while to solve but to go about this use event KeyPreview and inside set e.IsInputHandled to true. or use this code as your textbox class:

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;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top