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. USer Control Error

USer Control Error

Scheduled Pinned Locked Moved C#
graphicsgame-devhelpquestion
3 Posts 3 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.
  • S Offline
    S Offline
    SRJ92
    wrote on last edited by
    #1

    hi, ever since i put in a user control, my other buttons don't work why is this ? The form opens with this user control then once the user clicks "start" it disappears (visible = false) and displays the game behind it, this is the delicate code... for the Delicate for the useer control

        void usrMenu1\_ClickMenu(string Button)
       {
           switch (Button)
           {
               case "Start":
                   Gameplay = true;
                   usrMenu1.Visible= usrSettings1.Visible = false;
                   usrMenu1.Hide();
                   usrSettings1.Hide();
                   break;
                case "Settings":
                usrMenu1.Visible = false;
                usrSettings1.Visible = true;
                   break;
           }
        }
    

    This is the button control..

    private void FrmGame_KeyDown(object sender, KeyEventArgs e)
    {
    if(Gameplay == true)
    {
    if (e.KeyCode == Keys.Up)
    {
    if (U == true)
    {
    //tim_Player.Enabled = true;
    Player.Direction = 1;
    Player.Normal = new Bitmap("images\\playerup.png");
    U = true;
    R = true;
    L = true;
    D = false;
    }
    }
    if (e.KeyCode == Keys.Right)
    {
    if (R == true)
    {
    Player.Normal = new Bitmap("images\\playerright.png");
    //tim_Player.Enabled = true;
    Player.Direction = 3;
    L = false;
    R = true;
    D = true;
    U = true;
    }
    }
    if (e.KeyCode == Keys.Down)
    {
    if (D == true)
    {
    Player.Normal = new Bitmap("images\\playerdown.png");
    //tim_Player.Enabled = true;
    Player.Direction = 2;
    R = true;
    L = true;
    U = false;
    D = true;
    }
    }
    if (e.KeyCode == Keys.Left)
    {
    if (L == true)
    {

    OriginalGriffO _ 2 Replies Last reply
    0
    • S SRJ92

      hi, ever since i put in a user control, my other buttons don't work why is this ? The form opens with this user control then once the user clicks "start" it disappears (visible = false) and displays the game behind it, this is the delicate code... for the Delicate for the useer control

          void usrMenu1\_ClickMenu(string Button)
         {
             switch (Button)
             {
                 case "Start":
                     Gameplay = true;
                     usrMenu1.Visible= usrSettings1.Visible = false;
                     usrMenu1.Hide();
                     usrSettings1.Hide();
                     break;
                  case "Settings":
                  usrMenu1.Visible = false;
                  usrSettings1.Visible = true;
                     break;
             }
          }
      

      This is the button control..

      private void FrmGame_KeyDown(object sender, KeyEventArgs e)
      {
      if(Gameplay == true)
      {
      if (e.KeyCode == Keys.Up)
      {
      if (U == true)
      {
      //tim_Player.Enabled = true;
      Player.Direction = 1;
      Player.Normal = new Bitmap("images\\playerup.png");
      U = true;
      R = true;
      L = true;
      D = false;
      }
      }
      if (e.KeyCode == Keys.Right)
      {
      if (R == true)
      {
      Player.Normal = new Bitmap("images\\playerright.png");
      //tim_Player.Enabled = true;
      Player.Direction = 3;
      L = false;
      R = true;
      D = true;
      U = true;
      }
      }
      if (e.KeyCode == Keys.Down)
      {
      if (D == true)
      {
      Player.Normal = new Bitmap("images\\playerdown.png");
      //tim_Player.Enabled = true;
      Player.Direction = 2;
      R = true;
      L = true;
      U = false;
      D = true;
      }
      }
      if (e.KeyCode == Keys.Left)
      {
      if (L == true)
      {

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Without looking in too much depth at that monolithic code, I think the first improvement you make should be to these lines:

                  if (e.KeyCode == Keys.Up && e.KeyCode == Keys.Right)
                  if (e.KeyCode == Keys.Down && e.KeyCode == Keys.Right)
                  if (e.KeyCode == Keys.Up && e.KeyCode == Keys.Left)
                  if (e.KeyCode == Keys.Down && e.KeyCode == Keys.Left)
      

      Perhaps OR might work better than AND?

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • S SRJ92

        hi, ever since i put in a user control, my other buttons don't work why is this ? The form opens with this user control then once the user clicks "start" it disappears (visible = false) and displays the game behind it, this is the delicate code... for the Delicate for the useer control

            void usrMenu1\_ClickMenu(string Button)
           {
               switch (Button)
               {
                   case "Start":
                       Gameplay = true;
                       usrMenu1.Visible= usrSettings1.Visible = false;
                       usrMenu1.Hide();
                       usrSettings1.Hide();
                       break;
                    case "Settings":
                    usrMenu1.Visible = false;
                    usrSettings1.Visible = true;
                       break;
               }
            }
        

        This is the button control..

        private void FrmGame_KeyDown(object sender, KeyEventArgs e)
        {
        if(Gameplay == true)
        {
        if (e.KeyCode == Keys.Up)
        {
        if (U == true)
        {
        //tim_Player.Enabled = true;
        Player.Direction = 1;
        Player.Normal = new Bitmap("images\\playerup.png");
        U = true;
        R = true;
        L = true;
        D = false;
        }
        }
        if (e.KeyCode == Keys.Right)
        {
        if (R == true)
        {
        Player.Normal = new Bitmap("images\\playerright.png");
        //tim_Player.Enabled = true;
        Player.Direction = 3;
        L = false;
        R = true;
        D = true;
        U = true;
        }
        }
        if (e.KeyCode == Keys.Down)
        {
        if (D == true)
        {
        Player.Normal = new Bitmap("images\\playerdown.png");
        //tim_Player.Enabled = true;
        Player.Direction = 2;
        R = true;
        L = true;
        U = false;
        D = true;
        }
        }
        if (e.KeyCode == Keys.Left)
        {
        if (L == true)
        {

        _ Offline
        _ Offline
        _Erik_
        wrote on last edited by
        #3

        First of all, it would be good to know if this is Windows.Forms or WPF, becouse the possible solutions might be different. I guess your usrMenu1 object is an instance of your user control. You have defined a new delegate for the ClickMenu event, so the target method receives an string. It is not strictily bad, the code compiles and works, but it is not the best practice. You should better follow the structure of all event delegates defined in .NET, I mean, the target method should receive two parameters, you know, (object sender, EventArgs e), and if you need to pass some aditional data, you just have to extend EventArgs class subclassing it and adding what you need. The problem here might be in the code which raises the ClickMenu event, and you have not posted it here, or might be in the KeyDown event handler you have posted becouse there are several mistakes in it. That said, I would like to help you improve this code a little. In this case, instead of a strange ClickMenu event which sends a string I would have defined two different events using the "standard" delegate EventHandler: one for Start button and another one for Settings button. On the other hand, there is a huge optimization job you should do on your code. These composed conditions are always false, and the code of these if blocks do never execute:

        if (e.KeyCode == Keys.Up && e.KeyCode == Keys.Right) // this is always false

        Considering this, you do not need the if blocks to set the values to the four bool variables you are using, these four lines would do the same:

        U = e.KeyCode != Keys.Down;
        D = e.KeyCode != Keys.Up;
        L = e.KeyCode != Keys.Right;
        R = e.KeyCode != Keys.Left;

        So setting Player.Normal and Player.Direction properties might be done with a switch.

        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