Results 1 to 2 of 2

Thread: [2005] Creating a Class.cs File for Input & Otherstuff

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    16

    [2005] Creating a Class.cs File for Input & Otherstuff

    Hi, I'm pretty new to C#, I've made a simple Duck Hunt game, but now I'm trying to creating a new game, called Jail Break. But anyways, what I would like to do is have all my PlayerInput (Keyboard Input) in one Class File instead of the Big Form1.cs Code File being gaint.

    I've tried something like this.

    Created new Class File called it PlayerInput.cs

    In it, I have:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Jail_Break
    {
        class PlayerInput
        {
        }
    }
    Just a simple Class File, right? now I want in it is what shows up when you do a Event for Keydown in Form1.cs

    Code:
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
    
            }
    So what I'm trying to do is have the Form1.cs KeyDown WHOLE CODE in the PlayerInput.cs File

    Something like this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Jail_Break
    {
        class PlayerInput
        {
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
    Code for KeyDown Here.. .. . . .
            }
        }
    }
    and then I would like to call the code either in the:

    Code:
            public Form1()
            {
                InitializeComponent();
    Call Code Here . . . . 
            }

    or in the Form1_Load Event.


    Please someone help, this will make my code alot neater, and i can't figure out how to do something like this, I've never ever used a Created Class before for Events or Other stuff.


    Thanks, Wesley

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Creating a Class.cs File for Input & Otherstuff

    Well you won't be able to capture the event from the form in another class like that. What you may want to do is create a function in the class (you could call it OnKeyDown or anything). Then in the form, when the event triggers, just call the function you made within the class.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width