Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help needed XNA and Window Forms

Help needed XNA and Window Forms

Scheduled Pinned Locked Moved C#
questiongame-devhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    larsp777
    wrote on last edited by
    #1

    Hi! I have a XNA Project with a XNA-game contained inside a winform. My program.cs looks like this:

    Form1 form = new Form1();
    form.Show();
    Game1 game = new Game1(form.getDrawSurface());
    form.Game = game; //The new line
    game.Run();

    I have a setter and a getter in the winfom like this:

    Game1 game = new Game1();

    public Game1 Game
    {
    get
    {
    return game;
    }
    set
    {
    game = value;
    }
    }

    So now I can send values from my form to my Game1 in XNA but not the other way around. How can I send values from XNA to the WinForm? I have used som solutions I have found on the internet so I don't fully understand everything.

    D 1 Reply Last reply
    0
    • L larsp777

      Hi! I have a XNA Project with a XNA-game contained inside a winform. My program.cs looks like this:

      Form1 form = new Form1();
      form.Show();
      Game1 game = new Game1(form.getDrawSurface());
      form.Game = game; //The new line
      game.Run();

      I have a setter and a getter in the winfom like this:

      Game1 game = new Game1();

      public Game1 Game
      {
      get
      {
      return game;
      }
      set
      {
      game = value;
      }
      }

      So now I can send values from my form to my Game1 in XNA but not the other way around. How can I send values from XNA to the WinForm? I have used som solutions I have found on the internet so I don't fully understand everything.

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Create custom events in your Game1 class and subscribe to those events from the game instance in your form

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      L 1 Reply Last reply
      0
      • D DaveyM69

        Create custom events in your Game1 class and subscribe to those events from the game instance in your form

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        L Offline
        L Offline
        larsp777
        wrote on last edited by
        #3

        Thanks. Can you give me an example? Is it like a method?

        D M 2 Replies Last reply
        0
        • L larsp777

          Thanks. Can you give me an example? Is it like a method?

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Form1 form = new Form1();
          form.Show();
          Game1 game = new Game1(form.getDrawSurface());
          form.Game = game; //The new line
          game.GameEvent += HandleGameEvent;
          game.Run();

          // Inside Form1
          private void HandleGameEvent(object sender, EventArgs e)
          {
          //
          }

          public class Game1
          {
          public event EventHandler GameEvent;

          protected vitual void OnGameEvent(EventArgs e)
          {
          EventHandler eh = GameEvent;
          if(eh != null)
          eh(this, e);
          }
          }

          Check out Basic Event Creation in C#[^] and Events Made Simple[^]

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          L 1 Reply Last reply
          0
          • L larsp777

            Thanks. Can you give me an example? Is it like a method?

            M Offline
            M Offline
            Marco Bertschi
            wrote on last edited by
            #5

            How to raise and consume Events[^]

            Clean-up crew needed, grammar spill... - Nagy Vilmos

            1 Reply Last reply
            0
            • D DaveyM69

              Form1 form = new Form1();
              form.Show();
              Game1 game = new Game1(form.getDrawSurface());
              form.Game = game; //The new line
              game.GameEvent += HandleGameEvent;
              game.Run();

              // Inside Form1
              private void HandleGameEvent(object sender, EventArgs e)
              {
              //
              }

              public class Game1
              {
              public event EventHandler GameEvent;

              protected vitual void OnGameEvent(EventArgs e)
              {
              EventHandler eh = GameEvent;
              if(eh != null)
              eh(this, e);
              }
              }

              Check out Basic Event Creation in C#[^] and Events Made Simple[^]

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              L Offline
              L Offline
              larsp777
              wrote on last edited by
              #6

              Thanks, it starting to be clear but it isn´t all that simple to me... :) So, suppose something happends in my XNA-game. There is a update-method were I check if the enemy is alive and within range of Another object:

              if (!(enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)))
              isActivated = false;

              if (enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)) {

              if ((depthChargeX >= enemyX - 50 && depthChargeX <= enemyX + 50) || (depthChargeY >= enemyY - 50 && depthChargeY <= enemyY + 50) && isActivated == true)
              {
              enemy.IsAlive = false;
              hit.Play();
              wreck2.IsAlive = true;

                                  wreck2.Update(gameTime);
                                 
                          }
                      }
              

              If it isn´t in range I want to send a bool or something to my form so I can e.g. enable or disable a button. Would I then send that bool to the OnGameEvent or must I create a new class like in one in one of the examples. I am a little confused still I must admit. But I suppose the HandleGameEvent in the form listens to the event raised. And

              game.GameEvent += HandleGameEvent;

              Is where you subscribe to the event raised by the game. I also get: The name 'HandleGameEvent' does not exist in the current context but I suppose something is missing.

              P 1 Reply Last reply
              0
              • L larsp777

                Thanks, it starting to be clear but it isn´t all that simple to me... :) So, suppose something happends in my XNA-game. There is a update-method were I check if the enemy is alive and within range of Another object:

                if (!(enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)))
                isActivated = false;

                if (enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)) {

                if ((depthChargeX >= enemyX - 50 && depthChargeX <= enemyX + 50) || (depthChargeY >= enemyY - 50 && depthChargeY <= enemyY + 50) && isActivated == true)
                {
                enemy.IsAlive = false;
                hit.Play();
                wreck2.IsAlive = true;

                                    wreck2.Update(gameTime);
                                   
                            }
                        }
                

                If it isn´t in range I want to send a bool or something to my form so I can e.g. enable or disable a button. Would I then send that bool to the OnGameEvent or must I create a new class like in one in one of the examples. I am a little confused still I must admit. But I suppose the HandleGameEvent in the form listens to the event raised. And

                game.GameEvent += HandleGameEvent;

                Is where you subscribe to the event raised by the game. I also get: The name 'HandleGameEvent' does not exist in the current context but I suppose something is missing.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                larsp777 wrote:

                The name 'HandleGameEvent' does not exist in the current context but I suppose something is missing

                You are missing the event handler itself. Assuming that it just passed across EventArgs, the handler would look like this:

                private void HandleGameEvent(object sender, EventArgs e)
                {
                }

                larsp777 wrote:

                If it isn´t in range I want to send a bool or something to my form so I can e.g. enable or disable a button.

                A simple event args class that looks like this should help:

                public class GameRangeEventArgs : EventArgs
                {
                public GameRangeEventArgs(bool isInRange) : base()
                {
                IsInRange = isInRange;
                }
                public bool IsInRange { get; private set; }
                }

                Now, in your Game, you would declare this as:

                public event EventHandler<GameRangeEventArgs> GameRangeEvent;

                To use this from your game code, I would typically have a helper method that looks like this:

                private void RaiseGameRangeEvent(bool isInRange)
                {
                var handler = GameRangeEvent;
                if (handler != null)
                {
                handler(this, new GameRangeEventArgs(isInRange));
                }
                }

                Now, in your code you simply call this method and the event is raised if there is an event handler attached to it in the form. So, what would this look like in the form? Well, you will have something like this:

                game.GameRangeEvent += HandleGameRangeEvent;
                ...
                private void HandleGameRangeEvent(object sender, GameRangeEventArgs e)
                {
                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups