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