PDA

View Full Version : c# if (txtMessage.Text == ("home"))


wolfandcrow
12-07-2007, 12:35 PM
Hi,

What am trying to do is when you type home it will play the sound file. This works fine if i just type home on its own. But when i type do you like my home. The sound file does not play. Am guessing i need a bit of code that tells it to ignore all other characters unless in a certain sequence?

Also if there is an easy way to do this for a lot of words i would very keen to learn it.

Thanks




if (txtMessage.Text == ("home"))
playsound.SoundLocation = @"c:\simple.wav";

if (txtMessage.Text == ("home"))
playsound.Play();

Roelf
12-07-2007, 12:52 PM
if (txtMessage.Text.ToUpper().IndexOf("HOME") > -1)
playsound.......
the toupper part converts the string to uppercase to be sure it is caseinsensitive, then it returns the indexposition of "HOME". If the string is not found, the IndexOf method returns -1, so all indexes greater than -1 indicate that home appears in the typed text

wolfandcrow
12-07-2007, 01:37 PM
That works great thanks.


So it's the IndexOf("") that looks for the text.
And ToUpper() makes it so IndexOf("") finds the text in upper and lower case.
Am not sure about the > -1 i will read up on that.

Again Thanks

Roelf
12-07-2007, 01:41 PM
yes, the IndexOf looks for the provided text and returns the startposition of this text in the string. If the text is not found in the string the IndexOf method returns -1. So when the return of this method > -1, the text is found in the string.

Converting to uppercase and then looking for uppercase "HOME" ensures that if someone types HoMe, this will also be a positive match.

wolfandcrow
12-07-2007, 01:45 PM
ok i hit a small problem

if (txtMessage.Text.ToUpper().IndexOf("HOME") > -1)
playsound.SoundLocation = @"c:\simple.wav";

if (txtMessage.Text.ToUpper().IndexOf("HOME") > -1)
playsound.Play();

if (txtMessage.Text.ToUpper().IndexOf("HONER") > -1)
playsound.SoundLocation = @"c:\simple.wav";

if (txtMessage.Text.ToUpper().IndexOf("HONER") > -1)
playsound.Play();

as soon as i type HOME every bit of text after that plays the sound?

Roelf
12-07-2007, 02:06 PM
two things. The moment the sound is played, depends on the moment you perform these checks. If you execute this code after the field is left, it will play only then. Of you perform the check after every keystroke, it will play it after every kehstroke

other change i would suggest is change your code into:


if (txtMessage.Text.ToUpper().IndexOf("HOME") > -1) {
playsound.SoundLocation = @"c:\simple.wav";
playsound.Play();
}

if (txtMessage.Text.ToUpper().IndexOf("HONER") > -1) {
playsound.SoundLocation = @"c:\simple.wav";
playsound.Play();
}

the { and } are to create a codeblock. This block is executed when the tested condition is true so you dont have to perform the check for every line of code you want to execute

wolfandcrow
12-07-2007, 02:34 PM
hmmm ok this what i have it does not work in any other area. I don't think any way.


private void txtMessage_TextChanged(object sender, EventArgs e)
{

// sets the variable name playsound so it can be used
System.Media.SoundPlayer playsound = new System.Media.SoundPlayer();

if (txtMessage.Text.ToUpper().IndexOf("HOME") > -1)
{
playsound.SoundLocation = @"c:\simple.wav";
playsound.Play();
}

if (txtMessage.Text.Length == 0)
btnSend.Enabled = false;
else
btnSend.Enabled = true;


}

Roelf
12-07-2007, 02:44 PM
exactly what i thought, you are using the Textchanged event of the textbox. So after every keystroke, this method is executed. And thus the file starts to play.

you might want to look to use the lostfocus event to execute the playing of the file.

wolfandcrow
12-07-2007, 03:05 PM
i knew i was but it was the only place it worked.

ok these are what i have tried.

Enter Event.
GotFocus Event
KeyDown Event
KeyUp Event
KeyPress Event
Leave Event
LostFocus Event

some play the sound after every key stroke some do nothing. But none do what i need any ideas?