954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Code Confusion

ok, after i got thelp with keypresses it is time to go one step further. i am ,making a very simple hangman game. the user will guess letters like you mormally do in hangman. what i want to be able to do is if he cant remember what he has guessed he can press a button. with that presss of the buttion all the letters/ keys that were captured would show up in a text box. but they would only show up when he pressed them.

i dont know if this is possible so any help would be great

Tayspen

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

couple of different ways you can handle it
I would probably do something like the following.
When the game starts instantiate a string called lettersUsed to be an empty string

string lettersUsed = "";


then, when someone hits a letter, add it to the string if it isn't already there. (adding to the code from your previous post)

case(Keys.A):
 if(lettersUsed.IndexOf("a") = -1)  lettersUsed+= "a";
 break;


then when they hit space

case(Keys.Space):
 MessageBox.Show("Letters used: " + lettersUsed);
 break;
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

great thank you. you guys are very helpful on this forum

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

ug im still consused though it always says lettersUsed is not defined. would any body mind giving sample code.

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

you probably need to do something like. This is all pseudocode

class Hangman{
string usedLetters= "";;
...
function startGame{
usedLetters = "";
}

}

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

call me stupid but i cant get it it always gives me erros index of... does not exist

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

send me your code

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

heres that part of it keep in mind im new :D


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsApplication1
{
public partial class Form1 : Form
{
string keyData = "";

public Form1()
{
InitializeComponent();
string keyData = "";

}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
bool result = false;
switch (keyData)
{
case(Keys.A):
if(lettersUsed.IndexOf("a") = -1) lettersUsed+= "a";
break;

case(Keys.Space):
MessageBox.Show("Letters used: " + lettersUsed);
break;
break;





}
return result;
}


[DllImport("winmm.dll")]
private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);


}
}

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

not sure y that play sound dll import is in there lol

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

here it is closer i think

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsApplication1
{
public partial class Form1 : Form{
string lettersUsed = "";

public Form1()
{
InitializeComponent();


}

protected override bool ProcessCmdKey(ref Message msg, Keys lettersUsed)
{
bool result = false;
switch (lettersUsed)
{
case(Keys.A):
if(lettersUsed.IndexOf("a") = -1) lettersUsed+= "a";
break;

case(Keys.Space):
MessageBox.Show("Letters used: " + lettersUsed);
break;
break;





}
return result;
}


[DllImport("winmm.dll")]
private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);


}
}

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

i see the problem. you are using the same variable name twice.
you have it as the name of the string and as the name of the parameter in ProcessCmdKey(ref Message msg, Keys lettersUsed).


try this instead

ProcessCmdKey(ref Message msg, Keys pressedKey){
....
switch(pressedKey){...

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

so this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{

string lettersUsed = "";

public Form1()
{
InitializeComponent();
}


protected override bool ProcessCmdKey(ref Message msg, Keys pressedKey)
{
{
bool result = false;
switch (pressedKey)
{
case (Keys.A):
if (lettersUsed.IndexOf("a") = -1) lettersUsed += "a";
break;

case (Keys.Space):
MessageBox.Show("Letters used: " + lettersUsed);
break;
}
return result;
}

}


}
}

if that is right i still get errors.... :(

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

what errors and when do you get them? compiling or running?

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

compiling

they are

cannot implicitly convert 'int' to 'bool'

and

the left hand side of an assigment must be a varible, propery, or idexer

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

do u no what the problem is?

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

made the most basic of mistakes while I was in a hurry to give you the first code that I gave you. in the if statements, change the = to ==

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

thank you hank you thank you thank you. i really aprciate your help

thank you

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You